Thread: free pointer to chars

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    free pointer to chars

    Hi all,

    I am having a problem I never had before when deallocating a pointer to char. I am not sure what I am doing wrong because I am using the same standard I have used other times.

    I am declaring and allocating the following variable:

    Code:
    char   *problem[10];
    
    //Allocation of the strings of the above lines:
      for(i=0; i<10; i++)
        problem[i] = (char *) malloc(32 * sizeof(char *));
    and after it use, I free it by:

    Code:
      for(i=0; i<10; i++)
        free(problem[i]);
    but I get the following error:

    malloc: *** error for object 0x200000: pointer being freed was not allocated
    *** set a breakpoint in malloc_error_break to debug

    Could anyone help me with this?

    thank you in advance,
    cfd

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    problem[i] = (char *) malloc(32 * sizeof(char *));
    It should be:
    Code:
    problem[i] = malloc(32 * sizeof(char));
    or better yet:
    Code:
    problem[i] = malloc(32 * sizeof(problem[i][0]));
    or since sizeof(char) == 1:
    Code:
    problem[i] = malloc(32);
    though you probably should use a named constant instead of directly using 32.
    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
    Mar 2009
    Posts
    114
    Hi, thanks for replying, but although I change the allocation following your suggestion, I am still getting the error the the pointer I am freeing was not allocated to begin with.

    I don't know where the actual problem would be since I am not freeing it twice throughout the code.

    Thanks again

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Apologies,

    I was deallocating another array within another function. That is what was causing the problem.

    Thanks again for helping.
    cfd

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, this results in the error that you were talking about?
    Code:
    #include <stdlib.h>
    
    int main(void)
    {
        char *problem[10];
        int i;
    
        for (i = 0; i < 10; i++)
            problem[i] = malloc(32 * sizeof(problem[i][0]));
    
        for (i = 0; i < 10; i++)
            free(problem[i]);
    
        return 0;
    }
    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. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  2. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM