Pergunta

I'm reading a book about C, and GNU but it references code on a 32 bit processor. I've already figured out that EIP is actually RIP on 64 bit, but it keeps giving me woods when I deal with pointers... Are there done some more differences that I'm not aware of??? Thanks - pointer code -

int j = 10;
int *pointr;
pointr = j;

printf("in j has an adress of %p", pointr);
Foi útil?

Solução

Your code should be

pointr = &j;

With pointr = j; you are assigning value of j i.e 10 to the pointer.

Outras dicas

The syntax of C is the same on all processors.

What is different are the sizes of the different native types. There are rules for what is permitted, but there is also an awful lot of scope for inventiveness. In particular, pointers are 8 bytes long on a 64-bit machine, rather than 4 bytes on a 32-bit machine. Most 32-bit compilers treat both int and long as 32-bit types; most 64-bit compilers treat long as a 64-bit type, the notable exception being Windows 64 where long remains a 32-bit type.

Note that your code should be:

int j = 10;
int *pointr = &j;
printf("int j has an address of %p\n", pointr);

It isn't.

Except some sizes of variables (esp pointers) might be different.
This should almost never matter but if it does, you should be using sizeof to get sizes if they ever matter.

However your code should probably be taking the address of j Whether in 64bit or 32 bit

int j = 10; int *pointr;
pointr = &j;
printf("in j has an adress of %p", pointr);

The one major thing that changes when not using inline assembly is the pointer size. If your program doesn't explicity depend on the pointer size, you should usually be fine.

See http://www.viva64.com/en/a/0065/ for a detailed list of problems with 64-bit C/C++ code.

In your code, you should use:

pointr = &j;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top