Fotinopoulos Giorgos
Fotinopoulos Giorgos

Reputation: 1027

GDB arithmetics

When i use the commands:

print/x &_start -> i get: 0x08049054 print/x &_key -> i get: 0x0804916d

It is quite easy to figure out that the difference is: 119h

But if i use the command:

print/x &_key-&_start -> i get: 0x46 (!!)

Why? Does anyone confirm this if debug a program of his own?

Upvotes: 2

Views: 2491

Answers (2)

Marcus Borkenhagen
Marcus Borkenhagen

Reputation: 6656

What you see is pointer arithmetic.

See also: SO:Pointer Arithmetic

Upvotes: 1

0xC0000022L
0xC0000022L

Reputation: 21319

This is because you use pointers to an unsigned int or some other type (for _start and _key) that is four bytes wide. You will notice that even with pointer arithmetics in C/C++ you get the same results.

Write this into foo.cpp:

#include <cstdio>

int main(int argc, char** argv)
{
  unsigned int* _start = (unsigned int*)0x08049054, * _key = (unsigned int*)0x0804916d;
  printf("start(%p), key(%p) -> [key - start](%li)\n", _start, _key, _key - _start);
}

Now the make file (GNUmakefile):

CXXFLAGS=-ggdb -g3 -O0

foo: foo.cpp

Build it by invoking make (GNU make, to be precise).

The output will be:

start(0x8049054), key(0x804916d) -> [key - start](70)

... and 70 == 0x46.

Upvotes: 1

Related Questions