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. Allocation/Deallocation from Memory Pool [Solved]

Allocation/Deallocation from Memory Pool [Solved]

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
4 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.
  • S Offline
    S Offline
    Skippums
    wrote on last edited by
    #1

    I am wondering if the type passed to delete[] needs to be the same as the type allocated by new? I am attempting to do the following:

    void** memPoolPtrs = new void* [4 * (1 + 12)];
    double* memPoolData = new double[4 * 12 * 30];
    double*** myData = reinterpret_cast<double***>(memPoolPtrs);
    memPoolPtrs += 4;
    for (size_t idx0 = 0; idx0 < 4; ++idx0) {
    myData[idx0] = reinterpret_cast<double**>(memPoolPtrs);
    memPoolPtrs += 12;
    for (size_t idx1 = 0; idx1 < 12; ++idx1) {
    myData[idx0][idx1] = memPoolData;
    memPoolData += 30;
    }
    }

    // Do some stuff with myData

    delete[] **myData; // Delete ALL dynamically allocated doubles
    delete[] myData; // Delete ALL dynamically allocated pointers

    Is there any risk in the above approach (other than allocating things of improper size)? I can't think of a reason why such a thing wouldn't work, but I am curious if the two delete statements will work as I anticipate. Thanks,

    Sounds like somebody's got a case of the Mondays -Jeff

    modified on Friday, April 16, 2010 12:41 PM

    _ C G 3 Replies Last reply
    0
    • S Skippums

      I am wondering if the type passed to delete[] needs to be the same as the type allocated by new? I am attempting to do the following:

      void** memPoolPtrs = new void* [4 * (1 + 12)];
      double* memPoolData = new double[4 * 12 * 30];
      double*** myData = reinterpret_cast<double***>(memPoolPtrs);
      memPoolPtrs += 4;
      for (size_t idx0 = 0; idx0 < 4; ++idx0) {
      myData[idx0] = reinterpret_cast<double**>(memPoolPtrs);
      memPoolPtrs += 12;
      for (size_t idx1 = 0; idx1 < 12; ++idx1) {
      myData[idx0][idx1] = memPoolData;
      memPoolData += 30;
      }
      }

      // Do some stuff with myData

      delete[] **myData; // Delete ALL dynamically allocated doubles
      delete[] myData; // Delete ALL dynamically allocated pointers

      Is there any risk in the above approach (other than allocating things of improper size)? I can't think of a reason why such a thing wouldn't work, but I am curious if the two delete statements will work as I anticipate. Thanks,

      Sounds like somebody's got a case of the Mondays -Jeff

      modified on Friday, April 16, 2010 12:41 PM

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Since the delete operator takes a void* type, it doesn't matter what type is passed for deletion as long as it is the starting address of the allocation.

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      1 Reply Last reply
      0
      • S Skippums

        I am wondering if the type passed to delete[] needs to be the same as the type allocated by new? I am attempting to do the following:

        void** memPoolPtrs = new void* [4 * (1 + 12)];
        double* memPoolData = new double[4 * 12 * 30];
        double*** myData = reinterpret_cast<double***>(memPoolPtrs);
        memPoolPtrs += 4;
        for (size_t idx0 = 0; idx0 < 4; ++idx0) {
        myData[idx0] = reinterpret_cast<double**>(memPoolPtrs);
        memPoolPtrs += 12;
        for (size_t idx1 = 0; idx1 < 12; ++idx1) {
        myData[idx0][idx1] = memPoolData;
        memPoolData += 30;
        }
        }

        // Do some stuff with myData

        delete[] **myData; // Delete ALL dynamically allocated doubles
        delete[] myData; // Delete ALL dynamically allocated pointers

        Is there any risk in the above approach (other than allocating things of improper size)? I can't think of a reason why such a thing wouldn't work, but I am curious if the two delete statements will work as I anticipate. Thanks,

        Sounds like somebody's got a case of the Mondays -Jeff

        modified on Friday, April 16, 2010 12:41 PM

        C Offline
        C Offline
        cmk
        wrote on last edited by
        #3

        For yor case it works. As a general rule it won't. delete[] against an object, whose type has a destructor will call the 'vector deleting destructor', which will call the destructor for each element. The problem is the number of elements. When new[] is called on a type with a destructor: 1. malloc a block large enough to hold the reqested # of elements, the actual block size will be rounded up to some grain size. 2. store the actual block size (bytes) with the block for free(). 3. store the requested # of elements with the block so delete[] can iterate over them and call ~(). When you call delete on a different (sized) type the # of elements stored with the block will be wrong and you will either call ~() on too few or too many elements. If too few then you could have a memory leak if the destructor frees resources. If too many then you corrupt the heap.

        ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

        1 Reply Last reply
        0
        • S Skippums

          I am wondering if the type passed to delete[] needs to be the same as the type allocated by new? I am attempting to do the following:

          void** memPoolPtrs = new void* [4 * (1 + 12)];
          double* memPoolData = new double[4 * 12 * 30];
          double*** myData = reinterpret_cast<double***>(memPoolPtrs);
          memPoolPtrs += 4;
          for (size_t idx0 = 0; idx0 < 4; ++idx0) {
          myData[idx0] = reinterpret_cast<double**>(memPoolPtrs);
          memPoolPtrs += 12;
          for (size_t idx1 = 0; idx1 < 12; ++idx1) {
          myData[idx0][idx1] = memPoolData;
          memPoolData += 30;
          }
          }

          // Do some stuff with myData

          delete[] **myData; // Delete ALL dynamically allocated doubles
          delete[] myData; // Delete ALL dynamically allocated pointers

          Is there any risk in the above approach (other than allocating things of improper size)? I can't think of a reason why such a thing wouldn't work, but I am curious if the two delete statements will work as I anticipate. Thanks,

          Sounds like somebody's got a case of the Mondays -Jeff

          modified on Friday, April 16, 2010 12:41 PM

          G Offline
          G Offline
          Gwenio
          wrote on last edited by
          #4

          Yes if the destructor is not virtual, no if it is. Delete will take any type, but it will only call the appropriate destructor if it is virtual or if you pass it as the correct type.

          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