Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   size of a sizeof(pointer) (http://www.velocityreviews.com/forums/t317266-size-of-a-sizeof-pointer.html)

 syntax 02-08-2004 07:37 PM

size of a sizeof(pointer)

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 02-08-2004 07:40 PM

Re: size of a sizeof(pointer)

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 02-08-2004 07:53 PM

Re: size of a sizeof(pointer)

"syntax" <sanko50@yahoo.com.hk> 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 02-08-2004 07:58 PM

Re: size of a sizeof(pointer)

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 : binary@eton.powernet.co.uk
"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 02-08-2004 08:29 PM

Re: size of a sizeof(pointer)

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 02-08-2004 08:41 PM

Re: size of a sizeof(pointer)

Josh Sebastian <curien@cox.net> 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) kst-u@mib.org <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 02-08-2004 09:19 PM

Re: size of a sizeof(pointer)

"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnfzdlcnhn.fsf@nuthaus.mib.org...
> Josh Sebastian <curien@cox.net> 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 02-08-2004 10:01 PM

Re: size of a sizeof(pointer)

"Malcolm" <malcolm@55bank.freeserve.co.uk> 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) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"

 Malcolm 02-08-2004 10:04 PM

Re: size of a sizeof(pointer)

"Keith Thompson" <kst-u@mib.org> 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 02-09-2004 02:00 AM

Re: size of a sizeof(pointer)

Malcolm wrote:
> "syntax" <sanko50@yahoo.com.hk> 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 (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.