Thread: char ** Problem

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    char ** Problem

    Hello everyone,

    I have a problem, I have a char ** called "buffer". I want to have the 'buffer' point to a number of char *'s which store up to "bufsize" characters. I use malloc to allocate the space which gives me the code:

    nbufs = 8;
    bufsize = 32;
    char **buffer;
    buffer = malloc(nbufs);
    for(int i = 0; i<nbufs; i++){
    buffer[i] = malloc(bufsize+1);
    }
    First off... is this right?

    Now when I try to free the buffers I get an error.
    If i try to free the buffer like this...

    for(int i = 0; i<nbufs; i++){
    free(buffer[i]);
    }

    I get this error at compile time:

    *** glibc detected *** free(): invalid next size (fast): 0x0804a008 ***
    Aborted

    If I just ignore the for statement and try this...

    free(buffer);

    I get the error at compile time:

    *** glibc detected *** free(): invalid next size (fast): 0x0804a008 ***
    Aborted

    Is my char ** buffer not malloc'd right? What am I missing? Thank you ahead of time for your help.
    Last edited by parad0x; 11-27-2006 at 01:30 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    it should be
    Code:
    buffer = malloc(nbufs * sizeof( char *) );
    Kurt

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    First off... is this right?
    no it is not
    buffer[i] takes more then 1 byte, so the first malloc allocates not enough memory

    buffer = malloc(nbufs * sizeof (*buffer));

    read the FAQ about malloc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    2
    Thanks for the help!

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    use code tags. here is a sample code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        char **buf;
        int i;
            
        buf = malloc(5 * sizeof (*buf));
        
        for(i=0;i<5;i++)
            buf[i] = malloc(20);
            
        for(i=0;i<5;i++)
            strcpy(buf[i],"This is a test");
        
        for(i=0;i<5;i++)
            printf("%s\n",buf[i]);
        
        for(i=0;i<5;i++)
            free(buf[i]);
        
        getchar();
        return 0;
    }
    /* my output
    This is a test
    This is a test
    This is a test
    This is a test
    This is a test
    */
    ssharish2005

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for(i=0;i<5;i++)
            free(buf[i]);
    free( buf );
    Missed something.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  2. char problem
    By ForlornOdium in forum C++ Programming
    Replies: 10
    Last Post: 10-29-2003, 02:39 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM