Archived

This topic is now archived and is closed to further replies.

PSioNiC

Dynamic arrays giving me troubles

Recommended Posts

Tis is probably a REALLY easy question, but its 2:00 AM and I refuse to goto bed (you all being coders, im sure understand ) I have this (not exactly, but you get the point):

class MYCLASS {
  public:
    MYCLASS2  *MyClass2;
    void Init();
};

void MYCLASS::Init() {

  MyClass2 = new MYCLASS2[SomeNumber];

  ...

  delete MyClass2;
}

  
then elsewhere their is:

void APPLICATION::Init() {
 
  MYCLASS Class1, Class2;
  Class1.Init();
  Class2.Init();
}

  
After i call the init() method of Class1, its al working fine. But when i call the init of the second one, it craps out. i made it so that class2.init was called first and same. i've also been deleting all my pointers like a good coder too. again its very late for me and i know its something stupid but bare with me. any help would be good. Edited by - Psionic on November 10, 2001 11:09:37 AM

Share this post


Link to post
Share on other sites
No it is not the same. If you look up this very topic in Bjarne''s book, he explicitly warns about such thing as delete will not properly deallocate an array allocated with new[]. Now, it might just so happen that on a particular platform it works, but never make that assumption. Assumptions like that are why most software out there is so darned buggy.

Instead of dynamically creating and destroying an array--forcing you to have to worry about such issues--consider using vector from the Standard Template Library. All access is guaranteed to be O(1) which means that you get exactly the same performance as with "normal" arrays, yet you can access them in the same way. Furthermore, you can resize them to your heart''s content and they automatically deallocate themselves, so there''s no worry of memory leaks.

Use vector.

Share this post


Link to post
Share on other sites
quote:
Original post by Invader X
Actually deleting an array using delete array or delete [] array is generally the same.

Actually the difference is that the first will not call destructors for the array elements while the second will. In any case, as merlin9x9 suggested you should really try vector. I use the STL for everything now.



I wanna work for Microsoft!

Share this post


Link to post
Share on other sites
here is some code that will allow for better understanding:



class Grid {
public:
VERTEX *VtcGrid;

void CreateGrid(int NumVertices) {
VtcGrid = new V3DVERTEX[NumVertices];

for (int i = 0; i <= NumVertices; i++)
VtcGrid.x = VtcGrid[i].y = VtcGrid[i].z = 0;
delete[] VtcGrid;
}

};

void APPLICATION::Start() {

Grid Terrain, Sky;
Terrain.CreateGrid(64);
Sky.CreateGridEx(64);
}



on thie first .CreateGrid call everything works, but on the second (Sky.CreateGrid), I get a heap error. I can't allocate the memory or something like that.


Edited by - Psionic on November 10, 2001 12:49:18 PM

Share this post


Link to post
Share on other sites
Exactly what is CreateGrid supposed to be doing? the way it is written is that it allocates memory, zeros it out, and then releases them memory -- so basically its done nothing!

You are exceeding the array bounds by one:
for (int i = 0; i <= NumVertices; i++)

should be
for (int i = 0; i < NumVertices; i++)

Also if you change it so that VtcGrid is not deleted, the vertex pointer could leave some dangling memory. I suggest in the constructor you add:

VtcGrid=NULL;

and at the top of all your create functions add

if (VtcGrid != NULL)
delete [] VtcGrid;
VtcGrid = NULL;

This keeps you from losing memory if you do a double create. If all that doesn't work, something may be wrong with CreateGridEx.



Edited by - invective on November 10, 2001 1:48:05 PM

Share this post


Link to post
Share on other sites
its creating a grid that will be able to use for everything from height fields to walls to sprites.
i cropped out the code that didn't matter.


i tried making VtcGrid a local varible in the method and it still didn't work

Edited by - Psionic on November 10, 2001 2:47:08 PM

Share this post


Link to post
Share on other sites