Thread: Freeing char pointers

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    31

    Freeing char pointers

    Hi I created a char* pointer and malloc a certain size to it. Then I pass this pointer to a function which fills up the memory allocated.

    Now i want to free the memory allocated but I keep getting some error. Heap corruption detected

    Here is what i did

    Code:
    char* buffer;
    buffer = (char*)malloc(100);
    fillupbuffer(&buffer);
    the freeing part i did this

    Code:
    free(buffer);
    and there comes the error.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you need to pass a char** to fillupbuffer when you have already allocated memory for the buffer? Normally, if you pass a char**, it means that fillupbuffer itself will be doing a malloc. I suggest that you show us the declaration of fillupbuffer. Also, check that you #include <stdlib.h> because the unnecessary cast of the return value of malloc would mask an error associated with failing to include that header.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    31
    oh the fillupbuffer function basically looks like this

    Code:
    freebuffer(char** buffer)
    {
    uint16_t a= 1234;
    memcpy(*buffer,a,sizeof(uint16_t));
    }
    If I do the malloc inside the fill buffer function, how do I free it outside the function?

    Yes i included stdlib.h already.

    Sorry for the noobness

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Edelweiss
    If I do the malloc inside the fill buffer function, how do I free it outside the function?
    Since you don't need malloc there, just pass a char*.

    Quote Originally Posted by Edelweiss
    oh the fillupbuffer function basically looks like this
    It seems you renamed it
    You probably want to pass the address of a, e.g.,
    Code:
    void fillupbuffer(char *buffer)
    {
        uint16_t a = 1234;
        memcpy(buffer, &a, sizeof(a));
    }
    Of course, now fillupbuffer(&buffer) should be fillupbuffer(buffer). Incidentally, are you trying to fill the buffer with "1234" instead?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    31
    Oh hahaha sorry for the renaming, morning blues.

    But if i declare a char* and allocate memory and pass the address of that pointer to a function, what advantages does it has?

    Compared the above to declaring a char* and pass address of that pointer to function and allocating memory in that function,

    and compared to just passing the address in.

    If I have 2 memcpy actions, in which both copies to different memory addresses in the buffer, which of the above will be better?

    Oh i actually have a lot of information to memcpy to the buffer, but its the concept which I am confused with.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Edelweiss
    But if i declare a char* and allocate memory and pass the address of that pointer to a function, what advantages does it has?

    Compared the above to declaring a char* and pass address of that pointer to function and allocating memory in that function,
    If you take my suggestion of just having a char* parameter, your function is then more reusable: you can use it even if buffer is an actual array instead of a pointer. It is also typically easier to work with fewer levels of indirection. If you use a char** parameter and malloc from within the function, you are then constrained with working with just such a dynamic array.

    Quote Originally Posted by Edelweiss
    If I have 2 memcpy actions, in which both copies to different memory addresses in the buffer, which of the above will be better?

    Oh i actually have a lot of information to memcpy to the buffer, but its the concept which I am confused with.
    This does not affect the choice.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointers in C
    By Rishi Kumar in forum C Programming
    Replies: 22
    Last Post: 05-23-2008, 03:07 AM
  2. int & char pointers
    By cjohnman in forum C Programming
    Replies: 7
    Last Post: 05-02-2008, 01:46 PM
  3. Freeing a char**
    By +Azazel+ in forum C Programming
    Replies: 3
    Last Post: 02-26-2008, 07:27 PM
  4. vector / linkedlists of pointers, freeing memory
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 10-04-2006, 09:45 AM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 12:53 PM