943,911 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1361
  • C++ RSS
Nov 30th, 2008
0

Howmany byte is pointer?

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
quocnam00 is offline Offline
1 posts
since Nov 2008
Nov 30th, 2008
0

Re: Howmany byte is pointer?

Ask him/her what platform.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Nov 30th, 2008
0

Re: Howmany byte is pointer?

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.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Nov 30th, 2008
0

Re: Howmany byte is pointer?

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*).
Last edited by nmaillet; Nov 30th, 2008 at 10:38 pm.
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Dec 1st, 2008
0

Re: Howmany byte is pointer?

It's so simple: exactly sizeof(void*) . Let the interviewer counts
Last edited by ArkM; Dec 1st, 2008 at 9:56 am.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 1st, 2008
0

Re: Howmany byte is pointer?

>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?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 1st, 2008
0

Re: Howmany byte is pointer?

Click to Expand / Collapse  Quote originally posted by Narue ...
>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.
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Dec 1st, 2008
0

Re: Howmany byte is pointer?

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

Quote ...
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.
Last edited by jbennet; Dec 1st, 2008 at 1:09 pm.
Moderator
Featured Poster
Reputation Points: 1784
Solved Threads: 574
Moderator
jbennet is offline Offline
16,520 posts
since Apr 2005
Dec 1st, 2008
0

Re: Howmany byte is pointer?

Click to Expand / Collapse  Quote originally posted by Narue ...
>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 )...
Last edited by ArkM; Dec 1st, 2008 at 1:53 pm.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 1st, 2008
0

Re: Howmany byte is pointer?

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Connecting to a php file
Next Thread in C++ Forum Timeline: virtual class with friend functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC