Freeing char pointers

This is a discussion on Freeing char pointers within the C Programming forums, part of the General Programming Boards category; Hi I created a char* pointer and malloc a certain size to it. Then I pass this pointer to a ...

  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
    19,404
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    19,404
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    19,404
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 06: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, 11:53 AM

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