1167 ptr = (void*)getcwd(cwd, MAX_PATH_LENGTH-1); (gdb) n 1168 if (!ptr) { (gdb) print ptr $1 = 0xbff2d96c '/media/MMC-SD/partition1/aaaaaaaaaaa' (gdb) print &cwd $2 = (char (*)[3500]) 0xbff2d96c (gdb) print strlen(cwd) $3 = 36 (gdb) print '%s',cwd $4 = '/media/MMC-SD/partition1/aaaaaaaaaaa', '\0' <repeats 912 times>, '��O�001\000\000\000\000��027\000\000\000�3����EL鷠3�000��027\000\000\000\000\000\000\000\027\000\000\000\000��/�027\000\000\000�3����N����\230���鷠3�000��027\000\000\000\000\000\000\000��000\000\000\000\001\000\000\000��M鷠3����\000\000\000\000.\231�027��w\005\b\001\000'... (gdb) print '%s', ptr $5 = 0xbff2d96c '/media/MMC-SD/partition1/aaaaaaaaaaa' (gdb) Quit
Why is ptr printing the string correctly but cwd not; this also affects the program and it crashes if I try to use the cwd…
[edit: turns out that crash was caused by a stupid buffer overflow on this var… grr…not gdb, but the print question was still valid]
The reason that
cwdis printed differently ingdbis becausegdbknows thatptris achar *(I guess) and thatcwdis an array of length3500(as shown in your output). So when printingptrit prints the pointer value (and as a service also the string it points to) and when printingcwdit prints the whole array.I don’t see any reason why using
cwdinstead ofptrwould lead to problems, but I would need to see some code to be sure.