Thread: memory allocation

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    memory allocation

    Hello,

    i want to find some information about how the compiler allocates memory to char[] and char*at run time.

    obviously dynamic memory allocation occurs during malloc and free, but what about char[]s.

    is memory allocated at startup or during the call of a function (if its a function member)? similarly, is it freed at the end of the program or upon leaving a function.

    particularly, are char[]s more efficient than char*s? I know malloc and free take thier time.

    does anyone know the answer to these questions or know of a good resource? hammer, i know you have a good link somewhere!

    TIA, rotis23

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    http://www.eskimo.com/~scs/C-faq/top.html

    That's always a good read
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    thanks. i knew you would have one hammer!

    however, are char[]s better to use, in terms of efficiency, than char*s (if the size is known)?
    Last edited by rotis23; 10-02-2002 at 09:07 AM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by rotis23
    thanks. i knew you would have one hammer!

    however, are char[]s better to use, in terms of efficiency, than char*s (if the size is known)?
    Sure. If the size is known, and the size is never going to change, then it saves you a call to malloc and free.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    thanks

  6. #6
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    1) i want to find some information about how the compiler allocates memory to char[] and char*at run time.

    2) obviously dynamic memory allocation occurs during malloc and free, but what about char[]s.

    3) is memory allocated at startup or during the call of a function (if its a function member)? similarly, is it freed at the end of the program or upon leaving a function.

    4) particularly, are char[]s more efficient than char*s? I know malloc and free take thier time.

    5) does anyone know the answer to these questions or know of a good resource? hammer, i know you have a good link somewhere!
    Answers:
    ------------
    1) if the "char[]" is allocated globally it is created out of the codesegment (added at the end of it) and actually makes the code segment larger. Kind of wasteful. If allocated locally (like in a function), it is calculated as an offset by the linker at compile time and then allocated on the stack frame at execute time. Creating large global arrays this way is frowned up because it is not a dynamic allocation; prevents memory management of the heap; and presents the opportunity for code corruption if the array is overwritten.

    2) dynamically allocated RAM comes from the heap zone when malloc() is called to satisfy the request. dynamic memory may be moved by a memory manager in order to keep a heap organized and it does not increase the size of the executable, as does a globally allocated 'char[]' variable (see point 1).

    3) When and how memory is allocate/deallocated depends on the kind of memory it is. Any global variable is created at compile time. Any local variable is created during execute time, on the Stack Frame. When a program terminates, if a memory manager is present, it is not necessary to deallocate the RAM because the entire HeapZone is disposed of by the Memory Manager. Windows does not have this or do this, therefore it is important to deallocate everything manually, yourself.

    If you create a local variable of any kind, it is allocated on the stack. When the function or procedure exits, the stack is disposed of so that variable is deallocated. If the variable was created at compile time, globally, it is disposed of when the application exits. Remember, there is a difference between disposing of a pointer variable and the block of RAM the pointer variable points at. If it was dynamically allocated using malloc() or some derivitive, then it must be free()'d.

    4) Effeciency is a matter of what you're trying to accomplish. 'char[]' allocations are no more effecient than malloc() calls. The former sacrifices space, while the other sacrifices time. It is the perpetual trade-off in performance. You trade one or more resources of one kind in order to gain more of the one or more resources of value (at the moment). There is no such thing as a free ride.

    In truth, if you need to dynamically create ramblocks lots and lots of times (thousands, millions...etc), I would consider allocate the RAMBlock/s once and reusing it/them. Or at the beginning of execution so they are there when you need them.

    5) Yep, right here.
    It is not the spoon that bends, it is you who bends around the spoon.

  7. #7
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    For the compiler I use, writing code like this

    Code:
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
         char* s;
    
         s = malloc(30);
         strcpy(s, "hello, world");
          free(s);
         return 0;
    }
    The string "hello" is stored in the data
    section. Then memory is allocated then "hello" is
    copyied to the address s contains. So if you can just
    keep a pointer to "hello" then that is better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM