1

Crashes when the delete command comes around. It's supposed make a struct with pointers to arrays, fill them with random numbers, and then deallocate the memory. Works fine until the delete command, in which it crashes with our without the for loop or boolean check.

int main() {

    cout << "start" << endl;
    //Creating Struct
    struct 
    {
        int* ptrarray[10];
        bool boolarray[10];
    } data;

    //Initializing Random Generator
    srand ( time(NULL) );
    cout << "Initializing: ";

    //Allocating Memory and generating random numbers with for loops

    for (int i = 0; i < 10; i++)
    {   
        int counter = 0; //Counts numbers set   
        cout <<  i << " "; //Counting arrays initialized    
        data.ptrarray[i] = new int [12582912]; // Memory Allocation 

        for (int j = 0; j < 12582912; j++)//Number Generating
        {
            *data.ptrarray[i] = rand() % 899 + 100;
            data.ptrarray[i]++;
            counter++;
        }

        //Checking for failed initializations and declaring if success
        if (counter == 12582912)
        {
            data.boolarray[i] = true;
        }
        else
        {
            data.boolarray[i] = false;
        }
    }

    cout << endl;

    //This is where it always crashes.
    for (int i=0; i<10; i++)
    {
        if (data.boolarray[i] == true)
            delete[] data.ptrarray[i];
    }
    cout << endl;

    return 0;
}

I'm using MS Visual studio 2010.

1
  • 1
    This formatting is terrible. Please reformat and paste in gist. Commented Sep 19, 2012 at 21:59

3 Answers 3

2

The culprit is this line:

data.ptrarray[i]++;

You are changing the pointer, then using delete [] on the modified pointer. This will not work: you must use delete[] with the exact same pointer you got from new[].

Try

data.ptrarray[i][j] = rand() % 899 + 100;

instead so you don't have to change your pointer.

Sign up to request clarification or add additional context in comments.

5 Comments

Even though it's a bit slower. Unless the compiler optimizes that away.
Most modern compilers are smart enough to recognize an array-filling loop for(x=0; x<n; x++) arr[x] = f(x); and will create an appropriate temporary variable.
Thanks a lot (and to everyone who saw the mistake). Makes perfect sense now.
Well, okay. It deletes fine now. (I deleted data.ptrarray[i]++; and replaced *data.ptrarray[i] = rand() % 899 + 100; with data.ptrarray[i][j] = rand() % 899 + 100;) But now it doesn't print the proper range of integers, instead some wild numbers (negative, 0, way over 1000). Any ideas?
Okay, forget that, I had to change the print function in my extended code as well. Thanks again.
1

data.ptrarray[i]++; is the problem.

You're incrementing your reference to the data, but then not resetting it to the start before trying to delete it.

Comments

1

Your problem lies with:

data.ptrarray[i]++;

This is modifying the pointer. When you then attempt to free that modified pointer, it crashes.

You can solve this by using a copy of the pointer to run through the array, or by indexing with thej variable as well:

data.ptrarray[i][j]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.