Thread: Memory allocation and Var Scope

  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, 03:06 PM
  2. Memory Allocation for a Structure
    By vsriharsha in forum C Programming
    Replies: 0
    Last Post: 06-06-2003, 08:11 AM