Thread: Freeing dynamic memory

  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
    Chappell Hill, Texas
    Posts
    2,332
    It will be automatically detected and free the correct amount.

    What you are doing has already been implemented by realloc().
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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