Thread: Strange Segfault

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    Strange Segfault

    Im having trouble with this code
    Code:
    int getpackagename(char *package, char **dest) {
        int x = 0;
        int y = 0;
        
        while(package[x] != '\0') {
            if(package[x] == '-') {
                ++x;
            
                if( isdigit(package[x]) != 0 ) {
                    ++x;
                    
                    *dest = malloc( strlen(package) - x + 1 );
                    
                    while(package[x] != '\0') {
                        printf("x = %i\ty = %i\n", x, y);
                        *dest[y] = package[x];
                        
                        ++y;
                        ++x;
                    }
                    
                    *dest[y] = '\0';
                    
                    return 0;
                }
            }
        
            ++x;
        }
    
        return 1;
    }
    
    int main(int argc, char *argv[]) {
        char package[] = "test-1.0.0";
        char *test = NULL;
    
        getpackagename(package, &test);
        
        printf("%s\n", test);
        
        free(test);
    
        return 0;
    }
    It gives me back
    x = 6 y = 0
    x = 7 y = 1
    And then it crashes. I have no idea why.

    Any ideas?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I just found the one of the problems. There maybe more but I'll let you tackle them. You had

    Code:
    *dest[y] = package[x];
    That should probably be...

    Code:
    (*dest)[y] = package[x];
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    What Wizard told is right..
    You can do
    *dest[y]=package[x] (or) (to be clear) *(*(dest)+y)=package[x];
    Saravanan.T.S.
    Beginner.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >(or) (to be clear) *(*(dest)+y)=package[x];
    That's clear? I personally find array notation to be more clear when working with dynamic arrays. Pointer notation can get messy as you so neatly have shown.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    "(*dest)[y] = package[x];" gives me this and a crash.
    .
    .0
    .0.
    "*(*(dest)+y)=package[x];" does that same but scares me more.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    Have you tried running it throught the debugger to find out where exactly the program is crashing?
    $ENV: FreeBSD, gcc, emacs

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    yeah, i have cc0d3r. its these lines
    Code:
    while(package[x] != '\0') {
                        printf("x = %i\ty = %i\n", x, y);
                        *dest[y] = package[x];
                        
                        ++y;
                        ++x;
                    }
    I showed the out put in my first post, i dont get how it could work once and then crash.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    >(*dest)[y] = package[x]; gives me this and a crash.

    I don't know why it's crashing for you, but it runs fine on my system, here's the output:
    x = 6 y = 0
    x = 7 y = 1
    x = 8 y = 2
    x = 9 y = 3
    .0.0
    $ENV: FreeBSD, gcc, emacs

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    That still crashes for me, i showed my output from it before.

    Any new ideas?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Did you fix this line too:
    >>(*dest)[y] = '\0';
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    Did you fix this line too:
    nope, i completly forgot about that one sorry about that.

    thanks

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Just out of curiosity, wouldn't something like this be a little easier to write and debug?
    Code:
    int getpackagename(char *package, char **dest) {
        char *pos;
    
        if ((pos = strchr(package, '-')) == NULL)
            return 1;
    
        ++pos;
        if ((*dest = malloc(strlen(pos) + 1)) == NULL)
            return 1;
    
        strcpy(*dest, pos);
    
        return 0;
    }
    My best code is written with the delete key.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Prelude
    Just out of curiosity, wouldn't something like this be a little easier to write and debug?
    I was going to suggest strchr earlier, but he seemed to be having so much fun, I didn't want to spoil it.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  2. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 12:38 PM
  3. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 10:51 PM
  4. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM