Thread: A possibly foolish question about dynamic allocation.

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    A possibly foolish question about dynamic allocation.

    I was reading some literature about some overheads incurred in dynamic memory allocation when the following question came to mind.

    What is the difference between how the following two blocks are compiled/allocated/executed..etc ?
    Code:
    while(true)
    {
        int x[100];
        for(auto y : x)
            //do something with the values 
    }
    AND
    Code:
    while(true)
    {
        int *x=new int[100];
        for(/*100 times*/)
            //do something with the values 
        delete[] x;
    }
    Last edited by manasij7479; 08-24-2011 at 03:20 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    The first snippet allocates the data on the stack, usually, and the second on the heap. Allocating memory on the stack is easy: simply subtract the size from the stack pointer; you automatically release it after each function by restoring the old pointer. However, stack space is usually limited in both size and lifetime, in the same way you can't return pointers to local variables.
    On the heap you can allocate and deallocate data whenever you wish. There's usually loads available, but allocating and deallocating is relatively slow.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    There are actually three places to allocate array: On the stack, on the heap, and on the data segment. Each have advantages and disadvantages.

    The stack Is used for most function local variables, including parameters and return values, and some additional meta data involved in calling functions. It is limited in size, and so holding large array can cause your program to "stack overflow", which is generally not a recoverable exception. Having deep recursion can also cause your stack to overflow.

    The data segment is used for local-static and global variables. It's size is determined readily by the sum of all your global and static variables rounded up to the kilobyte or so. You can allocate large array here, but you only get what you asked for: you can't create a global variable at runtime, nor can you remove variables you don't need.

    The heap is the additional memory that you can add to your program at any time using operations like new. The heap is allocated in large chunks, but operations like new will delegate small, reusable portions. Getting a block allocated can be slightly costly, but getting a portion of a previously allocated block isn't. The heap is limited by the other programs running, any memory quota restrictions by your operating system, and by the address space of your operating system (and processor). A program can generally ask for a lot of heap memory if it needs it, though memory space is not unlimited.

    Generally, speed is not the factor in deciding between these three. Speed is chiefly impacted by how near you memory accesses are to each other. To write fast memory intensive code, you want to try to put the frequently accessed data together.

    EDIT: I should mention that this only applies to modern operating system setups. Ancient operating systems and microcontrollers are different.
    Last edited by King Mir; 08-24-2011 at 06:18 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    The heap is the additional memory that you can add to your program at any time using operations like new. The heap is allocated in large chunks, but operations like new will delegate small, reusable portions. Getting a block allocated can be slightly costly, but getting a portion of a previously allocated block isn't. The heap is limited by the other programs running, any memory quota restrictions by your operating system, and by the address space of your operating system (and processor). A program can generally ask for a lot of heap memory if it needs it, though memory space is not unlimited.
    So, w.r.t the second snippet, the time for allocating an array of size 100 is always less than 100 times the time for allocating one variable ?
    Or are there better ways to access the remaining portion of the 'blocks' you mention?

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by manasij7479 View Post
    So, w.r.t the second snippet, the time for allocating an array of size 100 is always less than 100 times the time for allocating one variable ?
    Or are there better ways to access the remaining portion of the 'blocks' you mention?
    Well allocation one variable 100 times with new involves calling new 100 times. Allocating an array calls it once. The first of those 100 allocations will be as slow as allocating the array, the rest will be faster. But Again, this isn't where you optimize for speed.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. A question related to dynamic memory allocation
    By spiit231 in forum C Programming
    Replies: 2
    Last Post: 03-11-2008, 12:25 AM
  3. Foolish question
    By InvertedSaint in forum C Programming
    Replies: 4
    Last Post: 09-01-2007, 02:16 PM
  4. Dynamic Memory Allocation Question
    By Piknosh in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2004, 01:55 PM
  5. dynamic allocation question
    By vale in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 04:23 PM