Search code examples
c++arraysmemorystructallocation

Pointer to an array of structs


I am attempting to continuously free memory and allocate memory. I keep getting the error: access violation reading location 0xblahblahblah.

Here is essentially what I am doing:

Block *treeBlocks1;
treeBlocks1 = new Block[topology1.length()];
//I am able to use "treeBlocks1" as needed in my code to calculate cost

while(true)
{
    delete [] treeBlocks1;
    treeBlocks1 = new Block(newTopology.length());
    //And now I get an error when trying to calculate cost using treeBlocks1
}

What could be my problem?

Thanks for the help!


Solution

  • The answer was simple....changing the () to [].... duh.

    treeBlocks1 = new Block[newTopology.length()];