size of pointers

S

subramanian

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?
 
R

Richard Bos

subramanian 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?

Entirely wrong, on all three counts.

Richard
 
I

iamgodk

machine word is 32bit

sizeof(char) == 1
sizeof(int) == 4
sizeof(char *) == 4
sizeof(int *) == 4

char array[30]= "abc"
char *p=array;

sizeof(array) == 30
sizeof(p) == 4
strlen(array) == strlen(p)
strlen(array) == 3


ÓÚ Mon, 18 Dec 2006 23:32:41 -0800£¬subramanianдµ½£º
 
M

Martin Ambuhl

subramanian 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?

You are wrong on all counts. Pointers to different kinds of objects may
have different sizes; that size need not be an int; memory locations are
addressed by a pointer-to-object, not by an int.
 
M

Martin Ambuhl

iamgodk said:
machine word is 32bit

This is by no means guaranteed.
sizeof(char) == 1
OK

sizeof(int) == 4

This is by no means guaranteed.
sizeof(char *) == 4

This is by no means guaranteed.
sizeof(int *) == 4

This is by no means guaranteed.
char array[30]= "abc"
char *p=array;

sizeof(array) == 30
OK

sizeof(p) == 4

This is by no means guaranteed.
strlen(array) == strlen(p)
OK

strlen(array) == 3

OK

Please don't post information specific to your implementation as if it
were normative for the C programming language.
 
I

iamgodk

:)
some examples which are not normative may help
beginner to understand.

ÓÚ Tue, 19 Dec 2006 03:05:53 -0500£¬Martin Ambuhlдµ½£º
iamgodk said:
machine word is 32bit

This is by no means guaranteed.
sizeof(char) == 1
OK

sizeof(int) == 4

This is by no means guaranteed.
sizeof(char *) == 4

This is by no means guaranteed.
sizeof(int *) == 4

This is by no means guaranteed.
char array[30]= "abc"
char *p=array;

sizeof(array) == 30
OK

sizeof(p) == 4

This is by no means guaranteed.
strlen(array) == strlen(p)
OK

strlen(array) == 3

OK

Please don't post information specific to your implementation as if it
were normative for the C programming language.
 
M

mark_bluemel

iamgodk said:
machine word is 32bit

On your implementation. I've worked on machines where words were 36
bits, machines where words were 16 bits and machines where words are 64
bits.
sizeof(char) == 1
sizeof(int) == 4

On your implementation perhaps. It doesn't have to be.
sizeof(char *) == 4

See above.
sizeof(int *) == 4

See above.

As I recall, on Pr1me systems, sizeof(char *) was 6 and sizeof(int *)
was 4. There were good architectural reasons for this.

As Martin A commented, you should not regard your specific C
implementation as definitive. My car is grey, has a diesel engine and 5
doors - does that mean anything with a petrol engine, blue and with 3
doors is not a car?
 
M

mark_bluemel

iamgodk said:
Tue, 19 Dec 2006 03:05:53 -0500£¬Martin Ambuhl£º
iamgodk said:
machine word is 32bit

This is by no means guaranteed.
sizeof(char) == 1
OK

sizeof(int) == 4

This is by no means guaranteed.
sizeof(char *) == 4

This is by no means guaranteed.
sizeof(int *) == 4

This is by no means guaranteed.
char array[30]= "abc"
char *p=array;

sizeof(array) == 30
OK

sizeof(p) == 4

This is by no means guaranteed.
strlen(array) == strlen(p)
OK

strlen(array) == 3

OK

Please don't post information specific to your implementation as if it
were normative for the C programming language.
some examples which are not normative may help
beginner to understand.

1) Please don't top-post.
2) Providing specific examples which reinforce the original posters
misconceptions will not help their understanding at all.
 
I

iamgodk

ÓÚ Tue, 19 Dec 2006 00:34:46 -0800£¬mark_bluemelдµ½£º
Tue, 19 Dec 2006 03:05:53 -0500£¬Martin Ambuhl£º
iamgodk wrote:
machine word is 32bit

This is by no means guaranteed.

sizeof(char) == 1

OK

sizeof(int) == 4

This is by no means guaranteed.

sizeof(char *) == 4

This is by no means guaranteed.

sizeof(int *) == 4

This is by no means guaranteed.


char array[30]= "abc"
char *p=array;

sizeof(array) == 30

OK

sizeof(p) == 4

This is by no means guaranteed.

strlen(array) == strlen(p)

OK

strlen(array) == 3

OK

Please don't post information specific to your implementation as if it
were normative for the C programming language.
some examples which are not normative may help
beginner to understand.

1) Please don't top-post.
2) Providing specific examples which reinforce the original posters
misconceptions will not help their understanding at all.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thanks for your answer or the examples of the entire answer,
and I think your answer would help the the original posters:)

If you agree with your own opinion, please list all examples of
disadvantage about my post. Could you could u do that? Thanks.
 
P

pete

iamgodk said:
ÓÚ Tue, 19 Dec 2006 00:34:46 -0800£¬mark_bluemelдµ½£º
If you agree with your own opinion, please list all examples of
disadvantage about my post. Could you could u do that? Thanks.

What's the point of saying:
machine word is 32bit
sizeof(int) == 4
sizeof(char *) == 4
sizeof(int *) == 4
?

What is anybody supposed to be able to learn from that?
 
I

iamgodk

ÓÚ Tue, 19 Dec 2006 08:59:40 +0000£¬peteдµ½£º
ng:
machine word is 32bit
sizeof(int) == 4
sizeof(char *) == 4
sizeof(int *) == 4
?

What is anybody supposed to be able to learn from that?

somebody, like me, even learn something from yours:)
and I think that you think so too.

Can nobody not learn something from yours?
 
J

jacob navia

subramanian a écrit :
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?
No.

It is true that memory locations are integers, in some
machines a sequence of integers that refer to sequential
memory locations x, x+1, x+2, x+3, etc.

The "int" data type can be different from those integers,
much smaller, for instance. This is common in small
processors, where you have a big ROM and a small RAM amount.
There, pointers can be much bigger (24 bits or 32 bits) than
integers, since pointers must address the ROM.

Besides, not all pointers are equal, since you can have pointers to ram
that will fit in 16 bits, for instance, and pointers to ROM, that
need 24 bits, and pointers to some EPROM that will be 32 bits,
since the addresses of those areas were designed like that
in the circuit board.
 
C

Chris Dollin

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

Why have you been thinking that? It's not true.

(By "it's not true" I mean that it's not /required/ to be true
by the C Standard, and is (or was) /actually/ false on real
C implementations [1].)
The size of a pointer is the size of an int.

Not true (ditto).
This is beause a memory location is addressed by an int.

Not true (ditto) [2].
Is that right/wrong?

Yes, it's wrong.

[1] I recall Chris Torek describing one of the Primes where
char* was bigger than other* because the Prime was word-
addressed and extra room was needed in the char* for the
character offset.

[2] MS-DOS implementations had, I am told, multiple memory
models manifesting one where ints were 16 bits and pointers
were 32 bits.
 
P

pete

iamgodk said:
ÓÚ Tue, 19 Dec 2006 08:59:40 +0000£¬peteдµ½£º


somebody, like me, even learn something from yours:)
and I think that you think so too.

Can nobody not learn something from yours?

The original post was:
"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?"

Your post seems to suggest that you think that
the original poster was right.
The original poster was wrong.

Pointers to different object types
may or may not have the same size.

What somebody can learn from me,
is that the original poster was wrong,
and that you were wrong to suggest otherwise.
 
K

Keith Thompson

jacob navia said:
subramanian a écrit :
No.

It is true that memory locations are integers, in some
machines a sequence of integers that refer to sequential
memory locations x, x+1, x+2, x+3, etc.

It is not *necessarily* true that memory locations are integers. In
most modern systems, machine addresses can be viewed as integers, but
the C standard is designed to support more complex memory models.
(For example, some systems use segmented addresses, where a machine
address consists of a segment number and an offset within the segment;
this is not necessarily meaningful as a single number. Arbitrarily
complex schemes are possible.)

As far as the C language is concerned, pointers are pointers and
integer are integers.
The "int" data type can be different from those integers,
much smaller, for instance. This is common in small
processors, where you have a big ROM and a small RAM amount.
There, pointers can be much bigger (24 bits or 32 bits) than
integers, since pointers must address the ROM.

Besides, not all pointers are equal, since you can have pointers to ram
that will fit in 16 bits, for instance, and pointers to ROM, that
need 24 bits, and pointers to some EPROM that will be 32 bits,
since the addresses of those areas were designed like that
in the circuit board.

You're talking about pointers to objects in different memory spaces;
that's not something that's directly supported by C, though some
compilers might support something like that as an extension. (Perhaps
something like "near" and "far" pointers?)

More relevantly, a pointer to one *type* may have a different size
than a pointer to another type. For example, a char* might be larger
than an int*. This shouldn't cause any problems for well-written
portable code.
 
I

iamgodk

ÓÚ Tue, 19 Dec 2006 09:21:06 +0000£¬peteдµ½£º
The original post was:
"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?"

Your post seems to suggest that you think that
the original poster was right.
The original poster was wrong.

Pointers to different object types
may or may not have the same size.

What somebody can learn from me, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
is that the original poster was wrong, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
and that you were wrong to suggest otherwise.
What somebody can learn from you,
is that I must select new suitable cases for answer.
That's right. It's my fault. And then,
please remeber u have not reply to the original poster until now:)
Whoever are you, please give the original poster valuable answer
rather than give me the unilateralism answer for my post:)
Thanks.
 
R

Richard Bos

jacob navia said:
subramanian a écrit :

No.

It is true that memory locations are integers,

Entirely wrong. Not all the world is a bitty-box, you know.

Richard
 
K

Keith Thompson

iamgodk said:
ÓÚ Tue, 19 Dec 2006 09:21:06 +0000£¬peteдµ½£º [...]
What somebody can learn from me, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
is that the original poster was wrong, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
and that you were wrong to suggest otherwise.
What somebody can learn from you,
is that I must select new suitable cases for answer.
That's right. It's my fault. And then,
please remeber u have not reply to the original poster until now:)
Whoever are you, please give the original poster valuable answer
rather than give me the unilateralism answer for my post:)
Thanks.

Learn to accept criticism.
 
R

Richard Heathfield

pete said:
iamgodk wrote:

What's the point of saying:
machine word is 32bit
sizeof(int) == 4
sizeof(char *) == 4
sizeof(int *) == 4
?

What is anybody supposed to be able to learn from that?

Not to trust iamgodk's advice about the C language. And that's a useful
lesson in itself.
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top