Thread: malloc (more)

  1. #1
    Unregistered
    Guest

    malloc (more)

    ok.guys thanks for the help on malloc but does a stack work like this...



    ps. does the stack work like this, the amount of memory needed in main is places on the stack then when we go to another function main memory is pushed off the top and the memory for the new function is poped on the bottom then freed when we go to another function and all the memory used locally is freed automatically when a program terminates.

    thanks again, a learner(wanting to be a master).

    also
    If a heap is'nt any free space on the hard drive what is it.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    A C program uses four distinct regions of memory.The first region is the memory that holds the programs executable code. The next region is memory where global variables are stored. The remaining two regions are the stack and the heap. The stack is used for a great many things while your program exectues. It holds the return address of function calls, arguments to functions, and local variables. It will also save the current state of the CPU. The heap is a region of free memory that your program can use via dynamic memory allocation.

    All non dynamic variables are allocated memory from the stack at the beginning of execution to the end of execution, even if they only appear once in one function, their memory is reserved for the entire program.
    Last edited by SPOOK; 10-14-2001 at 04:41 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > their memory is reserved for the entire program.
    The stack exists for the duration, but all local variables only exist while the function is in scope. As soon as the scope exits, the variables in that scope all cease to exist.

    This is why you cannot return a pointer to a local variable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    Their scope may only be local to a particular function or block of code but their memory is reserved from the begining of execution, therefore by declaring variables only where and when you need them doesn't save you any memory.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > but their memory is reserved from the begining of execution,
    By your argument, this code would reserve 2000 bytes of stack.

    Code:
    void foo ( void ) {
        char buff[1000];
    }
    void bar ( void ) {
        char buff[1000];
    }
    int main ( ) {
        foo();
        bar();
        return 0;
    }
    When foo() exits, the stack space used by foo() is reclaimed and re-used by bar().
    The result is that only 1000 bytes of stack space is needed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    MMMM
    Looks like i gotta eat some humble pie Salem. I did some searching and just found this:

    each application uses an area of memory called "the stack", which holds all local variables and parameters used by any function. The stack also remembers the order in which functions are called so that function returns occur correctly. Each time a function is called, its local variables and parameters are "pushed onto" the stack. When the function returns, these locals and parameters are "popped". Because of this, the size of a program's stack fluctuates constantly as the program is running but it has some maximum size.

    Above text located at
    http://www.howstuffworks.com/c11.htm

    Thanks for getting me thinking or who knows when I'd have found out. Now I dont have to be so Stingy with my variables.
    Cheers mate.

  7. #7
    Unregistered
    Guest
    > Each time a function is called, its local variables and parameters
    > are "pushed onto" the stack.

    Which is why it's called a 'stack', and it is also why they have
    'stacks and queues' lessons. *grin* Stack = filo, Queue = fifo.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM