Dynamic arrays giving me troubles

Started by
8 comments, last by PSioNiC 22 years, 5 months ago
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
"This is stupid. I can't believe this! Ok, this time, there really IS a bug in the compiler."... 20 mins pass ..."I'M AN IDIOT!!!"
Advertisement
Actually the proper way to delete an array is delete [] array, not delete array. Not sure if that is the problem though.
Actually deleting an array using delete array or delete [] array is generally the same.

I believe the problem is you (at least with what you typed here) are not creating 2 MYCLASS''s but 2 MYCLASS2''s.

Invader X
Invader''s Realm
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.
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!
which ever it is, i tried both and none work
"This is stupid. I can't believe this! Ok, this time, there really IS a bug in the compiler."... 20 mins pass ..."I'M AN IDIOT!!!"
whoops screwed the code up

fixed now
"This is stupid. I can't believe this! Ok, this time, there really IS a bug in the compiler."... 20 mins pass ..."I'M AN IDIOT!!!"
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.y = VtcGrid.z = 0;<br>                delete[] VtcGrid;<br>	}<br><br>};<br><br>void APPLICATION::Start() {<br><br>	Grid Terrain, Sky;<br>	Terrain.CreateGrid(64);<br>	Sky.CreateGridEx(64);<br>}<br><br>  </pre>  <br><br>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.   </i> <br><br>Edited by - Psionic on November 10, 2001 12:49:18 PM    
"This is stupid. I can't believe this! Ok, this time, there really IS a bug in the compiler."... 20 mins pass ..."I'M AN IDIOT!!!"
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
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
"This is stupid. I can't believe this! Ok, this time, there really IS a bug in the compiler."... 20 mins pass ..."I'M AN IDIOT!!!"

This topic is closed to new replies.

Advertisement