Sizeof ponters

S

Subriemanian

I have been thinking that all pointers(to any obejct) have the same
size. The size of a pointer is the size of an Int. This is beause a
memory location is addressed by an Int. Is that right/wrong?
 
F

Flash Gordon

Subriemanian wrote, On 03/11/07 23:01:
I have been thinking that all pointers(to any obejct) have the same
size. The size of a pointer is the size of an Int. This is beause a
memory location is addressed by an Int. Is that right/wrong?

Completely, totally and utterly wrong. Pointers to different types can
be of different sizes. On most modern 64 bit implementations an int is
32 bits but a pointer is 64 bits. A fairly common series of processors
in one of its modes of operations uses a segmented architecture with
overlapping segments, so multiple bit patters point to the same address,
the architecture I am thinking of is the Intel 80x86.
 
S

santosh

Subriemanian said:
I have been thinking that all pointers(to any obejct) have the same
size. The size of a pointer is the size of an Int.

The answer depends on a specific implementation. To generalise is wrong.
Pointers may or may not be of the same sizes. A pointer may or may not
be of the same size as an int.
This is beause a memory location is addressed by an Int. Is that
right/wrong?

An int is a specific C type. In C only pointers can address memory,
never other types such as int. A memory address may seem like an
integer value to you, as per your very limited experience, but there
are architectures where a memory address may be a composite entity not
directly translatable to an integer value. That's why C completely
separates integers and pointers and converting between them is very
implementation dependant and requires a cast.
 
C

CBFalconer

Subriemanian said:
I have been thinking that all pointers(to any obejct) have the same
size. The size of a pointer is the size of an Int. This is beause a
memory location is addressed by an Int. Is that right/wrong?

Wrong. And wrong.
 
K

karthikbalaguru

I have been thinking that all pointers(to any obejct) have the same
size. The size of a pointer is the size of an Int. This is beause a
memory location is addressed by an Int. Is that right/wrong?

It can be of same size or of different size.
Varies based on architecture/implementation.

This is a frequently asked interview question :):)

Karthik Balaguru
 
C

cr88192

Subriemanian said:
I have been thinking that all pointers(to any obejct) have the same
size. The size of a pointer is the size of an Int. This is beause a
memory location is addressed by an Int. Is that right/wrong?

both of these are architecture dependent.

however, as it so happens, both are true for 32 bit x86 (except in certain
edge cases, such as an x86-based AS/400, ...).

only the former is true for x86-64 (where int remains 32 bits, but pointers
are 64 bits).

and neither is necessarily true on non-x86 architectures (though the former
is true, most of the time, as 'most' archs are also byte-addressed, and use
an 8-bit byte as the basic unit of memory). but, then again, on
word-addressed archs, it is up to the compiler how to handle the different
pointer types (afaik, it was fairly common practice to have all pointers be
the same size, but to ignore the low-order bits for non-byte-addressed
objects, or to have the pointers be pad-extended on the left-hand-side even
though many of the high-order bits were unused).

in part, a lot of this is catering to the general naivity of many coders
when it comes to oddball archs, and so seeming more 'normal' is often
preferable even in the face of reduced performance...


but, for portability, rely on neither. or, if targetting a specific arch, no
the specifics of that arch well...

for example, I know what is generally done on x86 and x86-64 down to the
machine code level, but can say much less about, say, PPC, Motorola, or ARM
based systems.

other people who target ARM, for example, likely also know about what the
compilers tend to output.


or such...
 
G

Gordon Burditt

I have been thinking that all pointers(to any obejct) have the same
both of these are architecture dependent.

however, as it so happens, both are true for 32 bit x86 (except in certain
edge cases, such as an x86-based AS/400, ...).

It isn't true for Large Model 32-bit x86, where pointers are 48 bits.
That isn't used much, though.
only the former is true for x86-64 (where int remains 32 bits, but pointers
are 64 bits).

MS-DOS on x86 used 16-bit configurations where int is 16 bit, data
pointers were 16 bit or 32 bit, and code pointers were 16 bit or
32 bit, in 4 possible combinations.
 
M

Mark Bluemel

Subriemanian said:
I have been thinking that all pointers(to any obejct) have the same
size. The size of a pointer is the size of an Int. This is beause a
memory location is addressed by an Int. Is that right/wrong?
Short response: Wrong. Next question.

Long response: Consider reading the FAQ and looking in Google Groups'
archive for this group - this is discussed periodically and your
misconception can be dealt with simply by some basic research...
 
C

Chris Dollin

Subriemanian said:
I have been thinking that all pointers(to any obejct) have the same
size.

Wrong -- they /may/ all have the same size, but it's not /required/
that they all have the same size, and there have been implementations
in which they /did not/ all have the same size. (Hint: not all machines
use byte addresses.)
The size of a pointer is the size of an Int.

(You mean `int`, right?) Wrong: that's not required.
This is beause a memory location is addressed by an Int.

What's an `Int`?
Is that right/wrong?

It's wrong /in general/, although it may happen to be true on a particular
implementation, such as many common desktop implementations.
 
C

cr88192

Gordon Burditt said:
It isn't true for Large Model 32-bit x86, where pointers are 48 bits.
That isn't used much, though.

yes, but then again, I will regard this as an edge case...
in my case, I was generally excluding edge cases.

MS-DOS on x86 used 16-bit configurations where int is 16 bit, data
pointers were 16 bit or 32 bit, and code pointers were 16 bit or
32 bit, in 4 possible combinations.

yes.


maybe I should have further specified that by x86 I meant:
x86 in 32 bit flat-model protected mode.

anymore, development of real-mode, big-real, or segmented 16/32-bit
protected-mode apps is fairly rare...

likewise, people, will usually state that they are developing specifically
for DOS or RM (or sometimes, i86), rather than using the more general term
of x86.

this is even more the case for '32 bit x86', which can be assumed to exclude
these other options.


x86-64, well, implies an x86-64 processor operating in long mode (if not in
long mode, I will still regard it as x86, as is also the case for legacy
mode).


or such...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top