Memory allocation and Var Scope

This is a discussion on Memory allocation and Var Scope within the C Programming forums, part of the General Programming Boards category; Can someone please verify for me that the following statement is true? I have been writing my code assuming this: ...

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

    Memory allocation and Var Scope

    Can someone please verify for me that the following statement is true? I have been writing my code assuming this:

    If I allocate memory for a local variable using malloc/realloc that when the variable goes out of scope - the memory associated with the variable is automatically freed?

    I also assume this is also true if I return a pointer from a function to memory alloc'd and when the variable outside the function goes out of scope that the memory will be automatically freed.

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    If I allocate memory for a local variable using malloc/realloc that when the variable goes out of scope - the memory associated with the variable is automatically freed?
    Nope, when you allocate memory with malloc and friends, it's not freed until you do it explicitly with free.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 04:06 PM
  2. Memory Allocation for a Structure
    By vsriharsha in forum C Programming
    Replies: 0
    Last Post: 06-06-2003, 09:11 AM

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