Thread: memory allocation!

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    17

    Lightbulb memory allocation!

    Hi All,
    Could somebody tell me
    1.) where memory is allocated for Global/static Global -->
    2.) where memory is allocated for Local/static Local -->
    3.) where memory is allocated for dynamic variables/malloc() -->

    thanks,
    cool

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    13
    i'm assuming that you are talking about the lifetime of the variable

    1) Global variables cannot be static, declareing a global variable as static is a self defeating purpose, because you never step outside the scope of the variable for it to be destroyed.

    global variables are always held in memory, and are not removed until the program ends. they can be accessed by every procedure

    2) Local variables are declared within a procedure, and they are only held in memory while the procedure is running, ie when a return statement is reached, or the final } is reached the variables are removed out of memory and the memory is free for other use

    static local variables are a bit different in that the memory space isnt released, and the value is held between procedure calls, so they sort of act like global variables except they cant be accessed outside of the scope of the procedure.

    3) Dynamic variables are set up at runtime because the size of the variable cant be determined, these variables are always useable until they are releced

    dynamic variables have to be refrenced as pointers, dont ever loose the pointer to a malloc address, or you will have memory leaks

    and all of the variables are held in ram

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    17

    GhOsT_DeStRoYeR for your info.

    Hi GhOsT_DeStRoYeR!
    What i wanted to know was in terms of stack and heap.

    For your knowledge Global variables can be static and its not a "self defeating purpose" as mentioned by you, in embedded programming they are a bone, where application always runs and never stops.

    As far as i know Local variables & function arguments are held in Stack ??????
    Dynamic allocation is held in heap ??????????

    What i am not sure of is about static variables i believe they are also held in Heap ??? pls correct me.

    cool

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    13
    i'de question local variables being stored in the stack, but i'm probly wrong.
    Dynamic allocation is definately held in the heap, or when the procedure ended, removing all local vars, the dynamic would be removed also, which is not the case.

    Function args are passed through the stack
    -push Calling address
    -push Parameters
    -Call Procedure

    then for a return statement
    -push Return Value
    -return to Calling address
    /*please correct me if i'm wrong, this is just my understanding from debuggers etc*/

    i havent dont any embedded programming before, know of anywhere i can read up on it??

  5. #5
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    cool pal,
    I believe between you and Salem I believe you've got your answers. Global / malloc's / and statics are heap. Local vars are definitely stack. By the way static globals are a beautiful thing as they allow for data encapsulation / hiding.

    I have a question related to all this. I know that with mallocs we are responsible for freeing the memory. However, doesn't the program automatically release memory associated with a non-malloc'd variable that goes out of scope? Therefore, wouldn't logic have it that even though you use malloc to allocate memory to a variable, wouldn't that memory become available again if the variable associated with the malloc'd memory goes out of scope?

    The problem with malloc using global variables is that the global variables never go out of scope so if you lose the pointer to the memory then you have a memory leak. Let me know if this is not correct and if it's not correct then I think that sucks about the malloc, that no matter what, you have to be responsible for freeing the memory even if the variable associated with the memory is going to go out of scope. How does the program know the difference between this type of heap memory allocation and any other for that matter? Also, is there a way to know how much heap memory is available because then I could perform my own experimentation with malloc's and scope?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >What i wanted to know was in terms of stack and heap.
    I try avoid "stack" and "heap" discussions since neither is required. So answering your question in terms of "stack" and "heap" is not entirely correct from the start. (I also try to avoid "global".)

    >/*please correct me if i'm wrong, this is just my understanding from debuggers etc*/
    Calling conventions vary.

    >i havent dont any embedded programming before, know of anywhere i can read up on it??
    http://www.embedded.com/links.htm#EMBEDDED
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    17

    penney,Dave_Sinkula

    Dave_Sinkula -->You are surely wrong in saying that neither is required.
    Everything i.e. Your C Program and OS reside in memory so memory need to used in a certain manner so it is arranged in a certain manner so that your program does not mess around with the OS , so various segments are defined to store your program.

    --> Text Segment -Contains all the instructions.It can also contain Data,
    -->but Usually data is Placed in Data Segment(only initialized data).
    -->Uninitialized data , local vaiables and function arguments are stored on Stack.
    -->Static variables can be stored in Data or Bss segment But most implementation of compilers store Static and global on Heap.

    Penney --> Your are correct in sayin that Global varaiables are a simple way of data hiding in C/No other source file can access them.

    And about your question regarding malloc() i think it is implemention specific , what i mean here is that one API is provided to allocate the memory(malloc()) and another is provided to free the memory (free()) . The people who implemented it could have done this way also that once you have called malloc() you don't need to call free the underlying layers on the complier take care of freeing the memory.

    I guess you can make a utility for your own use wherein you give the user only one API for allocating memory and keep track of if it is in use or not, if not in use you free it.:-)

    Penney--> you can find information about segments DS/BSS/Stack?heap i.e. their starting address and ending address in the .Map file which is created when you complie your program, if it is not created automatically then you will have to enable it from the options(in case of borland complier), in other compliers you can check.

    --->I have one more question Whenever i use malloc() in an ISR the Application crashes, any ideas????:-)

    cheers
    pal

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    44

    Re: penney,Dave_Sinkula

    Originally posted by coo_pal
    Dave_Sinkula -->You are surely wrong in saying that neither is required.
    Everything i.e. Your C Program and OS reside in memory so memory need to used in a certain manner so it is arranged in a certain manner so that your program does not mess around with the OS , so various segments are defined to store your program.

    --> Text Segment -Contains all the instructions.It can also contain Data,
    -->but Usually data is Placed in Data Segment(only initialized data).
    -->Uninitialized data , local vaiables and function arguments are stored on Stack.
    -->Static variables can be stored in Data or Bss segment But most implementation of compilers store Static and global on Heap.
    In one sense, you are right: the IMPLEMENTATION needs to worry about these things.

    The definition of the C standard does not require that kind of memory model at all - it is a very very flexible beast. There is no such thing as a 'heap' or a 'stack' as far as C is concerned. Your implementation may use such terms, but they're specific only to YOUR implementation and are nothing to do with the C language.

    Penney --> Your are correct in sayin that Global varaiables are a simple way of data hiding in C/No other source file can access them.
    Other modules can access them. You should take a look at 'extern' sometime.

    Global variables are very very rarely needed, and in general are an indicator that you're making things overcomplex.

    And about your question regarding malloc() i think it is implemention specific , what i mean here is that one API is provided to allocate the memory(malloc()) and another is provided to free the memory (free()) . The people who implemented it could have done this way also that once you have called malloc() you don't need to call free the underlying layers on the complier take care of freeing the memory.
    Indeed. The C standard actually states that the underlying system does not need to clean up and that your program should do so itself.

    If you're programming for only platforms which support such things (most Unix-like OSs and a handful of others) then not cleaning up after yourself is an option. If you're programming in C, then you should clean up.

    <snip>

    --->I have one more question Whenever i use malloc() in an ISR the Application crashes, any ideas????:-)

    cheers
    pal
    Probably because there are restrictions in what you're allowed to do in an ISR. Read the documentation for your platform/compiler on what restrictions it places on ISR usage. It's probably something internal and dodgy.

    Ian Woods

  9. #9
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47

    Comments and More Questions.

    Salem,

    Your example of malloc is exactly how I am using the function. I am returning a pointer to some allocated memory. However, I didn't really want the user of the function to have to worry about freeing that memory later on. I develop C programs on a AIX Unix platform if that makes a difference. Therefore if the user called the function like:

    char *str;

    str = func_call();

    Assuming func_call returns a pointer to allocated memory as your function showed. I'm surprised to find that when str goes out of scope that the memory does not become available for use again. In otherwords that the user of my function func_call() would then have to do a free(str) when they are done with it. By the way I do currently free any memory I allocate. However, I just had a question about whether it was really necessary in all situations.

    As far as my global data hiding comment. I simply stated that if you perform the following:

    static int somevar; within a file.

    That somevar will have internal linkage thereby no other file can see or access somevar - thereby creating the desired effect of data encapsulation / data hiding. Correct me if I'm wrong somehow because I love to learn.

    Lastly, when a program ends on a Unix AIX box does any unfreed memory become freed. I didn't write the code but I see where we are allocating and never freeing memory for some "global" linked lists when the program is done.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Originally posted by coo_pal
    Dave_Sinkula -->You are surely wrong in saying that neither is required.
    Everything i.e. Your C Program and OS reside in memory so memory need to used in a certain manner so it is arranged in a certain manner so that your program does not mess around with the OS , so various segments are defined to store your program.

    --> Text Segment -Contains all the instructions.It can also contain Data,
    -->but Usually data is Placed in Data Segment(only initialized data).
    -->Uninitialized data , local vaiables and function arguments are stored on Stack.
    -->Static variables can be stored in Data or Bss segment But most implementation of compilers store Static and global on Heap.
    This sounds like vaxocentrism (see also #10). It's not helpful for you to think of things in this way.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    17
    Hi Guys!
    I checked about the ISR(hardware interrupt i.e. external) problem, basically one can use malloc() in ISR only if he is very sure how exactly memory is allocated by Malloc() function, i am not sure of that so i would prefer to avoid it at this time, Coz have finish the thing in Time:-).

    If somebody can tell me in detail or some link which tells me how it does then will study it.

    There is one more problem that i am facing i am waiting for a flag to be set in my KEYPAD ISR now this flag is set by my SERIAL_RECIEVE ISR, but the KEYPAD always keeps waiting for the Flag for infinite time i have checked that the data comes on the Serial port with logic level analyzer, and the priority of SERIAL_RECIEVE ISR is higher than the KEYPAD ISR, i think this could be coz i need to enable Multiple interrupt handling i.e. SERIAL_RECIEVE ISR inside KEYPAD ISR. But still if anyone could throw light on this prob it will be of great help.

    cheers
    pal

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    17
    Its a bit Flag

    e.g.

    bit Flag ; /* only one bit :-) */


    i got the problem solved, it was multiple interrupt handling only.

    anyways thanks

    cheers
    pal

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