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.

Community
  • 1
  • 1
Andrey Dobrikov
  • 457
  • 4
  • 20
  • 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. – Kiril Kirov Nov 12 '12 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`. – Pascal Cuoq Nov 12 '12 at 12:38
  • @PascalCuoq shouldn't that be an answer, not a comment. – weston Nov 12 '12 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. – Andrey Dobrikov Nov 12 '12 at 12:48
  • @AndreyDobrikov he said `%zu` not `%lu`. My K&R booksays `%u` for unsigned int as decimal, so might be worth trying that. Hopefully Pascal will expand on it in an answer. – weston Nov 12 '12 at 12:52
  • Try [this link](http://stackoverflow.com/questions/1865069/how-to-compile-a-64-bit-application-using-visual-c-2010-express) Should be what you're looking for. – Mike Nov 12 '12 at 12:57
  • when i try %zu it says unknows conversion type character "z"... – Andrey Dobrikov Nov 12 '12 at 12:57
  • BTW my compiler is minGW's gcc. – Andrey Dobrikov Nov 12 '12 at 12:58
  • So how do i check the length of a pointer on my system? – Andrey Dobrikov Nov 12 '12 at 13:01
  • Never mind, its still 4 bytes... :( – Andrey Dobrikov Nov 12 '12 at 13:07
  • @GajananH in duplicate i dont have answer to my question, im used to eclipse and dont want to change to vs. – Andrey Dobrikov Nov 12 '12 at 13:10
  • 1
    `%zu` is the format one should use for printing values of type `size_t`. More description can be found at http://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(...` – Pascal Cuoq Nov 12 '12 at 13:17
  • @AndreyDobrikov Could you post the compilation command which eclipse is using ? – CCoder Nov 12 '12 at 13:42
  • `%zu` was introduced with C99; if you're using a C89 or earlier compiler, you'll need to use `%lu` and cast the result of `sizeof` to `unsigned long`: `printf("%lu\n", (unsigned long) sizeof p);` – John Bode Nov 12 '12 at 15:26
  • Thanks ALL, you help me a lot doing my first steps! – Andrey Dobrikov Nov 12 '12 at 16:46

2 Answers2

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.

Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
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.

DrGlitch
  • 136
  • 11