7

I know: char * is a pointer to char. and int * is a pointer to int.

So, i want to confirm following two things:

  1. So now suppose I am on 32 bit machine, then that means memory addresses are 32 bit wide. Thus that means size of char * and int * is both 32 bits ( 4 bytes), right ? Also is size of char * * also same as size of int * ?

  2. suppose I have: int * ptr;

Thus now doing *((char * *) ptr) = 0x154 is same as *((int *) ptr) = 0x514 same, right ? ( 0x514 is just any random memory address)

Platform: I am on x86.

P.S.: I know type casting is not a suggested way to code. But I am doing Kernel coding, thus I HAVE TO do type casting !

user1599964
  • 860
  • 2
  • 13
  • 29
  • Even if the standard doesn't say it, I doubt it'll be easy to find an environment where it is actually different. – Mysticial Feb 16 '13 at 17:40
  • While their size is the same in practice, their alignment requirements are not. – DCoder Feb 16 '13 at 17:41
  • If you are doing "kernel coding", that already limits the number of platforms the code can run on. Check with the OS spec what is supported. – Bo Persson Feb 16 '13 at 22:01

2 Answers2

12

In C pointers are not guaranteed to have the same size. Now in reality most implementations pointers will be the same size, but that is an implementation detail of the compiler.

From the C Faq:

The old HP 3000 series uses a different addressing scheme for byte addresses than for word addresses; like several of the machines above it therefore uses different representations for char * and void * pointers than for other pointers

Depending on the ``memory model'' in use, 8086-family processors (PC compatibles) may use 16-bit data pointers and 32-bit function pointers, or vice versa.

Also *((char *)ptr) = 0x154 is not the same as *((int *)ptr) = 0x154. Because you are dereferencing the pointer you will write data the size of a char and the size of an int into the location pointed to by ptr. Assuming an 8 bit char and a 32 bit int, *((char *)ptr) = 0x154 will write0x154 to the memory address assigned to ptr and *((int *)ptr) = 0x154 will write 0x0000000154 to the 4 bytes starting at the address assigned to ptr.

shf301
  • 31,086
  • 2
  • 52
  • 86
  • Short answer but great link. I would recommend inlining the most interesting sentence into your answer, for instance “Older, word-addressed Prime machines were also notorious for requiring larger byte pointers (char *'s) than word pointers (int *'s).” – Pascal Cuoq Feb 16 '13 at 17:43
  • @user1599964 I assumed that was a typo as will as having different values in your example - casting to a `char**` or a `int*` mean very different things and are not directly comparable. They are different levels of indirection and are not even close do doing the same thing. – shf301 Feb 16 '13 at 17:58
  • @user1599964 I've thought about this more closes and your two statements are equivalent of `sizeof(char*) == sizeof(int)`. So a system with 32 bit pointers and 32 bit int's they will both write to the same location. – shf301 Feb 16 '13 at 21:21
1

Technically speaking, the C standard only guarantees that sizeof(char) == 1, and the rest is up to the implementation. But on modern x86 architectures (e.g. Intel/AMD chips) it's fairly predictable.

You've probably heard processors described as being 16-bit, 32-bit, 64-bit, etc. This usually means that the processor uses N-bits for integers. Since pointers store memory addresses, and memory addresses are integers, this effectively tells you how many bits are going to be used for pointers. sizeof is usually measured in bytes, so code compiled for 32-bit processors will report the size of pointers to be 4 (32 bits / 8 bits per byte), and code for 64-bit processors will report the size of pointers to be 8 (64 bits / 8 bits per byte). This is where the limitation of 4GB of RAM for 32-bit processors comes from -- if each memory address corresponds to a byte, to address more memory you need integers larger than 32-bits.

In practice, pointers will be size 2 on a 16-bit system (if you can find one), 4 on a 32-bit system, and 8 on a 64-bit system, but there's nothing to be gained in relying on a given size

Shobhit Sharma
  • 1,599
  • 13
  • 14
  • That's all fine until you run into a 36- or 48-bit architecture that [the standards committee also considered](http://stackoverflow.com/questions/6971886/exotic-architectures-the-standard-committee-cares-about). – Bo Persson Feb 16 '13 at 21:56