0

I'm trying to complete Problem 4 of Project Euler, and I've been trying to debug my solution for quite awhile now. The bug itself isn't part of my question – I can't figure out why GDB won't show me an array value necessary to figuring out the bug.

Whenever gdb reaches this loop,

for (int i = numDigits; i > 0; i--) {
    numArray[i-1] = prevPalindrome % 10;
    prevPalindrome /= 10;

I am unable to get a value from GDB by the command print numArray or print numArray[0] or print numArray[5].

GDB instead does the following:

  • print numArray returns $1 = <optimized out>
  • Both print numArray[0] and print numArray[5] return `value has been optimized out'

I have already scoured StackOverflow for a way to fix this. I have attempted the following, and none have worked:

  • My makefile already uses -O0 (its command is clang -ggdb3 -O0 -std=c99 -Wall -Werror test.c -lcs50 -lm -o test), so I tried to use just clang -O0 test.c -lcs50 -lm -o test. This did not do anything.
  • I tried using -0g, and I got a compiler error.
  • I tried making the numArray volatile. I got the code to compile, but it was still <optimized out>.
  • I tried TUI mode at the suggestion of @Zan-Lynx, and it was quite cool, but I to be honest I got lost and it was still <optimized out>.

Is there something I am missing here? Shouldn't I just be able to print an array value?

The full code is available here, with the loop in question on line 95.

Community
  • 1
  • 1
jhschwartz
  • 168
  • 1
  • 2
  • 13

2 Answers2

1

There are two possible causes I can think of:

  1. You are debugging the wrong binary (i.e. you think you are debugging -O0 binary, but are in fact debugging an -O2 or -O3 binary).
  2. Your GDB is too old compared to your GCC, and doesn't understand newer debugging format.

To eliminate possibility of #1, add abort() before accessing numArray, rebuild your program, run it under GDB, and confirm that it dies with SIGABRT. If it does not, my guess is correct and you are debugging a different binary from the one you think you are debugging.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
0

Will just -g work? Also, leave out the O0. As in clang -g -std=c99 -Wall -Werror test.c -lcs50 -lm -o test.

Duane McCully
  • 406
  • 3
  • 6