-
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]);}
}
-
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;
}
-
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.
-
> 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.