Hello every one, I got a interview and 1 of interviewer ask me that Howmany byte is a pointer? I can not answer this question. seem it is trick question. All I know is pointer is an address location where the pointer point to.
Anyone get input to make it clear would be appreciate.
thanks

Recommended Answers

All 9 Replies

Ask him/her what platform.

It depends on the platform. Since a pointer is an address, it must be big enough to store the largest address. I'm fairly sure that in windows addresses are 32bits (4 bytes). In linux/unix it may be different.

It's 4 bytes on a 32-bit system (reason for a 4GB RAM limit), and 8 bytes on a 64-bit system. I believe you can check using something like sizeof(void*).

It's so simple: exactly sizeof(void*) . Let the interviewer counts ;)

>seem it is trick question.
It is a trick question. Not only can the size of a pointer vary depending on the system, the size of pointers to different types aren't required to be the same. The best answer to this question would be sizeof p , where p is defined as the pointer one wants to test.

>It's so simple: exactly sizeof(void*) .
How do you propose this to be meaningful for pointers to functions?

>It's so simple: exactly sizeof(void*) .
How do you propose this to be meaningful for pointers to functions?

The size of a pointer has to be consistent for a particular system. It is the reason for labeling a system as 32-bit, 64-bit, etc. The reason for having different types of pointers is for the allocation of memory at that particular location, and to help prevent buffer overflows when passing a pointer.

http://bytes.com/groups/c/216087-size-sizeof-pointer

The size of a pointer has to be consistent for a particular system. It is the reason for labeling a system as 32-bit, 64-bit, etc. The reason for having different types of pointers is for the allocation of memory at that particular location, and to help prevent buffer overflows when passing a pointer.

not nececerially. there is no default pointer size, just the actual size it is. If there were, it would imply that you could
somehow specify any size you wished. No mention of a default size is mentioned in the C spec.

There is no guarantee that all pointer types will have the same sizeeither (except in the case of pointers to structures and unions).

e.g an int* may be larger than a char*. This is particuarly true with regards to function pointers and data pointers, which are, for example, different sizes from each other in DOS depending on which memory model is used. For example, the AS/400 PS by IBM specifies additional informationb with regards to function pointers whioch can make it significantly bigger than an ordinary data pointer.

However, On most modern OS platforms and Compilers, all pointer types are of equal size and are reprsesneted in the same way on a low level.

In most cases, word length / register size of the processor (CPU) determines the size of a pointer which is generally 2 or 4 or 8 bytes for 16,32 and 64 bit CPUs respectively, but as i said, you should never assume this to always be the case. Thats why the size of a pointer determines the maximum memory that can be
addressed at once e.g the 4gb limit on 32 bit systems.

>seem it is trick question.
It is a trick question. Not only can the size of a pointer vary depending on the system, the size of pointers to different types aren't required to be the same. The best answer to this question would be sizeof p , where p is defined as the pointer one wants to test.

>It's so simple: exactly sizeof(void*) .
How do you propose this to be meaningful for pointers to functions?

1. The void* type size is the supremum of all pointer type sizes because it's possible to assign arbitrary pointer type value (including function pointers) to the void* type variable and get back the original value. It's impossible to do if sizeof(void*) < sizeof(wide_pointer_type) for wide_pointer_type (set theory).

Of course, a schizophrenical software architect is capable to invent the C or C++ implementation with unused data fields in all pointers except void* ones. Well, let's remember default argument promotion rules and %p format specification. It's too hard job for lunatic to implement all these features with such exotic pointers...

Make a compromise: sizeof(pointer_type) <= sizeof(void*) 2. Don't tread on me with member pointers ;), otherwise it was an ill-formed question...

3. The only type sizes in C and C++ are introduced via operator sizeof. Strictly speaking any answer without sizeof is incorrect (or the question is ill-formed;) )...

>The size of a pointer has to be consistent for a particular system.
Correct, but keep in mind that T* is a completely different type from U*, assuming T and U don't map to the same type. You seem to be pretending that all pointers have the same type (a universal pointer type), which is false.

>It is the reason for labeling a system as 32-bit, 64-bit, etc.
An xx-bit system is where xx is the number of bits that can be processed in parallel. Usually this means the data bus of the CPU, not the size of pointers in the addressing system.

>because it's possible to assign arbitrary pointer type value (including
>function pointers) to the void* type variable and get back the original value
That's incorrect. A pointer to void is defined to work only with object types, not function types. It's not safe to assign the address of a function to a pointer to void.

>Don't tread on me with member pointers
I'm not quite that pedantic. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.