-1

I tried out the following code example from the "Understanding and Using C Pointers" (Reese) book, that illustrates passing a two dimension array pointer to a function:

void display2DArray(int arr[][5], int rows) {
   for (int i = 0; i<rows; i++) {
      for (int j = 0; j<5; j++) {
         printf("%d", arr[i][j]);
      }
      printf("\n");
   }
}

void main() {
   int matrix[2][5] = {{1, 2, 3, 4, 5},{6, 7, 8, 9, 10}};
   display2DArray(matrix, 2);

When I examine the arr variable using the gdb debugger during execution, I get:

(gdb) p arr
$156 = (int (*)[5]) 0x7fffffffe1c0
(gdb) p &arr
$157 = (int (**)[5]) 0x7fffffffe198
(gdb) p *0x7fffffffe198
$158 = -7744

What is this negative number -7744?

I thought that dereferencing the address 0x7fffffffe198 should yield 0x7fffffffe1c0?

Zong
  • 6,160
  • 5
  • 32
  • 46
korppu73
  • 237
  • 1
  • 2
  • 5
  • 2
    You're asking GDB to print something without any specification as to what it is. When using `arr` and `&arr`, the underlying type can be deduced (obviously, just look at your output). When presented with a hand-coded literal address and dereference, it is *probably* running home to the only thing it knows: assuming and integer an giving what it found. – WhozCraig Jun 28 '18 at 20:10
  • 2
    If you tell it to print it as unsigned hex you'll get the result you expect. – Barmar Jun 28 '18 at 20:10
  • https://stackoverflow.com/questions/9671820/print-variables-in-hexadecimal-or-decimal-format – Eugene Sh. Jun 28 '18 at 20:11
  • 2
    `-7744` is `#x1fffe1c0` which is the bottom half of what you expected to see. – Barmar Jun 28 '18 at 20:12
  • Try `x/a *0x7fffffffe198` – WhozCraig Jun 28 '18 at 20:22
  • 1
    Memory addresses are never negative, only in the way they are reported. If you are using a book which codes `void main()` I suggest you find a newer book, using `int main(void)` or `int main(int argc, char *argv[])` – Weather Vane Jun 28 '18 at 20:30
  • Memory addresses appear as negative numbers, because you have chosen to print the address as a signed integers The addresses are just numbers, and every number that has it's most significant bit set will be printed as a negative number, thus higher addresses will have this bit set and will appear as negative decimal numbers while the lower addresses won't have that bit set and will appear as positive decimal numbers. – малин чекуров Jun 28 '18 at 20:43
  • @WhozCraig Thanks for replies. x/a works as you suggest but I think that the dereference operator should be left out with the x command – korppu73 Jun 28 '18 at 21:12
  • @WeatherVane "Memory addresses are never negative" hmmm. Cannot find C spec support for that. Certainly easy to think of pointers as non-negative, but that is only one memory model. The sign-ness of a pointer seems like a non-issue anyways - but perhaps an interesting curiosity. AFAIK, compliant code `printf("%p", some_void_ptr);` could emit "-123" - yet that is not compelling evidence that the pointer is "negative" any more than the output could have been `"some_text:1234"` and is the positive or negative? – chux - Reinstate Monica Jun 28 '18 at 22:15
  • @chux that is silly. Please provide an example of the `%p` format emitting "-123". Was that comment really worth it? – Weather Vane Jun 28 '18 at 22:23
  • @chux as you well know, an address is a representation of the state of the address bus. On a modern PC that is a virtual address, in a basic OS might be a real address. Showing it as a negative decimal number is extremely unhelpful. The hex representation is usual and more readable. – Weather Vane Jun 28 '18 at 22:58
  • OT: `main()` returns `int` not `void`. – alk Jun 30 '18 at 11:52

1 Answers1

0

What is this negative number -7744?

It is decimal conversion of FFFFFFFFFFFFE1C0.

I thought that dereferencing the address 0x7fffffffe198 should yield 0x7fffffffe1c0?

Yes it does , but it is converted to decimal representation.

Memory addresses are not negative but data types used to represent the memory locations can be.