free pointer to chars

This is a discussion on free pointer to chars within the C Programming forums, part of the General Programming Boards category; Hi all, I am having a problem I never had before when deallocating a pointer to char. I am not ...

  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
    22,312
    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.
    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
    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
    22,312
    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;
    }
    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. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 04:20 AM
  2. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 02: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, 02:27 PM

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