1

When I try to delete array pointer which declared like

short *width = NULL;
width = new short[lenght];
delete[] width;

the programs stop at delete part and returns a random number.

here is my all program

int main(){
    short **junction = NULL, *width = NULL, length = 0;
    ifstream input;

    input.open("sample_input.txt");
    if(!input)
        cout << "No such a file named \"sample_input.txt\"" << endl;
    else if(input >> length){
        junction = new short*[length];
        width = new short[length];
        string str; 
        for(int i = 0; i - 1 < length && getline(input, str); i++){
            istringstream ss(str);
            width[i - 1] = (str.size() + 1) / 2; //numbers can be two char, so in this case, here we allocate more than necessary memory
            junction[i - 1] = new short[width[i - 1]];
            for(int j = 0; j < width[i - 1]; j++){
                if(ss.eof()){
                    junction[i - 1][j] = -1; // -1 is the key value of emtpy spaces
                    continue;
                }
                ss >> junction[i - 1][j];
            }
        }
    }
    input.close();

    /*
    for(int i = 0; i < length; i++)
        delete[] junction[i];
    delete[] junction;
    delete[] width;
    */

    return 0;
}

the comment part makes problem, I tried those delete statements seperately just for part is runing but not as I expected here is my sample input: sample input

5
  • 1
    There is nothing inherently wrong with the shown code. The problem lies in the context that you are not showing. Please provide a proper minimal reproducible example for your problem. See also How to Ask. Commented May 1, 2022 at 12:55
  • 1
    You can't have this kind of code at the global scope. Commented May 1, 2022 at 12:55
  • Please reduce your code to the minimal amount necessary to reproduce the issue. In particular avoid having all the file IO in there. If it is unavoidable to reproduce the issue, then specify the contents of the file as well. Commented May 1, 2022 at 12:58
  • 1
    That said, what index of the array do you expect width[i - 1] to access in the first loop iteration where i is 0? It seems to me your program has undefined behavior whether or not you are adding the commented part at the end. Commented May 1, 2022 at 13:01
  • Yeah, but when I typed 'width[i]' width[0] not assigned, I think reason of this is 'getline(input, str)' part, I check the index of the width its fine Commented May 1, 2022 at 13:06

1 Answer 1

0

Your code should be actually fine. Please show us the whole program.

florian@florian-desktop:~$ cat -n test2.cpp
     1  #include <iostream>
     2  int main()
     3  {
     4  const int length = 4;
     5  short *width = NULL;
     6  width = new short[length];
     7  delete[] width;
     8  }
florian@florian-desktop:~$ valgrind ./a.out 
==22630== Memcheck, a memory error detector
==22630== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==22630== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==22630== Command: ./a.out
==22630== 
==22630== 
==22630== HEAP SUMMARY:
==22630==     in use at exit: 0 bytes in 0 blocks
==22630==   total heap usage: 2 allocs, 2 frees, 72,712 bytes allocated
==22630== 
==22630== All heap blocks were freed -- no leaks are possible
==22630== 
==22630== For lists of detected and suppressed errors, rerun with: -s
==22630== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Valgrind shows that all reserved memory is correctly released.

-- Updated:

I corrected your full code:

#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main() {
short **junction = NULL, *width = NULL, length = 12;
    ifstream input;

    input.open("sample_input.txt");
    if(!input)
        cout << "No such a file named \"sample_input.txt\"" << endl;
    else if(input >> length){
        junction = new short*[length];
        width = new short[length];
        string str; 
        for(int i = 0; i < length && getline(input, str); i++){
            istringstream ss(str);
            width[i] = (str.size() + 1) / 2; //numbers can be two char, so in this case, here we allocate more than necessary memory
            junction[i] = new short[width[i]];
            for(int j = 0; j < width[i]; j++){
                if(ss.eof()){
                    junction[i][j] = -1; // -1 is the key value of emtpy spaces
                    continue;
                }
                ss >> junction[i][j];
            }
        }
    }
    input.close();

    
    for(int i = 0; i < length; i++)
        delete[] junction[i];
    delete[] junction;
    delete[] width;
    

    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

just updated the code, what do you think now. I spend my hours to see anything bad in the code
11 2 4 5 1 5 6 1 5 8 1 3 4 3 7 10 6 11 4 9 8 10 6 9 7 this is sample data but system wrtie those on the same line
Your mean issue is that you messed up the indices. If you access width[i - 1] and i is 0, a memory error occurs because the first valid index in width is 0. I fixed your code now and it seems to behave fine.. At least, the memory accesses are correct now and all reserved memory is being free'd. Please always write the full code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.