Freeing dynamic memory

This is a discussion on Freeing dynamic memory within the C++ Programming forums, part of the General Programming Boards category; Ok so what im going to attempt to do is extend dynamic memory by creating a new slot of dynamic ...

  1. #1
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96

    Question Freeing dynamic memory

    Ok so what im going to attempt to do is extend dynamic memory by creating a new slot of dynamic memory, copy the data over so there identical (well except for the new unused space) and then swap the pointers over so the old pointer has the new memory address and the new(temporery) pointer points to the old slot

    The question here is when I free the memory allocated to the temp pointer will it free up too much space now that the pointer has been swaped to a smaller array? or will it automaticly detect how big the array is?

    cheers Matty

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It will be automatically detected and free the correct amount.

    What you are doing has already been implemented by realloc().
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Which pointer originally pointed to the first element of the dynamic array does not matter; the size of the array will be accounted for whether you use delete[] (or free(), as the case may be) on one pointer or the other, so long as you get it right.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    Quote Originally Posted by Dino View Post
    What you are doing has already been implemented by realloc().
    Oh ok I was under the impression that realloc() only shortend but not extended memory, is there a C++ equivilent of realloc()?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Matty_Alan
    is there a C++ equivilent of realloc()?
    std::realloc()

    But no, there is no equivalent in the same vein as new[] and delete[]. Just use a std::vector, or some other appropriate container.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    LOL I was afraid that was going to sound like a stupid question, but you knew what i ment

    thanx 4 the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Why use Dynamic Memory
    By appleGuy in forum C++ Programming
    Replies: 11
    Last Post: 08-28-2006, 02:46 PM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  5. Memory freeing after exec call?
    By Sethiorth in forum C Programming
    Replies: 4
    Last Post: 10-02-2004, 03:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21