Thread: manualVSautomatic memory allocation.

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    106

    manualVSautomatic memory allocation.

    Hello, I was wandering when and how does the compiler do the memory allocation for custom types, so that would be possible to avoid the use of malloc() and free().

    Thanks for help.
    BrownB

  2. #2
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    Code:
    void
    my_func ( int n ) {
       char array[n]; // this array[] life time is that function scope and has n members allocated at runtime.
        ...
    }

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    106

    perhaps I should make an example...

    Let's have a struct type:
    Code:
    typedef struct{
        int a;
        int b;
        ....
    }MyStruct
    If I declare a variable of that type in my function as follows, will it be allocated in memory by the compiler?
    Code:
    void MyFunc(void){
        
        MyStruct s;
    
        s.a=1;
        s.b=2;
        ....
    }
    or the only way is to do a manual allocation:
    Code:
    void MyFunc(void){
        
        MyStruct *s;
    
        s=malloc(sizeof(MyStruct));
        
        s->a=1;
        s->b=2;
        ....
        free(s);
    }
    The question is:
    when does a compiler 'know' how to allocate a declared variable and when doesn't it know?

    I'm a PalmOS programmer, and for some system structs there is no need to use a pointer and manually allocate the memory, while for my structures I must do manually the work. I'd like to know how does the compiler work.
    Thanks.
    BrownB

  4. #4
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    int b; // memory of b exist in current scope
    int b[n]; // memory of b is allocated at runtime if n size couldn't be determined at compile time.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    Ok, I finally understand!!

    So, every structure can be allocated at compile time if it contains members which size can be known at compile time=>if the sizes are computed using constants.

    If a size depends from a runtime variable/value, the structure must be allocated at runtime.

    Is this right?
    Thanks a lot for help!
    BrownB

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by BrownB
    The question is:
    when does a compiler 'know' how to allocate a declared variable and when doesn't it know?
    It doesn't. If you statically declare an array or struct larger than the stack permits it, you will get an segmentation fault.

    Code:
    MyFunc:
     	pushl	%ebp
     	movl	%esp, %ebp
     	subl	$24, %esp
     	movl	$1, -8(%ebp)
     	movl	$2, -4(%ebp)
     	leal	-8(%ebp), %eax
     	movl	%eax, (%esp)
     	call	Unknownfunction
     	leave
     	ret
    As you can see, immediate 1 and 2 are assigned to locations on the stack, right behind our not-existing parameterlist. (Ignore the Unknownfunction, it's just to prevent GCC from wiping a function without in- or output).

    Code:
    MyFunc2:
     	pushl	%ebp
     	movl	%esp, %ebp
     	subl	$8, %esp
     	movl	$8, (%esp)
     	call	malloc
     	movl	$1, (%eax)
     	movl	$2, 4(%eax)
     	movl	%eax, (%esp)
     	call	free
     	movl	%ebp, %esp
     	popl	%ebp
     	ret
    Here of course the memory is allocated dynamically, eax contains the address of the malloc'd memory, movl moves the immediates into *eax and *(eax +1) (if eax were an int pointer).
    If you need to know more about how the stack itself is "allocated", have a look at intels x86 reference manual.

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