Try checking memory allocation and deallocation:
if you alloc with malloc() use free()
if you alloc with new use delete
if you alloc with new[] use delete[] <- (VERY important because if you alloc with new[] and want to use delete the state is UNDEFINED, you have to use delete[] to make everything work fine)
--------------------------------------------------------
"If it looks good, it is good computer graphics"
"If it looks like computer graphics, it is bad computer graphics"
Corrail
corrail@gmx.at
ICQ#59184081
Program crashing
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
So if I allocate a float arrray, of size 200 i should do this to allocate it (which i already do):
float *a;
a=new float[200];
But which one to deallocate ?:
delete a;
OR
delete a[];
?
Thanks in advance.
float *a;
a=new float[200];
But which one to deallocate ?:
delete a;
OR
delete a[];
?
Thanks in advance.
- growl -
You should deallocate with
delete[] a;
delete[] is a seperate function just as delete but for arrays.
delete a[]; won't work at all. I don't think that this is valid.
delete a; Isn't valid too because the state is undefined.
--------------------------------------------------------
"If it looks good, it is good computer graphics"
"If it looks like computer graphics, it is bad computer graphics"
Corrail
corrail@gmx.at
ICQ#59184081
[edited by - Corrail on December 16, 2003 9:12:14 AM]
delete[] a;
delete[] is a seperate function just as delete but for arrays.
delete a[]; won't work at all. I don't think that this is valid.
delete a; Isn't valid too because the state is undefined.
--------------------------------------------------------
"If it looks good, it is good computer graphics"
"If it looks like computer graphics, it is bad computer graphics"
Corrail
corrail@gmx.at
ICQ#59184081
[edited by - Corrail on December 16, 2003 9:12:14 AM]
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
Ok thanks, i''ll try as soon as I reach a computer with c++ on it
.

- growl -
quote:
Original post by Living Monstrosity
well, with this code:
if(p){ free(p);};// OR when using newif(p){ delete p;};
i get access violation, so does that mean that the array is allocated, but not really there?
pointer * p = new pointer;
delete p;
after that, the statement if(p) will still execute! Make sure you add p = NULL; after the delete.
quote:
Original post by Corrail
You should deallocate with
delete[] a;
delete[] is a seperate function just as delete but for arrays.
delete a[]; won''t work at all. I don''t think that this is valid.
delete a; Isn''t valid too because the state is undefined.
While we''re on the subject, could someone give me an example where deleting an array MUST be done with delete[]?
I have the bad habbit of never, ever using delete[], and off the top of my head I can''t think of a time when it''s caused problems for me.
In my current project I''m allocating and dealocating lots of large arrays on the fly and I''m confident that my memory is being correctly freed. (It would run out of memory in seconds if the memory weren''t being freed properly.)
Does delete[] as opposed to delete only become important if the constructors or destructors of the array elements do something unusual?
I feel stupid asking such a basic question, but every time I read about this I never quite understand why it works this way. Thanks.
-Andy
when using an array you always have to use []
but could we plz go back to my problem now?
but could we plz go back to my problem now?

quote:
Original post by Ruudje
when using an array you always have to use []
Hmmmm... My compiler must realise how cool I am and fix it for me, then.
quote:
but could we plz go back to my problem now?![]()
Is it possible that some other part of your program holds a pointer to the data structure that''s being deleted and then reallocated?
For instance if you had a "renderer" object with a pointer to some vertexes, it would crash during the next frame.
(About your object parser, you''re intentionaly throwing away the first token, right? And the LAST token on each line is .ID?)
Two other ideas occured to me that are probably not important at all unless you''re multi-threading, but I thought they were worth mentioning.
1) How would your program react if your renderer were called in the middle of this load operation?
2) strtok(...) isn''t thread-safe.
(P.S. Don''t forget to glDeleteLists.)
-Andy
December 17, 2003 03:48 PM
quote:
Hmmmm... My compiler must realise how cool I am and fix it for me, then.
Your compiler may be fixing it, but you can not rely on the compiler to do these things. You are probably just getting lucky with you compiler/machine set up. If you ran your code on enough computers, it most likely will cause problems on one of them. Calling delete on an item that was created with new[] is an UNDEFINED operation. That does not mean you can''t go right ahead and do it, it just means that you are can''t be sure what will happen. If you just use delete[] like you should, then you won''t have to worry.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement