scope of memory allocation

This is a discussion on scope of memory allocation within the C++ Programming forums, part of the General Programming Boards category; memory allocation is automatically freed when the scope in which it was allocated ends? seems to be the case, since ...

  1. #1
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459

    scope of memory allocation

    memory allocation is automatically freed when the scope in which it was allocated ends? seems to be the case, since if i use another function, lets call it f_x (char *buffer, int size);, to allocate on buffer for size, i get garbage on the exit of f_x if i redisplay the buffer...

    Code:
    //  choppy example
    
    void f_x (char *buffer, int size);
    void free_x (char *buffer);
    
    int main (void)
    {
      char *buf;
    
      f_x (buf, whatever_size);
    
      //  here data is not okay...
    
      free_x (buf);
    }
    
    void f_x (char *buffer, int size)
    {
      if((buffer = calloc (size, 1)) == NULL) //bomb out...
    
      // read something into buffer from a file
    
      //  here data is okay
    }
    
    void free_x (char *buffer)
    {
      free (buffer);
    }
    anyways, something like that... i figure that it works against my assumption since Xalloc has to return a void pointer and have casting/reassignment to the new pointer... [like in my usage]... i want to do this because i want to have a function that opens a file, allocs a buffer, reads the file into that buffer, and closes the file... and one that opens a file, writes the buffer to the file, frees the buffer, and closes the file... all in one handy reusable call...

    [but the data is altered in between the file loading and unloading] so i need the file size to be returned [and saved], which is the return value of my load/unload functions. my intermediate functions alter the data and a running count of the buffer size is needed... i could use a structure for file-length and buffer and have it be returned in my load/unload routines, but i wanted to check with you guys for alternatives just in case. thank you...
    hasafraggin shizigishin oppashigger...

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I think you need a double pointer. Otherwise the allocated pointer is lost when you return from the function.

    // choppy example

    void f_x (char **buffer, int size);
    void free_x (char **buffer);

    int main (void)
    {
    char *buf;

    f_x (&buf, whatever_size);

    // here data is not okay...

    free_x (&buf);
    }

    void f_x (char **buffer, int size)
    {
    if((*buffer = calloc (size, 1)) == NULL) //bomb out...

    // read something into buffer from a file

    // here data is okay
    }

    void free_x (char **buffer)
    {
    free (*buffer);
    }
    Last edited by swoopy; 02-16-2002 at 01:52 AM.

  3. #3
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    thank you very much.
    hasafraggin shizigishin oppashigger...

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    By the way, I like the description "choppy example". I might have to use that one sometime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  3. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  4. Varibale scope and memory allocation
    By Guang Yan Wang in forum C Programming
    Replies: 2
    Last Post: 02-25-2004, 09:16 AM
  5. Scope of memory allocation
    By Xzyx987X in forum C Programming
    Replies: 7
    Last Post: 01-11-2004, 06:35 PM

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