Like Tree1Likes
  • 1 Post By Cat

Dynamic Memory Allocation

This is a discussion on Dynamic Memory Allocation within the C Programming forums, part of the General Programming Boards category; Hi, I have declared a global variable as pointer. The program performs certain number of iterations. After every iteration, the ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    16

    Dynamic Memory Allocation

    Hi,
    I have declared a global variable as pointer. The program performs certain number of iterations. After every iteration, the size of memory required for the pointer changes and this pointer variable is to be accessed by different functions. Now, here is my doubt:
    If I allocate the memory for this global variable in a function, will the contents of the memory be lost once I exit that function. In my opinion, it should not be the case as the dynamic memory allocation takes place in "heap" and should not be affected by the call of functions. Am I right??
    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by kwzeet View Post
    If I allocate the memory for this global variable in a function, will the contents of the memory be lost once I exit that function.
    No, it will not be lost - the memory remain valid until either (i) you call free() on the pointer that malloc() passed out to you or (ii) your program exits.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Also note, it's not "because" of being on the heap that memory allocated with malloc() remains allocated until you call free().

    C, in fact, doesn't define 'stack' versus 'heap' variables - or even require that a stack or a heap exist. It defines 'automatic' allocation versus 'dynamic' allocation (and the third being 'static' allocation). Stacks and heaps are implementation details - automatic/dynamic/static describe how the variables behave, which is all the standard specifies. How that behavior is implemented is something that you shouldn't need to know or care about. If you allocate the storage to be pointed at dynamically (e.g. via malloc), then it will persist until you free it. If you allocate the storage space automatically, it will persist until the automatic variable goes out of scope.

    Now, yes, automatic allocation is usually implemented as a stack, because it simply makes a lot of sense to do it that way - the properties of a stack allow the behavior of an automatic variable to be simply and efficiently implemented. Note, though, that even in implementations where most automatic variables end up on a stack, some might not - for example, some implementations may store C99 variable length arrays on the heap, but they must still behave as automatic variables - that is, they will be deallocated when they go out of scope.

    There's an important and often glossed-over distinction between the behavior and the implementation of that behavior. The behavior is guaranteed by standards, but the implementation is flexible.
    Adak likes this.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I gave your reply above, a "like", but truth is, it's a beautiful reply, Cat!

    Well done.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,282
    Note that alloca() allocates from what is usually the stack, but I haven't seen it used other than some old examples of code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me.. (Dynamic memory allocation )
    By Devil211272 in forum C Programming
    Replies: 11
    Last Post: 10-17-2012, 09:10 AM
  2. Dynamic memory Allocation
    By exclusive in forum C Programming
    Replies: 4
    Last Post: 11-10-2011, 06:16 AM
  3. Dynamic Memory Allocation
    By BoneXXX in forum C Programming
    Replies: 11
    Last Post: 03-26-2007, 01:43 AM
  4. Dynamic Memory Allocation
    By oranges in forum C++ Programming
    Replies: 3
    Last Post: 11-28-2006, 06:50 AM
  5. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21