I am using code below and getting different values.
int *p;
printf("Size of *p = %d", sizeof(*p)); // Here value is 4
printf("Size of p = %d", sizeof(p)); // Here value is 8
Can any one please explain, what exactly is the reason behind this?
For any pointer variable p, the variable p itself is the pointer and the size of it is the size of the pointer. *p is what p is pointing to, and the size of *p is the size of what is being pointed to.
So when sizeof(p) reports 8 then you know that a pointer on your system is 8 bytes, and that you're probably on a 64-bit system.
If sizeof(*p) reports 4 then you know that the size of int (which is what p is pointing to in your case) is 4 bytes, which is normal on both 32 and 64 bit systems.
You would get the same result by doing sizeof(int*) and sizeof(int).
Oh and a last note: To print the result of sizeof (which is of type size_t) then you should really use the "z" prefix, and an unsigned type specifier (since size_t is unsigned). For example "%zu". Not doing that is technically undefined behavior.
sizeof(*p) returns size of type what the pointer points to while sizeof(p) returns size of pointer itself.
In your case *p = int and sizeof(int) = 4 on your machine, while you need 8 bytes to store memory address (address where p points to).
sizeof(p) is the size of the pointer itself. It depends on the size of the address bus. Which means for a 64-bit system, the address bus size will be 64-bit (8 bytes) so pointer will be 8 bytes long (that shows your system is 64-bit). And on a 32-bit system, it's size will be 32-bit(4 bytes).
sizeof(*p) is the size of pointer type i.e. int here. So usually int is of 32-bit long that is 4 bytes.
Related
I am studying pointers and found I get incorrect size of a pointer.
What I learned is that since p_num is a pointer to an int, it should show 4 bytes but is shows 8.
What could the possible reason for it?
#include <stdio.h>
int main()
{
int num = 0;
int * p_num = NULL;
num = 10;
printf("num address: %p\n", &num);
printf("num size: %zd\n", sizeof(num));
printf("num value: %d\n\n", num);
p_num = #
printf("p_num address: %p\n", (void*)&p_num);
printf("p_num size: %zd\n", sizeof(p_num));
printf("p_num value: %p\n", p_num);
printf("p_num value pointed: %d\n", *p_num);
return 0;
}
Output:
num address: 0x7fffcb0e8ffc
num size: 4
num value: 10
p_num address: 0x7fffcb0e8ff0
p_num size: 8
p_num value: 0x7fffcb0e8ffc
p_num value pointed: 10
What could the possible reason for it?
The size of a pointer is dependent upon the architecture. On 32-bit architectures, a pointer usually occupies 4 byte in memory, on 64-bit architectures commonly 8 byte.
So there is nothing wrong with the program or your system itself, if you use a 64-bit architecture as you said in the comments. You just had an incorrect assumption of:
What I learned is that ... a pointer to an int ... should show 4 bytes.
Furthermore, this is usually valid for all pointer types, not only pointers to int. They usually have the same size in memory, but the standard does not require that as you can see in the quote from ISO/IEC 9899:2011 (C11) in this answer regarding to the exact question for that.
The size of a pointer may be also different from the size of the object the pointer point to. So differ between the size of a pointer object and the size of an object the pointer point to.
What I learned is that since p_num is a pointer to an int, it should show 4 bytes but is shows 8.
Your assumption is incorrect.
The int is one variable.
The int pointer is another variable.
So two different variables and these two variables do not need to have the same size.
In other words:
printf("p_num size: %zu\n", sizeof(p_num)); // Gives you the size of the pointer
printf("p_num size: %zu\n", sizeof(*p_num)); // Gives you the size of the pointed to object
^
notice the *
In your case you see that size of int is 4 while size of int pointer is 8. This is perfectly legal.
Most likely, you could have made the size difference even bigger by looking at a char and a char pointer. Then you would most likely see 1 for char and still 8 for char pointer.
sizeof(p_num) is the size of the pointer, not the size of the thing it points to.
sizeof(*p_num) is the size of the thing it points to.
Also, the parentheses are unnecessary. sizeof p_num and sizeof *p_num work fine. Parentheses are needed only to take the sizes of types (rather than expressions) or of complicated expressions.
In order to address more than 4gb in memory the pointers should be 8 bytes of size. Addresses of Memory more than 4gb cannot be represented using 32 bits i.e. 4 bytes.
In the 64-bit machines, the size of pointer is 8 bytes, because a pointer is a memory address. The pointer p_num pointes to the varibale num that is just one of several integer types. In the 64-bits system, sizeof(int) = 4.
How can I justify the output of the below C program?
#include <stdio.h>
char *c[] = {"Mahesh", "Ganesh", "999", "333"};
char *a;
char **cp[] = {c+3, c+2, c+1, c};
char ***cpp = cp;
int main(void) {
printf("%d %d %d %d ",sizeof(a),sizeof(c),sizeof(cp),sizeof(cpp));
return 0;
}
Prints
4 16 16 4
Why?
Here is the ideone link if you want to fiddle with it.
char *c[] = {"Mahesh", "Ganesh", "999", "333"};
c is an array of char* pointers. The initializer gives it a length of 4 elements, so it's of type char *[4]. The size of that type, and therefore of c, is 4 * sizeof (char*).
char *a;
a is a pointer of type char*.
char **cp[] = {c+3, c+2, c+1, c};
cp is an array of char** pointers. The initializer has 4 elements, so it's of type char **[4]. It size is 4 * sizeof (char**).
char ***cpp = cp;
cpp is a pointer to pointer to pointer to char, or char***. Its size is sizeof (char***).
Your code uses %d to print the size values. This is incorrect -- but it happens to work on your system. Probably int and size_t are the same size. To print a size_t value correctly, use %zu -- or, if the value isn't very large, you can cast it to int and use %d. (The %zu format was introduced in C99; there might still be some implementations that don't support it.)
The particular sizes you get:
sizeof a == 4
sizeof c == 16
sizeof cp == 16
sizeof cpp == 4
are specific to your system. Apparently your system uses 4-byte pointers. Other systems may have pointers of different sizes; 8 bytes is common. Almost all systems use the same size for all pointer types, but that's not guaranteed; it's possible, for example, for char* to be larger than char***. (Some systems might require more information to specify a byte location in memory than a word location.)
(You'll note that I omitted the parentheses on the sizeof expressions. That's legal because sizeof is an operator, not a function; its operand is either an expression (which may or may not be parenthesized) or a type name in parentheses, like sizeof (char*).)
a is an usually pointer, which represents the memory address. On 32-bit operating system, 32bit (4 Byte) unsigned integer is used to represent the address. Therefore, sizeof(a) is 4.
c is an array with 4 element, each element is a pointer, its size is 4*4 = 16
cp is also an array, each element is a pointer (the first *, wich point to another pointer (the second *). The later pointer points to an string in the memory. Therefore its basic element size should represent the size of a pointer. and then sizeof(cp) = 4*4 = 16.
cpp is a pointer's pointer's pointer. It is as well represent the 32bit memory address. therefore its sizeof is also 4.
a is a pointer. cpp is also a pointer just to different type (pointer to pointer to pointer).
Now c is an array. You have 4 elements, each is a pointer so you have 4 * 4 = 16 (it would be different if you would run it on x64).
Similar goes for cp. Try changing type to int and you will see the difference.
So the reason you got 4 16 16 4, is because 'a' is simply a pointer, on its own, which only requires 4 bytes (as a pointer is holding a 32bit address depending on your architecture) and so when you have a **pointer which is == to a *pointer[], your really making an array of pointers, and since you initalized 4 things that created 4 pointers, thus the 4x4 = 16. And for the cpp you may ask "well wouldn't it then be 16 as it was initalized?" and the answer is no, because a ***pointer is its own separate variable and still just a pointer(a pointer to a pointer to a pointer, or a pointer to an array of pointers), and requires only 4bytes of memory.
Here is a c program
#include<stdio.h>
main()
{
int(*p)[3][4];
printf("size of p= %d",sizeof(p));
printf("size of *p= %d",sizeof(*p));
return 0;
}
I can understand that size of (*p) should be the total size of the array i.e.48 bytes in this case.
But gcc version 4.2.1 gives the size of p = 8.
WHY?
Pointers store the address of the data to which it points. The size of the pointer variable is always 8.
sizeof(*p) = 48
sizeof(p) = sizeof(&(*p)) = 8
PS : 8 byte size is only in 64 bit systems. It is 4 bytes in 32 bit and 2 bytes in 16 bit systems.
Because the value of a pointer is an address, its value is not equal to what it is pointing to. Otherwise pointers would not be pointers they would just be regular variables and therefore useless.
If you have a 64 bit machine, which you do, the size of the pointer will always be 8 bytes.
Edit: if you are wondering why sizeof(*p) is 8, it is because *p evaluates to the first (unless you increment it) element in the array of elements it is pointing to. so *p = p[0], *(p+n) = p[n], etc.
So sizeof(*p) always equals the size of the type p is pointing to.
when you say declaration is int(*p)[3][4],
you actually mean declare p as pointer to array 3 of array 4 of int type. Here p is a pointer where the size of pointer is 8 on your 64 bit machine.
Ideally sizeof(p) == 8 & sizeof(*p) == 48
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
p = (int *)malloc(20);
printf("%d\n", sizeof(p));
free(p);
return 0;
}
On my 64-bit machine, 4 is printed as the size of p. I'm assuming this is because integers take up 4 bytes in memory, and p is an integer pointer. What if I was running a 32-bit machine? Also, what would happen if I replaced
int *p with double *p
and
(int *)malloc(20) with (double *)malloc(20)?
You are assuming wrong.
In printf("%d\n", sizeof(p)); you are not printing size of integer.You are printing here sizeof 'pointer to an integer' which is 4 bytes in your case.Most probably you will get same result on 32-bit machine.
Now about malloc, It allocates number of bytes and returns pointer to it.So same size of memory will be allocated even if you cast the pointer from int* to double*.
In pointers, It will take four bytes for all pointers.
So while you are checking with sizeof, even it is a character pointer it will give four bytes. If you need the value of that pointer use like this.
printf("%d\n", sizeof(p));// It will give the pointer size.
malloc is allocating the given bytes. And it will give equal to all the pointers. Then don't cast the result of malloc. Refer this link.
You have some misunderstanding let me point them,
p = (int *)malloc(20);
You are allocating memory of 20 bytes and malloc returns a void pointer and but compiler does the casting for you and (int *) is not needed. Even though you have a pointer to a double or an int it takes the same amount of bytes (In a 32bit system this merely for mapping 4GB memory space).
// Should this be 4 or 8?
printf("%d\n", sizeof(p));
This should be 8 on a x64 platform if your executable or the build is x64 only. I assume your build is 32bit and it returns 4.
Above printf has a wrong specifier. sizeof returns size_t and not an int. So correct form should be,
printf("%zu\n", sizeof(p));
Irrespective of whether 64-bit system or 32-bit system, size of a pointer variable is 4 bytes by default since 64-bit build settings also have Debug32 by default.
If we specifically change build settings on 64-bit, then the pointer variable can hold 8 bytes.
I'm assuming this is because integers take up 4 bytes in memory, and p is an integer pointer.
Your assumption is not correct ! To answer your doubt not only integer pointer take up 4 bytes.
int *ptrint;
char *ptrchar;
float *ptrfloat;
double *ptrdouble;
Here all ptrint, ptrchar, ptrfloat, ptrdouble takes 4 bytes of memory since it would be the address stored in that variable.
And if you replace int *p with double *p and (int *)malloc(20) with (double *)malloc(20) , the size would be still 4 bytes. I hope this ans cleared your doubts.
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?
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
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.
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.
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.
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.
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
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