Thread: Problem with malloc ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    Question Problem with malloc ...

    Hey guys !

    First thing first, i am sorry about this post about malloc ... but i have serious problem with it ... I am programming Sokoban Console Game and i need to load levels from file (levels.dat) and then read line by line and parse every line in some blocks ...
    Example of level : <name>;<password>;<description>;<map>
    chicago;addie;story begins here;-----#####-----------|-----#@$.#-----------|-----#####-----------
    I have the struct :
    Code:
    typedef struct level 
    {
    char * name;
    char * password;
    char * description;
    char * map;
    struct level * next;
    }
    My problem is, when i am tryin to allocate memory for map ...
    Do not read the comments , i am from slovakia ...
    Here is the code :
    Code:
    Level * load_levels(const char * path)
    {
        // docasne udaje
        Level *prvy, *posl, *novy;
        // nacitaj subor
        FILE * f = fopen(path, "r");
        // ak subor neexistuje tak ukonci
        if(!f) 
            return NULL;
        // nacitaj riadok
        int i = 0,j;
        // sem citam retazce
        char t[128][512];
        // citaj retazce
        while(fgets(t[i], 512, f) != NULL) 
            i++;
        
         // rozparsuj level
            prvy = NULL;
            prvy= parse_level(t[0]);
             prvy->next_level = NULL;
            posl = prvy;
            //pokial som nacital level nacitaj dalsi
            for(j = 1; j < i; j++)
            {
                novy = parse_level(t[j]);
                if(novy != NULL)
                {
                   novy->next_level = NULL;
                   //posl->next_level = novy;
                   posl = novy;    
                }
                else 
                    return NULL;
            }
          
         // zatvor subor
         fclose(f);   
            
        // nacital som levely 
        return (prvy);
         
    }
    
    
    Level * parse_level(char s[])
    {
        // pridel pamat
        Level * t  = (Level*)malloc(sizeof(Level)) ;
        // ak sa nepodarilo naalokovat pamat tak skoncime s NULL
        if(t == NULL) 
            return NULL;
        // docasne premenne
        int i;
        // alokuj potrebnu pamat
        char * buffer = malloc( sizeof *s + 1);
        
        // ak sa nenaalokoval buffer konci
        if(buffer == NULL) 
            return NULL;
        // kopiruj do bafra 
        strcpy(buffer , "");
        // hladaci algoritmus
        // hladaj meno
        for(i = 0; i < strlen(s); i++)
        {
            if(s[i] != ';')
            {
                char znak = s[i];
                strcat(buffer, &znak);
            }
            else break;
        }
        // ak som nenacital nic do buffra skonci
        if(!strcmp(buffer, "") || !strcmp(buffer, " ")) 
            return NULL;
        // pridel pamat 
        if((t->name) != NULL) free(t->name);
        t->name = malloc( sizeof *buffer + 1 );
        // ak sa pamat neda pridelit tak skonci
        if(t->name == NULL) 
            return NULL;
        // skopiruj obsah z buffra do name
        strcpy(t->name, buffer);
        // kopiruj do buffra
        strcpy(buffer , "");
        
        // hladaj heslo
        for(i++; i < strlen(s); i++)
        {
            if(s[i] != ';')
            {
                char znak = s[i];
                strcat(buffer, &znak);
            }
            else break;
        }
        // ak som nic nenasiel konci
         if(!strcmp(buffer, "")  || !strcmp(buffer, " ")) 
             return NULL;
        // pridel pamat
        if((t->password) != NULL) free(t->password);
        t->password = malloc( sizeof *buffer + 1  );
        // ak sa nepridelila pamat konci
        if(t->password == NULL)
            return NULL;
        // kopiruj obsah buffra
        strcpy(t->password, buffer);
        // kopiruj do buffra
        strcpy(buffer , "");
        
        // hladaj description
        for(i++; i < strlen(s); i++)
        {
            if(s[i] != ';')
            {
                char znak = s[i];
                strcat(buffer, &znak);
            }
            else break;
        }
        // ak sa v buffrinic nenachadya konci
        if(!strcmp(buffer, "")  || !strcmp(buffer, "'")) 
            return NULL;
        // vyhrad pamat
        if((t->description) != NULL) free(t->description);
        t->description = malloc( sizeof *buffer + 1 );
        // ak nemam nic v description
        if(t->description == NULL)
            return NULL;
        // kopirujobsah z buffra
        strcpy(t->description, buffer);
        // kopiruj do buffra
        strcpy(buffer , "");
        
        // hladaj mapu
        for(i++; i < strlen(s); i++)
        {
            char znak = s[i];
            strcat(buffer, &znak);
        }
        // ak nemam nic v buffri konci
        if(!strcmp(buffer, "")  || !strcmp(buffer, " ")) 
            return NULL;
        // vyhrad pamat
        if((t->map) != NULL) free(t->map);
        t->map = malloc( sizeof  *buffer + 1 );
        //   t->map = malloc( (strlen(buffer) + 1) * sizeof  *t->map ); ak nemam pamatkonci
        if(t->map == NULL)
            return NULL;
        // kopiruj obsah buffra
        strcpy(t->map, buffer);
        
        // vrat rozparsovany level
        return (t);
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    in parse_level
    Code:
    char * buffer = malloc( sizeof *s + 1);
    sizeof s gives you the size of a char *
    You have to either pass the size of the buffer to parse_level or use strlen()

    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    Ok i changed the allocation of all pointers ....
    I run debugger and there is something very interesting ...
    when prvy = parse_level ( t[0] ); is called everything is OK , system alloc memory for all members ... but when i reach the code in the for, novy = parse_level ( t[i] ); , it alloc memory for all members , but not for map ... when the malloc for map is calling it send me segmentation fault ...

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You're doing it all over the place to call sizof on a pointer and strcpy() ing things into it.
    Kurt

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    size = strlen(buffer) + 1;
    t->map = (char*)malloc( sizeof(char) * size );

    i have something like this ... is that problem ?

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
        char * buffer = malloc( sizeof *s + 1);   // size of a pointer +1 ( 5 or 9 on 64 bits )
        ....
        if((t->password) != NULL) free(t->password);
        t->password = malloc( sizeof *buffer + 1  );   // size of a pointer +1 ( 5 or 9 on 64 bits )
        // ak sa nepridelila pamat konci
        if(t->password == NULL)
            return NULL;
        // kopiruj obsah buffra
        strcpy(t->password, buffer);
    Kurt

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well getting strlen() and sizeof() mixed up certainly is one problem.

    Another biggie is this code, copy/pasted in several places (make it a function, then you only have to debug it once).
    Code:
        for(i++; i < strlen(s); i++)
        {
            char znak = s[i];
            strcat(buffer, &znak);
        }
    This is NOT how you append a single character to a string, since znak has no \0 character.

    At the very least, you need something like
    Code:
        for(i++; i < strlen(s); i++)
        {
            char znak[2] = { 0 };
            znak[0] = s[i];
            strcat(buffer, znak);
        }
    However, since you're trying to append the whole string, you can do it all with something like
    Code:
    strcat( buffer, &s[i+1] );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  2. malloc problem
    By Oscar Wong in forum C Programming
    Replies: 1
    Last Post: 05-27-2012, 06:59 AM
  3. malloc problem
    By Ideswa in forum C Programming
    Replies: 4
    Last Post: 10-23-2007, 04:11 PM
  4. Problem with malloc in C
    By JaysunFl in forum C Programming
    Replies: 7
    Last Post: 01-28-2006, 01:00 PM
  5. malloc problem
    By fnoyan in forum C Programming
    Replies: 3
    Last Post: 09-19-2003, 11:59 AM