Can someone clarify this comment.

This is a discussion on Can someone clarify this comment. within the C Programming forums, part of the General Programming Boards category; A person from England had posted the following code snippet. Code: unsigned char *loadbmp(const char *fname, int *width, int *height) ...

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    Can someone clarify this comment.

    A person from England had posted the following code snippet.

    Code:
    unsigned char *loadbmp(const char *fname, int *width, int *height)
    {
      FILE *fp;
      int err;
      unsigned char *answer;
      /* open file in binary mode */
       fp = fopen(fname, "rb");
       if(!fp)
         return 0;
    
      /* get all the information we need for the header */
      err = loadheader(fp, &width, &height);
    
      /*
        in this code we allocate memory for the raster, and load it.
        we have to be careful to clean up if we encounter error condtions.
      */
      if(err == 0)
      {
         answer = malloc(width * height * 3);
         if(answer)
            err = loadraster(fp, answer, width, height);
         if(err != 0)
         {
            free(answer);
            answer = 0;
         }
      }
      else
         answer = 0;
    
      fclose(fp);
    
    }
    Shortly after that, someone made the following response.

    "Having defined the function as returning an unsigned char* would not
    returning one be a good idea? Possibly return answer since otherwise you
    have lost the memory? "

    I tried asking both of these people, via email to clarify this comment. Neither one responded. So can someone here please elaborate on this comment.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    The function prototype is:

    Code:
    unsigned char *loadbmp(const char *fname, int *width, int *height);
    The "unsigned char *" part means that the function is defined to return a pointer to an unsigned char. As you can see, the person that wrote this function has a variable of this type named answer. The memory it points to, if any, is dynamically allocated by malloc(). Most likely, the intent of the programmer was to return answer right after closing the file fp.

    If memory is dynamically allocated in a function, and not returned and free()ed later or free()ed inside the function, then you have a memory leak, since all memory malloc()ed must also be free()ed. Since the memory in this particular function is not necessarily free()ed inside the function, and it's not returned either, if this compiled, you would lose the address of the block of memory that malloc() returned. That's obviously wrong to do and what the person responding was referring to.
    Last edited by MacGyver; 05-15-2007 at 06:49 PM.

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    And somewhere in the back of my head, my inner child is telling me

    "Psst...you missed a very subtle point about malloc() in the Second Edition of the book 'The C Programming Language' by Kernighan and Richtie."

    Okay, I'm babbling. Thanks for the response.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comment using // or /* &&& */
    By Roaring_Tiger in forum C Programming
    Replies: 3
    Last Post: 03-16-2005, 01:45 PM
  2. Tab->Space converter and comment colorizer
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-30-2005, 04:46 PM
  3. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  4. what is a pragma comment?
    By Shadow12345 in forum C++ Programming
    Replies: 9
    Last Post: 11-25-2002, 04:50 AM
  5. Comment on this socket code
    By mashley in forum C Programming
    Replies: 5
    Last Post: 10-30-2002, 07:31 PM

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