3

When I execute the following code:

int main()
{
    char **temp;
    printf("Size of **temp %d", sizeof(**temp));
    printf("Size of *temp %d", sizeof(*temp));
    printf("Size of temp %d", sizeof(temp));
    return 0;
}

I get:

Size of **temp 1
Size of *temp 8
Size of temp 8

What I don't understand is how does a char pointer have a size of 8? Is it machine independent?

noMAD
  • 7,744
  • 19
  • 56
  • 94

7 Answers7

6

Is sizeof char ** pointer dependent on the architecture of machine

More than machine dependent, it's compiler-dependent. The only size you can always rely on to be the same is sizeof(char) == 1.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
6

In the original question you weren't calling sizeof.
duskwuff fixed that for you.

The output produced was:

Size of **temp 1
Size of  *temp 8
Size of   temp 8

Reason:

On a 64-bit architecture, pointers are 8-bytes (regardless of what they point to)

 **temp is of type char ==> 1 byte
  *temp is of type pointer-to-char ==> 8 bytes
   temp is of type pointer-to-pointer-to-char ==> 8 bytes
Community
  • 1
  • 1
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • umm.. This looks exactly the same as my code?? Am I missing something here? – noMAD Apr 10 '12 at 18:18
  • In the first version of the question you posted, you didn't call Sizeof. Someone edited your question to fix your mistake. – abelenky Apr 10 '12 at 18:20
  • aah, alright!! My bad. Didn't know that, sorry. Thanks for your answer :) – noMAD Apr 10 '12 at 18:23
  • 1
    `On a 64-bit architecture, pointers are 8-bytes` Not completely true. You can run 32-bit compilers where pointers are 4 bytes on a 64-bit machine – Pavan Manjunath Apr 10 '12 at 18:57
  • @PavanManjunath Not really. A 32 bit process running on a 64 bit system still runs inside a 32 bit machine. It's just an emulated machine rather than the true underlying physical machine. – David Heffernan Apr 10 '12 at 20:35
  • 1
    Pointers to different types don't *have* to be the same size or have the same representation (6.2.5/27), although you'll be hard-pressed to find an example among modern desktop systems. It's more of an issue in the embedded world these days. – John Bode Apr 10 '12 at 20:58
2

If you are running MS Visual Studio on a 64 bit machine, you can change the Solution platform from win32 to x64 to see the size of the pointer growing from 4 to 8 bytes with this code.

Samboy786
  • 101
  • 1
  • 8
1

The size of a pointer is machine dependent.

What I don't understand is how does a char pointer have a size of 8

A char pointer, i.e. a char* variable does have a size of 8, on your system. In your code sizeof(*temp) gives the size of a char pointer, and it is 8. The reason sizeof(**temp) is 1 is that **temp is char and sizeof(char) is 1 by definition.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

sizeof operator is resolved by the compiler at COMPILE TIME. No actual deference-ing occur at runtime. What is return by the size of operator is the size of the type. so

sizeof(*(char *)0) is 1

If the target platform has 64 bit pointers, then sizeof(temp) would be 8. If the target platform has 128 bit pointers, then sizeof(temp) would be 16.

"target" doesn't mean it is the platform you compile on, because you can cross-compile.

pizza
  • 7,296
  • 1
  • 25
  • 22
1

size of pointer will depend on your machine(check your machine type).If your machine is 64 bit then pointer size is 8 bytes and for 32 bit machine pointer size is 4 bytes.

pointer size will be same irrespective of type of the pointer that pointer variable points to. pointer type is more useful for pointer arithmetic, don't be confused with its size when using sizeof operator

AravindMS
  • 49
  • 7
0
Size of **temp 1
Size of *temp 8
Size of temp 8

it's showing the right answer. Why?

Because
Size of **temp 1--->> pointing to char.
Size of *temp 8----->>pointing pointer to char.
Size of temp 8---->>>>pointing pointer to pointer to char.

and you are using 64 bit compiler if you will run this code on 32 bit compiler it will give you

Size of **temp 1
Size of *temp 4
Size of temp 4
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Varun Chhangani
  • 1,116
  • 3
  • 13
  • 18