size of a sizeof(pointer)

S

syntax

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

Josh Sebastian

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
 
M

Malcolm

syntax said:
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.
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.
 
R

Richard Heathfield

Josh said:
Maybe. It must be >= 2.

Wrong. It must, however, be an exact multiple of 1.
sizeof(char) is, by definition, 1.
Right.


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

Keith Thompson

Josh Sebastian said:
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.
 
M

Mike Wahler

Keith Thompson said:
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
 
K

Keith Thompson

Malcolm said:
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.
 
M

Malcolm

Keith Thompson said:
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.
 
C

CBFalconer

Malcolm said:
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.
 
J

Jack Klein

Maybe. It must be >= 2.

No, the number of bits in an int must be at least 16, but on some
platforms CHAR_BIT is greater than 8.

I am actually developing code right now on for a Texas Instruments
2812 DSP with their Code Composer Studio. CHAR_BIT is 16. The types
char, signed char, unsigned char, signed short, unsigned short, signed
int and unsigned int all contain 16 bits and the sizeof operator
yields a value of 1 for each and every one of these types. The
processor only reads and writes memory in 16 bit words.

In the past I have worked with a 32 bit DSP from Analog devices which
only addressed memory in 32 bit words. CHAR_BIT was 32. All the
integer types (this was before C99, so there was no long long type)
had 32 bits and sizeof yielded 1, even for signed and unsigned long.
sizeof(char) is, by definition, 1.

This is true.
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).

And often it is not. Under the TI compiler I mentioned above, while
int has 16 bits and sizeof(int) is, pointers have 32 bits and
sizeof(void *) is 2.

Under Keil's compiler for the 8051, int has 16 bits and occupies 2
bytes, sizeof(void *) is 3, although it doesn't really use all 24
bits.
 
G

Grumble

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

An implementation cannot have 16-bit chars and 24-bit ints?

How about 16-bit chars and 24-bit pointers?
 
P

pete

It must be greater than 1, on hosted implementations.
An implementation cannot have 16-bit chars and 24-bit ints?

The sum of the numbers of padding bits,
value bits and the sign bit, is a multiple of CHAR_BIT.
How about 16-bit chars and 24-bit pointers?

The bit representation of pointers is not specified.
 
R

Richard Bos

pete said:
It must be greater than 1, on hosted implementations.

Chapter and verse, please.

Of course, it's exceedingly awkward for a hosted implementation to have
sizeof(int)==1, but it isn't illegal.
The sum of the numbers of padding bits,
value bits and the sign bit, is a multiple of CHAR_BIT.


The bit representation of pointers is not specified.

Even so, all types have sizes measurable in whole chars; look up the
definition of sizeof.

Richard
 
R

Richard Bos

Grumble said:
Richard said:
Of course, it's exceedingly awkward for a hosted implementation
to have sizeof(int)==1 [...]

Is it awkward because getc() can return either a char or EOF?

That, and related problems, yes. If you need to take these legal-but-
unlikely implementations into account (i.e., if you really want to be as
anal-retentive about ISO-conformance as your common huff-throwing newbie
(and uncommon troll) makes us out to be), you need to check for feof()
and ferror() after every read operation, instead of simply for EOF.
Personally, I never do.

Richard
 
M

Mark A. Odell

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

Indeed. For example, Keil C51 has 1 byte, 2 byte, and 3 byte pointer sizes
depending upon which memory space the pointer points to.
 
G

Grumble

Richard said:
Of course, it's exceedingly awkward for a hosted implementation
to have sizeof(int)==1 [...]

Is it awkward because getc() can return either a char or EOF?
 
P

Papadopoulos Giannis

Mark said:
Not if a char were 16 bits wide.

Is there any alive implementation that uses 16bit chars?? (I know of the
existance of a machine that a byte is 6-bit)

--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v?p(Little):p(Big);return 0;}

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top