Velocity Reviews > size of a sizeof(pointer)

size of a sizeof(pointer)

syntax
Guest
Posts: n/a

 02-08-2004
what is the size of a pointer?

suppose i am writing,

datatype *ptr;
sizeof(ptr);

now what does this sizeof(ptr) will give? will it give the size of the
data the pointer is pointing to?

if no, can you give an counter example?

basically , i want to know what is the meaning of size of a ponter.

as you know

sizeof(int)=4;

sizeof(char)= 2;

but what does sizeof(ptr) means??

can anybody explain?

Josh Sebastian
Guest
Posts: n/a

 02-08-2004
On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:

> what is the size of a pointer?
>
> suppose i am writing,
>
>
> datatype *ptr;
> sizeof(ptr);
>
>
> now what does this sizeof(ptr) will give? will it give the size of the
> data the pointer is pointing to?
>
> if no, can you give an counter example?
>
> basically , i want to know what is the meaning of size of a ponter.
>
> as you know
>
> sizeof(int)=4;

Maybe. It must be >= 2.

> sizeof(char)= 2;

sizeof(char) is, by definition, 1.

> but what does sizeof(ptr) means??

It's the amount of space the pointer itself takes up. Not the data pointed
to, but the pointer itself. Often, it's == sizeof(int).

Josh

Malcolm
Guest
Posts: n/a

 02-08-2004

"syntax" <(E-Mail Removed)> wrote in message
> what is the size of a pointer?
>

A pointer is a variable that holds an address. The size of a pointer is the
For instance, most computers have an address space of 4GB. 32 bits allows
you 4GB, so the size of a pointer will be 32 bits, or 4 (char is usually 8
bits). On some microcomputers the address space is only 64K, so 16-bit
pointers are used.
>
> datatype *ptr;
> sizeof(ptr);
>
> now what does this sizeof(ptr) will give? will it give the size of the
> data the pointer is pointing to?
>

No, it gives the size of the pointer, probably 4.
>
> if no, can you give an counter example?
>

One confusing thing about C is that arrays and pointer have array/pointer
equivalence.

char string[32];

printf("sizeof string %d\n", (int) sizeof(string));

will give you 32.

char *string = malloc(32);

printf(" sizeof string %d\n", (int) sizeof(string));

will give you the size of a pointer on your system, probably 4.
>
> basically , i want to know what is the meaning of size of a ponter.
>
> as you know
>
> sizeof(int)=4;
>
> sizeof(char)= 2;
>

sizeof(char) is always 1, one of the little quirks of the C language.
sizeof(int) is very commonly 4, but it can be any size. It is meant to be
the natural size for the machine to use, which means the width of the
register.
For technical reasons pointers are usually the same size as ints, but again
they can be any size.
>
> but what does sizeof(ptr) means??
>

Richard Heathfield
Guest
Posts: n/a

 02-08-2004
Josh Sebastian wrote:

> On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:
>
>> as you know
>>
>> sizeof(int)=4;

>
> Maybe. It must be >= 2.

Wrong. It must, however, be an exact multiple of 1.

>> sizeof(char)= 2;

>
> sizeof(char) is, by definition, 1.

Right.

>
>> but what does sizeof(ptr) means??

>
> It's the amount of space the pointer itself takes up. Not the data pointed
> to, but the pointer itself. Often, it's == sizeof(int).

But, of course, it doesn't have to be (as you know).

--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Josh Sebastian
Guest
Posts: n/a

 02-08-2004
On Sun, 08 Feb 2004 19:58:20 +0000, Richard Heathfield wrote:

> Josh Sebastian wrote:
>
>> On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:
>>
>>> as you know
>>>
>>> sizeof(int)=4;

>>
>> Maybe. It must be >= 2.

>
> Wrong. It must, however, be an exact multiple of 1.

Jeez... yeah, thanks.

Keith Thompson
Guest
Posts: n/a

 02-08-2004
Josh Sebastian <(E-Mail Removed)> writes:
> On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:

[...]
> > but what does sizeof(ptr) means??

>
> It's the amount of space the pointer itself takes up. Not the data pointed
> to, but the pointer itself. Often, it's == sizeof(int).

It's true that the size of a pointer is often equal to sizeof(int),
but it's dangerous (an unnecessary) to assume that it always is.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"

Mike Wahler
Guest
Posts: n/a

 02-08-2004

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Josh Sebastian <(E-Mail Removed)> writes:
> > On Sun, 08 Feb 2004 11:37:15 -0800, syntax wrote:

> [...]
> > > but what does sizeof(ptr) means??

> >
> > It's the amount of space the pointer itself takes up. Not the data

pointed
> > to, but the pointer itself. Often, it's == sizeof(int).

>
> It's true that the size of a pointer is often equal to sizeof(int),
> but it's dangerous (an unnecessary) to assume that it always is.

Or for that matter, to assume that all pointer types have the same size.

-Mike

Keith Thompson
Guest
Posts: n/a

 02-08-2004
"Malcolm" <(E-Mail Removed)> writes:
[...]
> One confusing thing about C is that arrays and pointer have array/pointer
> equivalence.

No, there is no array/pointer equivalence (or rather, "equivalence" is
a misleading term for what's really going on). Array names are
implicitly converted to pointer values in many contexts.

See the C FAQ at <http://www.eskimo.com/~scs/C-faq/faq.html>,
particularly section 6, particularly question 6.3.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"

Malcolm
Guest
Posts: n/a

 02-08-2004

"Keith Thompson" <(E-Mail Removed)> wrote in message
>
> No, there is no array/pointer equivalence (or rather, "equivalence" is
> a misleading term for what's really going on). Array names are
> implicitly converted to pointer values in many contexts.
>
> See the C FAQ at <http://www.eskimo.com/~scs/C-faq/faq.html>,
> particularly section 6, particularly question 6.3.
>

Exactly. "Equivalence" is the accepted term for what is going on, which is
confusing.

CBFalconer
Guest
Posts: n/a

 02-09-2004
Malcolm wrote:
> "syntax" <(E-Mail Removed)> wrote in message
>
> > what is the size of a pointer?
> >

> A pointer is a variable that holds an address. The size of a
> pointer is the size of this address.
>
> For instance, most computers have an address space of 4GB. 32
> bits allows you 4GB, so the size of a pointer will be 32 bits,
> or 4 (char is usually 8 bits). On some microcomputers the
> address space is only 64K, so 16-bit pointers are used.

Nope. A pointer points. What information it needs to hold to do
that is up to the implementation. It could consist of a URL and
other information, just as a not too wild example. Another might
be "Malcolms house, under the bed beside the dirty socks, last
Tuesday". The amount of information needed is usually constrained
by limiting the things that the pointer is allowed to point to.
Clear now?

At any rate the C expression "sizeof ptr", where ptr is an actual
pointer, is available to tell you how much space that particular
implementation needs for the job.

Sometimes that pointer may be a real memory address. Today it
more often represents an offset from another pointer which points
to a block of some sort of storage. You should neither know nor
care, unless you are implementing the system.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.