new and delete operators size element
-
Hi, I am curious about how c++ stores it's array size after a call to new x[]. does it store the array size, or an object count? If it is an object count then deleting an object recasted from what it was originally newed could cause an heap error right? eg, would the code... class objectx { // lots of things that make this an object much larger than bool! }; objectx* myObject = new (myObject)bool[20]; // do code delete[] myObject; ...cause a heap corruption as delete trys to delete 20 x objectx's? -- modified at 16:22 Friday 9th December, 2005
-
Hi, I am curious about how c++ stores it's array size after a call to new x[]. does it store the array size, or an object count? If it is an object count then deleting an object recasted from what it was originally newed could cause an heap error right? eg, would the code... class objectx { // lots of things that make this an object much larger than bool! }; objectx* myObject = new (myObject)bool[20]; // do code delete[] myObject; ...cause a heap corruption as delete trys to delete 20 x objectx's? -- modified at 16:22 Friday 9th December, 2005
lastgen wrote:
objectx* myObject = new (myObject)bool[20];
??? what are you trying to do there?
lastgen wrote:
delete[] myObject
the '[]' means this is an array of objects, so call their destructor (if there is one) before deallocating the memory. but the memory you allocated holds only bools, not myObjects. massive trouble awaits those who lie to the compiler about what lies on the other side of a pointer. Cleek | Image Toolkits | Thumbnail maker -- modified at 16:19 Friday 9th December, 2005
-
lastgen wrote:
objectx* myObject = new (myObject)bool[20];
??? what are you trying to do there?
lastgen wrote:
delete[] myObject
the '[]' means this is an array of objects, so call their destructor (if there is one) before deallocating the memory. but the memory you allocated holds only bools, not myObjects. massive trouble awaits those who lie to the compiler about what lies on the other side of a pointer. Cleek | Image Toolkits | Thumbnail maker -- modified at 16:19 Friday 9th December, 2005
I'm debugging code that was written by someone else. I'm fairly new to c++ but have been coding in other high level languages for years. I'm getting heap corruption but it's not occurring in previous version, but it seems to occur after leaving the scope of a function I call from the old code. One of the big changes is that 'objectx ' is now 4 times larger than it had previously been, although it had always been much larger than bool. I guessing that perhaps the call to 'delete[] myobject' would create code saying 'deallocate heap at myobject with a size of 20 * sizeof(myobject)' instead of the intended 'deallocate heap at myobject with a size of 20 * sizeof(bool)' and that was always causing heap corruption. This why the old version never completely worked, but it didn't cause many critical crashes. Now with the much larger object (an array of ~1000 in size of objects roughly 1k each!) its corrupting so much of the heap it killing the app. Is this likely to be correct? -- modified at 16:44 Friday 9th December, 2005
-
Hi, I am curious about how c++ stores it's array size after a call to new x[]. does it store the array size, or an object count? If it is an object count then deleting an object recasted from what it was originally newed could cause an heap error right? eg, would the code... class objectx { // lots of things that make this an object much larger than bool! }; objectx* myObject = new (myObject)bool[20]; // do code delete[] myObject; ...cause a heap corruption as delete trys to delete 20 x objectx's? -- modified at 16:22 Friday 9th December, 2005
-
I'm debugging code that was written by someone else. I'm fairly new to c++ but have been coding in other high level languages for years. I'm getting heap corruption but it's not occurring in previous version, but it seems to occur after leaving the scope of a function I call from the old code. One of the big changes is that 'objectx ' is now 4 times larger than it had previously been, although it had always been much larger than bool. I guessing that perhaps the call to 'delete[] myobject' would create code saying 'deallocate heap at myobject with a size of 20 * sizeof(myobject)' instead of the intended 'deallocate heap at myobject with a size of 20 * sizeof(bool)' and that was always causing heap corruption. This why the old version never completely worked, but it didn't cause many critical crashes. Now with the much larger object (an array of ~1000 in size of objects roughly 1k each!) its corrupting so much of the heap it killing the app. Is this likely to be correct? -- modified at 16:44 Friday 9th December, 2005
If you wish to allocate an instance of MyObject, then just say "MyObject *p = new MyObject". All this other stuff with bool has a terrible number of problems starting with the object won't be properly constructed. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
The reason why they were allocating this way was the object is a union of static and dynamically sized data. Although not the best code this wasn't causing the issue. After following further I found a function that was being called was shifting the object but not updating the new pointer correctly.