2

Possible Duplicate:
What the pointer size in 64 bits computer in C++?

I'm studing C at The University.

I try to setup an environment for programming on Windows 7/8 and have a problem

This code:

int main()
  int *p;
  printf("%d",sizeof(p));
  return 0;
}

prints 4 instead of 8 (8 is printed in University). What can i do?

My Windows is 64bit and x64 processor.

15
  • 3
    Probably your compiler compiles the code for x86 CPUs. What's the compiler and what options are incliuded? Visual Studio? If so, you should make it compile x64 bit applications, as far as I remember, by default, it compiles 32bit apps. Commented Nov 12, 2012 at 12:22
  • This is not the cause of your problem, but sizeof returns a size_t. When you call printf("%d", ... you print it as an int. You should use printf("%zu", ... or simply cast the result of sizeof to int. Commented Nov 12, 2012 at 12:38
  • @PascalCuoq shouldn't that be an answer, not a comment. Commented Nov 12, 2012 at 12:47
  • @PascalCuoq Pascal, did you mean %lu? because %lu is what was in the example, i by mistake set %d. When i compile with %lu it says that "expects argument of long unsigned int, but argument 2 has type int * " Btw with lu warning after compile it prints 8. Commented Nov 12, 2012 at 12:48
  • 1
    %zu is the format one should use for printing values of type size_t. More description can be found at stackoverflow.com/a/5943869/139746 . I am not putting this as an answer because it won't solve the unrelated 4/8 issue that the question really is about. If the compiler does not support %zu, then workarounds are printf("%d",(int)sizeof(... or printf("%lu",(unsigned long)sizeof(... Commented Nov 12, 2012 at 13:17

2 Answers 2

5

You probably compile the code into 32 bit application. You need to compile it as 64 bit application. Check your compiler settings. It does not matter that your OS is 64 bit.

Sign up to request clarification or add additional context in comments.

8 Comments

Ok so which compiles can i use to compile as 64 bit app? My compiler is minGW gcc.
@AndreyDobrikov: I would use VS 2012, but it is more a C++ compiler. For C you can try mingw-w64.
@AndreyDobrikov try -m64 as compiler argument.
@RedX -m64 "sorry, unimplemented: 64 bit mode not compile in.
@JurajBlaho is there mingw-w64 exe? or how i use it? when i try to download i get *.tar file with no bin and no gcc.exe...
|
1

The "problem" here is results depending on the compile architecture.

Basic types in C (like e.g. int, double, char) do not have a predefined size; it is up to the compiler which size to use for which type.

As for pointers, you typically want to be able to address any memory location available on your machine. On 32 bit architectures, the address range is 2^32. As a pointer is nothing more than a number referring to the address the memory is located at, 2^32 addresses (i.e., a range of 4 Bytes) suits just fine.

For 64 bit systems, in order to address all memory, a range of 2^64 (i.e. 8 bytes) is necessary.

Therefor, pointer sizes need to depend on the system architecture.

Keep in mind: all pointer types (be it int*, char*, double* or whatever) have the same size! Using integers and integer pointers on 32 bit can therefor be a bit confusing, as an int has a size of 4 bytes on most architectures, as well.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.