Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Sizeof ponters

Reply
Thread Tools

Sizeof ponters

 
 
Subriemanian
Guest
Posts: n/a
 
      11-03-2007
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?

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      11-03-2007
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.
--
Flash Gordon
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      11-03-2007
Subriemanian wrote:

> 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.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-04-2007
Subriemanian wrote:
>
> 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.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
karthikbalaguru
Guest
Posts: n/a
 
      11-04-2007
On Nov 4, 4:01 am, Subriemanian <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
cr88192
Guest
Posts: n/a
 
      11-04-2007

"Subriemanian" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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...



 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      11-04-2007
>>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, ...).


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.

 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      11-05-2007
Subriemanian wrote:
> 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...
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      11-05-2007
Subriemanian wrote:

> 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.

--
Chris "prime" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

 
Reply With Quote
 
cr88192
Guest
Posts: n/a
 
      11-07-2007

"Gordon Burditt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>>>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, ...).

>
> 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.


>>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.
>


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...



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sizeof( int ) != sizeof( void * ) blufox C Programming 2 05-22-2006 03:25 PM
Return array of ponters? priya C Programming 16 05-24-2005 09:04 AM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments