Thread: what am i missing now...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    what am i missing now...

    I was trying to write a brief example for scarlet00014 ("How do I read this in as a string?") using strtok in a method I've used dozens of times before.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (int argc, char *argv[]) {
            short int i=0;
            size_t len;
            char *line=NULL, *tok, DL[]=":\n";
            FILE *fstRO=fopen("/root/test/test.txt", "r");
            struct {char *name;
                    int age;
            } *example;
    
            while((getline(&line,&len,fstRO)) != -1) {
                    tok=strtok(line,DL);
                    printf("%d: %s=%d\n", i,tok,strlen(tok));
                    example[i].name=malloc(strlen(tok)+1);
                    strcpy(example[i].name,tok);
                    puts(example[i].name);
                    tok=strtok(NULL,DL);
                    example[i].age=atoi(tok);    
                    free(line);
                    i++;
            }   
    }
    except now it causes this:

    0: Jamie lynn=10
    Segmentation fault


    which gdb says is caused by example[i].name=malloc(strlen(tok)+1);
    As I was saying, the example, as far as I can tell, is EXACTLY like other routines I've already written that work fine.

    Where did I get confused?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You haven't allocated any memory for example[i].

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by rags_to_riches View Post
    You haven't allocated any memory for example[i].
    Bang on
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    Your imput buffer (line) has no memory allocated for it. The best way is to declare line like:

    char line[1024];

    That will allow up to 1023 bytes of input.


    The segmetation fault is caused by trying to free memory (line) that was never allocated.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > The segmetation fault is caused by trying to free memory (line) that was never allocated.
    No it's not. line was allocated by GNU getline()

    Even so, free(NULL) is defined to do nothing.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by zacs7 View Post
    Even so, free(NULL) is defined to do nothing.
    I thought that only applies to "delete 0". AFAIK bad things happen when you free a NULL pointer...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zacs7 View Post
    > The segmetation fault is caused by trying to free memory (line) that was never allocated.
    No it's not. line was allocated by GNU getline()

    Even so, free(NULL) is defined to do nothing.
    Yes, free(line) should be after the while block, not in it. But that doesn't cause a segfault for me -- it causes a "double free" error.

    The segfault was definately because, as rags_to_riches pointed out, I didn't malloc and realloc memory for an array of structs.

    A debugged and functional version of this was posted to scarlet00014's question.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > I thought that only applies to "delete 0". AFAIK bad things happen when you free a NULL pointer...
    Not according to the standard, free() checks the pointer is not NULL before it frees. Thus
    Code:
    if(x != NULL)
        free(x);
    Is redundant and silly.

    > Yes, free(line) should be after the while block, not in it.
    Depends on what effect you want. If you pass a pointer pointing to nothing (ie NULL) getline() will allocate memory for you. Otherwise it will use whatever you give it. However if you wanted to do the former over and over, you'd want to free the line and set it to NULL. So you keep passing in a pointer pointing at NULL.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, since in the example execution will never get to that point, it is all kind of theoretical, but I'd still like to know how line ended up as NULL*.

    Actually I think it's strange about the double free and having to move the line out of the block...perhaps something to do with the action of strtok.

    *b/t/w this is not a question, I'm being facetious
    Last edited by MK27; 09-15-2008 at 03:17 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM