dynamic memory

This is a discussion on dynamic memory within the C++ Programming forums, part of the General Programming Boards category; Is dynamic memory only used for arrays? I understand that if you don't have a fixed size for your array, ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Question dynamic memory

    Is dynamic memory only used for arrays? I understand that if you don't have a fixed size for your array, this is to be used. But does it have any other functions, i.e. just variables?
    Thanks..

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    Yes! If the memory you're trying to use is large (or a lot of it), then you use dynamic memory since the stack is limited to about 1 MB (or less on embedded systems), but the heap is limited to 2 GB (on Windows; may vary on other platforms, max 4 GB for 32-bit systems).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Yes! If the memory you're trying to use is large (or a lot of it), then you use dynamic memory since the stack is limited to about 1 MB (or less on embedded systems), but the heap is limited to 2 GB (on Windows; may vary on other platforms, max 4 GB for 32-bit systems).
    So, is that a yes, it is only used for arrays or yes, it can also be used in variables?
    Thanks, Elysia..

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    As I explained, if the memory you need is very big, then you use dynamic memory. Dynamic memory is not limited to arrays, it can be used for any type of variables.
    Dynamic memory is also used when you need to create memory that outlives the length of a function or when you dynamically need to create a variable or object.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    I am just starting to read about dynamic memory and still familiarizing myself with the facts. Thanks, Elysia..

  6. #6
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    maybe the hardest thing about dynamic memory is that you have to keep track of the pointers so you would have "memory leak". That's why "large" program projects use profilers
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    Uhh no, profilers don't catch memory leaks (well, some might, but that is not the purpose of a profiler). They catch parts that needs to be optimized.
    Dynamic memory isn't hard at all. You just need smart pointers. Classes that delete memory when no longer in use. C++ is so powerful, yet so underutilized by many.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You might also use dynamic memory when the lifetime of the variable you are creating needs to be longer than the function you create it in. One example is a Factory function that creates a variable of the correct kind of type depending on some input it gets. The function cannot create a local variable because that will die when the function ends, so it will create an instance using dynamic memory that will continue to exist when the function ends.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    The new & delete operators have single object & array of object versions, so you can use it for 1 object or for x objects. Ex.
    Code:
    int* pInt = new int;  // Allocate 1 new int.
    char* str = new char[256];  // Allocate an array of 256 new chars.
    delete pInt;  // Delete the single int.
    delete [] str;  // Delete the array of chars.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 09:57 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 03:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 09:52 AM

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