Need help using gdb!

This is a discussion on Need help using gdb! within the C Programming forums, part of the General Programming Boards category; Hi, I've produced some code (some of which is below) that contains a segmentation error on line 87 ("ascii[d] = ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    31

    Need help using gdb!

    Hi,

    I've produced some code (some of which is below) that contains a segmentation error on line 87 ("ascii[d] = '\0';" in my htoi function). I'm creating a function to convert a hexdecimal string into a standard ascii string. I'm not sure that the problem is really based on line 87 but is certainly caused by my integration of my 'd' variable into my main 'for' loop. Anyway I've placed a breakpoint on line 86 ('break 86') in an effort to see what my variables look like before the segmentation error. However using 'print x', where x is any of the variables defined in this function, gdb returns meaninglessly big values for x. What am I doing wrong?
    Another issue I worry about is how does gdb know which variable to examine when the automatic variable 'a', say, is contained in different functions in the same file.


    Code:
    void htoi(char hex[], char ascii[])
    {
        int part1, part2, a, b, c, d;
    
        for ((a = 0) && (d = 0); hex[a] != 0; ++a && ++d) {
            part1 = hex[a];
            ++a;
            part2 = hex[a];
    
            part1 = ((part1 - 48) * 16);
    
            for ((b = 97) && (c = 10); b < 103; ++b && ++c) {
                if (part2 == b) {part2 = c;}}
    
            for ((b = 48) && (c = 0); b < 58; ++b && ++c) {
                if (part2 == b) {part2 = c;}}
    
            ascii[d] = (part1 + part2);
    
            printf("\nP1 = %d\nP2 = %d\nAscii[d] = %d", part1, part2, ascii[d]);}
    
        ++d;
    
        ascii[d] = '\0';
    
        for (b = 0; ascii[b] != 0; ++b) {
            printf("\nAscii Array(%d) contains: %d", b, ascii[b]);}
    }

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    why don't you just put a print statement or something in there..? you dont *NEED* a debugger you can add your own debug code/break points directly in the code using fprintf(stderr,blah,blah) and exit(0) (include stdlib.h) or whatever

    also, you can easily convert octal/hex/decimal whatever numbers into the string representation using s*printf/s*canf functions:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, char ** argv)
    {
        unsigned long num = 0;
        char * str = NULL;
        size_t size = 0;
    
        if (argc != 2)
            return -1;
    
        size = strlen(argv[1]) + 3; /* for \0 and 0x */
        if (!(str = malloc(size)))
            return -2;
    
        sscanf(argv[1],"%x",&num);
        sprintf(str,"0x%x",num);
    
        printf("My number is %s.\n",str);
        free(str);
    
        return 0;
    }
    Last edited by nonpuz; 02-28-2010 at 02:51 PM. Reason: forgot to free() ! doh!

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    gdb has no look ahead capabilities. It knows about global and local symbols if the source has been compiled with the "-g" flag, and their contents only when it steps into a function. A meaninglessly big value of 'a' means that it contains garbage so step over the instruction that initializes 'a' before printing it out and if possible post the entire code.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > for ((a = 0) && (d = 0); hex[a] != 0; ++a && ++d)
    An excellent example of programmers being far too smart for their own good.

    a = 0, therefore d = 0 is NOT EVALUATED.
    This leaves d with garbage.

    After that, you then do using some junk value as an array index.

    Enable some warnings on the compiler, it would have told you.
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 -c foo.c
    foo.c: In function ‘htoi’:
    foo.c:7: warning: value computed is not used
    foo.c:7: warning: value computed is not used
    foo.c:14: warning: value computed is not used
    foo.c:17: warning: value computed is not used
    foo.c:17: warning: value computed is not used
    foo.c:4: warning: ‘d’ may be used uninitialised in this function
    
    FYI, you should be using the comma operator in this instance.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. buffered vs unbuffered question
    By Overworked_PhD in forum Linux Programming
    Replies: 6
    Last Post: 07-04-2008, 04:57 PM
  2. Memory allocation error
    By cunnus88 in forum C++ Programming
    Replies: 5
    Last Post: 01-25-2008, 03:24 PM
  3. Contiguous Array version of Linked List
    By ampersand11 in forum C Programming
    Replies: 19
    Last Post: 10-07-2007, 03:05 AM
  4. Too much output in GDB
    By MacNilly in forum Tech Board
    Replies: 0
    Last Post: 09-13-2006, 12:45 PM
  5. does gdb lie?
    By dinjas in forum C Programming
    Replies: 8
    Last Post: 03-10-2005, 04:17 PM

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