Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. new and delete operators size element

new and delete operators size element

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lastgen
    wrote on last edited by
    #1

    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

    C S 2 Replies Last reply
    0
    • L lastgen

      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

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • C Chris Losinger

        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

        L Offline
        L Offline
        lastgen
        wrote on last edited by
        #3

        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

        T 1 Reply Last reply
        0
        • L lastgen

          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

          S Offline
          S Offline
          sun_shb
          wrote on last edited by
          #4

          objectx* myObject = new objectx[20]; delete[] myObject;

          L 1 Reply Last reply
          0
          • L lastgen

            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

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • S sun_shb

              objectx* myObject = new objectx[20]; delete[] myObject;

              L Offline
              L Offline
              lastgen
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups