freeing allocated memory

This is a discussion on freeing allocated memory within the C Programming forums, part of the General Programming Boards category; Can I free something if it was declared like that: char str[100] ? Thanks....

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Arrow freeing allocated memory

    Can I free something if it was declared like that: char str[100] ?


    Thanks.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Nope. You can only free memory that is used by some pointer that was allocated by malloc (or a variant of it). char str[100] is declared on the stack, not on the heap, because the compiler knows the size of it at compile-time. It doesn't need freeing.

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    The heap and stack have always interested me, where can I read more on the subject?


    Thanks.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    The heap is a block of storage that a program can allocate and deallocate when needed.

    The system stack is a storage location for variables and function calls. Information is pushed on the stack when, for example, a function is called or variables are declared, and popped from the stack when the function exits.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    Lets say char str[100] is declared in a function so it has automatic storage duration:

    void funcExample(void)
    {
    int x;

    char str[100];

    ....(other things)

    return;
    }

    Each time funcExample is called, an "activation record" is created.
    An activation record is a data structure on the "stack" that holds the variables for an invocation of a function. The activation record holds a set of the variables of funcExample (x, and str);

    Storage is allocated for that str. On return from funcExample, the storage is deallocated.

    On the other hand, if char str[100] is declared outside of any function, str has static duration and external linkage. The array is allocated on the heap, but free cannot be used on it. It stays there until the program ends.

    Hope that helps.
    Last edited by voltson4; 06-29-2003 at 01:05 PM.

  6. #6
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Thanks...

    Thanks alot guys
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  5. Error when freeing memory
    By foniks munkee in forum C Programming
    Replies: 9
    Last Post: 03-01-2002, 07:28 PM

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