Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Size of a pointer

55 views
Skip to first unread message

Christian Wutscher Schneebrunzer

unread,
Aug 21, 2007, 5:29:24 PM8/21/07
to
What's the default size of a pointer?

I think 2 bytes are too short but how can i get the correct size from
within an application?

Thx.
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Keith Thompson

unread,
Sep 4, 2007, 1:08:59 PM9/4/07
to
Christian Wutscher Schneebrunzer <christian...@yesss.at> writes:
> What's the default size of a pointer?
>
> I think 2 bytes are too short but how can i get the correct size from
> within an application?

There is no "default size of a pointer". Different pointer types may
be of different sizes; for example, an int* might be bigger than a
char*, and a function pointer might be bigger than either. On most
modern implementations, all pointer types are the same size and use
the same underlying representation, but you shouldn't make that
assumption (and there's really no need to).

Use the sizeof operator to get the size of a pointer type (or, better,
of a pointer object).

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Hans-Bernhard Bröker

unread,
Sep 4, 2007, 1:09:16 PM9/4/07
to
Christian Wutscher Schneebrunzer wrote:
> What's the default size of a pointer?

Whatever the compiler deems suitable. In other words, there is no such
thing as a default size.

> I think 2 bytes are too short but how can i get the correct size from
> within an application?

The sizeof operator. And a serious reconsideration of whether you
*really* need to know it.

WillerZ

unread,
Sep 4, 2007, 1:09:46 PM9/4/07
to
Christian Wutscher Schneebrunzer wrote:
> What's the default size of a pointer?

sizeof(void *)

> I think 2 bytes are too short but how can i get the correct size from
> within an application?

sizeof(void *)

Gordon Burditt

unread,
Sep 4, 2007, 1:09:43 PM9/4/07
to
>What's the default size of a pointer?

Pointers have sizes, not default sizes. The size depends on the
compiler and the target platform. There is no guarantee that all
pointers will have the same size (particularly function pointers
vs. data pointers, which are, for example, different sizes from
each other in "middle" and "compact" memory models on MS-DOS.)

>I think 2 bytes are too short but how can i get the correct size from

C does *NOT* have a guarantee that a byte is 8 bits.

>within an application?

sizeof(void *)
sizeof(char *)
sizeof(int *)
sizeof(char **)
etc.

Jack Klein

unread,
Sep 4, 2007, 1:10:21 PM9/4/07
to
On 21 Aug 2007 21:29:24 GMT, Christian Wutscher Schneebrunzer
<christian...@yesss.at> wrote in comp.lang.c.moderated:

> What's the default size of a pointer?

What kind of pointer? Pointer to char and pointer to void have the
same size and representation. Pointers to any other object type can
be converted to pointer to void, and back again to pointer to the
original object type, without loss of information. So no pointer to
object may contain more information than pointer to void or char.

Pointers to functions may be completely different, and not convertible
with, pointers to any object type.

> I think 2 bytes are too short but how can i get the correct size from
> within an application?

There is no such thing as the default size of a pointer. It is
completely implementation-defined. And not all pointers are
necessarily the same size even on the same implementation.

It just so happens that I have worked with many implementations over
the years where a pointer to char is exactly 2 bytes in size:

1. In implementations for 8-bit processors, with 64K memory
addressing, where a 2-byte pointer can hold a 16-bit address.

2. In implementations for a DSP, where a 2-byte pointer holds a
32-bit address because char has 16 bits.

3. In some memory models for segmented architectures like the early
PC's running in 8086 real mode.

As for how you get the correct size, why do you think you need to?

If you define, for example:

char *char_pointer;

...the compiler will make it the correct size without your help.

If you want to know the size in bytes, do this:

printf("%lu\n", (unsigned long)sizeof(char *));

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

Jonathan Leffler

unread,
Sep 4, 2007, 1:10:19 PM9/4/07
to
Christian Wutscher Schneebrunzer wrote:
> What's the default size of a pointer?

What's the default size of a shoe?

It varies, by platform.

> I think 2 bytes are too short but how can i get the correct size from
> within an application?

sizeof(void *)?

--
Jonathan Leffler #include <disclaimer.h>
Email: jlef...@earthlink.net, jlef...@us.ibm.com
Guardian of DBD::Informix v2007.0226 -- http://dbi.perl.org/

Kenneth Brody

unread,
Sep 4, 2007, 1:10:25 PM9/4/07
to
Christian Wutscher Schneebrunzer wrote:
>
> What's the default size of a pointer?

Whatever the implementors of the compiler decide it is.

> I think 2 bytes are too short but how can i get the correct size from
> within an application?

Two bytes are just fine on some systems. But, if you need to find
the size of a pointer (or any other type), simply use sizeof, such
as:

x = sizeof(char *);

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsA...@gmail.com>

Douglas A. Gwyn

unread,
Sep 4, 2007, 1:11:59 PM9/4/07
to
Christian Wutscher Schneebrunzer wrote:
> What's the default size of a pointer?
> I think 2 bytes are too short but how can i get the correct size from
> within an application?

The size of a pointer object depends on the C implementation.
sizeof(any_object_or_object_type) evaluates to the number of
bytes in the object or object type. (If the argument is an
object, the parentheses are optional.) The odds are that
you shouldn't be writing code that depends on knowing this
size.

John Dallman

unread,
Sep 4, 2007, 1:12:05 PM9/4/07
to
In article <clcm-2007...@plethora.net>,
christian...@yesss.at (Christian Wutscher Schneebrunzer) wrote:

> What's the default size of a pointer?

This is entirely dependent on the platform you're running on. Do not
make any assumptions. In particular, pointers to different types are
allowed to be of different sizes. A void* pointer is required to be
convertible into any other kind of pointer, and that means, for
practical purposes, that it will be the largest kind of pointer if they
vary in size.

> I think 2 bytes are too short but how can i get the correct size from
> within an application?

printf( "A pointer occupies %d bytes\n", sizeof( void*));

--
John Dallman, j...@cix.co.uk, HTML mail is treated as probable spam.

Ravishankar S

unread,
Sep 4, 2007, 1:12:15 PM9/4/07
to
"Christian Wutscher Schneebrunzer" <christian...@yesss.at> wrote in
message news:clcm-2007...@plethora.net...

> What's the default size of a pointer?
>
> I think 2 bytes are too short but how can i get the correct size from
> within an application?
>
> Thx.
machine and compiler dependent. use sizeof(void*) on your machine/compiler
to get the correct size.

--Ravishankar

ardal...@gmail.com

unread,
Sep 4, 2007, 1:12:17 PM9/4/07
to
On Aug 22, 12:29 am, Christian Wutscher Schneebrunzer

<christian.wutsc...@yesss.at> wrote:
> What's the default size of a pointer?
>
> I think 2 bytes are too short but how can i get the correct size from
> within an application?
>
> Thx.
> --
> comp.lang.c.moderated - moderation address: c...@plethora.net -- you must

> have an appropriate newsgroups line in your header for your mail to be seen,
> or the newsgroup name in square brackets in the subject line. Sorry.

It depends on the system. In most cases, word length of the processor
(CPU) determines the size of a pointer. For instance, in the case of a
32-bit system, size of a pointer is 4 bytes. So actually we cannot
talk about the default size of a pointer, rather we can talk about the
size of a pointer for a certain system or architecture.
Anyway, for PCs of 32-bit architecture size of a pointer is 4-bytes,
or simply it can be printed with a simple line of code:
/* ..... */
printf("Size of a pointer: %u\n", sizeof(void *));
/* ..... */

Also size of a pointer determines the maximum memory that can be
addressed at once. That's why we cannot make a 32-bit PC work with
RAMs higher than 4 GB. (2^32 bytes = 4 GBytes)

Ahmet ARDAL

Barry Schwarz

unread,
Sep 4, 2007, 1:12:24 PM9/4/07
to
On 21 Aug 2007 21:29:24 GMT, Christian Wutscher Schneebrunzer
<christian...@yesss.at> wrote:

>What's the default size of a pointer?
>

There is no such thing. If there were, it would imply that you could
somehow specify a non-default size to use. For any given type, there
is only one size for a pointer to that type. There is no requirement
that pointers to different types have the same size (except in the
case of pointers to structures and unions).

>I think 2 bytes are too short but how can i get the correct size from
>within an application?

The same way you get the size of any object or type. Use the sizeof
operator. If variable X is a pointer to type T, then the expressions
sizeof X
and
sizeof (T*)
will both evaluate to the value you want.


Remove del for email

Matthias Buelow

unread,
Sep 4, 2007, 1:12:52 PM9/4/07
to
Christian Wutscher Schneebrunzer <christian...@yesss.at> wrote:

> I think 2 bytes are too short but how can i get the correct size from
> within an application?

For example, sizeof(char *).

Don Gingrich

unread,
Sep 4, 2007, 1:12:54 PM9/4/07
to
Christian Wutscher Schneebrunzer wrote:

> What's the default size of a pointer?
>
> I think 2 bytes are too short but how can i get the correct size
> from within an application?


use sizeof as in sizeof(* int)

I'm assuming that you want to use malloc or something
similar. (Note that all pointers should be the same
size, but the convention is to use the variable type
in the sizeof, just to be sure. As, for example:
struct foo_t *foo;

if ((foo = (struct foo_t *) malloc (10 * sizeof (struct foo_t *) ) ) == NULL)
{
error code
}

That would get you 10 pointers to struct foo_t where foo_t is a
structure type that you have defined elsewhere. Substitute int,
char, float, etc for the "struct foo_t" for pointers to other
variable types.

-Don

fre...@163.com

unread,
Sep 4, 2007, 1:13:06 PM9/4/07
to
On 8 22 , 5 29 , Christian Wutscher Schneebrunzer

<christian.wutsc...@yesss.at> wrote:
> What's the default size of a pointer?
>
> I think 2 bytes are too short but how can i get the correct size from
> within an application?
>
> Thx.
> --
> comp.lang.c.moderated - moderation address: c...@plethora.net -- you must

> have an appropriate newsgroups line in your header for your mail to be seen,
> or the newsgroup name in square brackets in the subject line. Sorry.

you can use sizeof to get the size of the pointer

for example:
char *p;
printf("%d\n", sizeof(p));

cham...@hotmail.com

unread,
Sep 4, 2007, 1:13:11 PM9/4/07
to
On 21 Aug, 22:29, Christian Wutscher Schneebrunzer

<christian.wutsc...@yesss.at> wrote:
> What's the default size of a pointer?
>
> I think 2 bytes are too short but how can i get the correct size from
> within an application?
>
> Thx.
> --
> comp.lang.c.moderated - moderation address: c...@plethora.net -- you must

> have an appropriate newsgroups line in your header for your mail to be seen,
> or the newsgroup name in square brackets in the subject line. Sorry.


Try

#define PTR_SIZE sizeof(void *)

ninj...@sina.com

unread,
Sep 4, 2007, 1:13:43 PM9/4/07
to
If you use a 32-bit system, it's 4 bytes.
It must be 8 bytes when come to a 64-bit system, I'm not sure about
this.

ta0...@yahoo.com

unread,
Sep 4, 2007, 1:13:47 PM9/4/07
to
On Aug 21, 5:29 pm, Christian Wutscher Schneebrunzer

<christian.wutsc...@yesss.at> wrote:
> What's the default size of a pointer?
>
> I think 2 bytes are too short but how can i get the correct size from
> within an application?
>
> Thx.
> --
> comp.lang.c.moderated - moderation address: c...@plethora.net -- you must

> have an appropriate newsgroups line in your header for your mail to be seen,
> or the newsgroup name in square brackets in the subject line. Sorry.


The default size is generally the register size of the OS. 32-bit
OSes normally have a 4B pointer and 64-bit normally have an 8B
pointer. You can check with sizeof(void*).
Kevin P. Barry

Paul D. DeRocco

unread,
Sep 4, 2007, 1:13:52 PM9/4/07
to
> "Christian Wutscher Schneebrunzer" <christian...@yesss.at> wrote
>
> What's the default size of a pointer?
>
> I think 2 bytes are too short but how can i get the correct size from
> within an application?

sizeof(void*) for a data pointer, sizeof(void(*)(void)) for a function
pointer.

--

Ciao, Paul D. DeRocco
Paul mailto:pder...@ix.netcom.com

Joel Yliluoma

unread,
Jan 12, 2008, 5:46:05 PM1/12/08
to
On 04 Sep 2007 17:08:59 GMT, Keith Thompson wrote:
> There is no "default size of a pointer". Different pointer types may
> be of different sizes; for example, an int* might be bigger than a
> char*.

Can that be true?

Suppose you have:
struct test
{
int a;
char b;
};

test tmp;

void iii()
{
int* aa = &tmp.a;
char* bb = &tmp.b;
}

Can those pointers really be of different size? What good would that do?
After all, "tmp" could be declared anywhere, and the location would affect
the requirements for the sizes of both pointers equally.


> and a function pointer might be bigger than either.

That I can understand though; functions cannot be relocated so it
is possible that they exist in a dedicated memory segment that must
be addressed differently from data.

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)

Keith Thompson

unread,
Jan 12, 2008, 8:04:49 PM1/12/08
to
Joel Yliluoma <bis...@iki.fi> writes:
> On 04 Sep 2007 17:08:59 GMT, Keith Thompson wrote:
>> There is no "default size of a pointer". Different pointer types may
>> be of different sizes; for example, an int* might be bigger than a
>> char*.
>
> Can that be true?

Yes (unless you can cite something in the standard that says it
can't).

[...]

> Can those pointers really be of different size? What good would that do?
> After all, "tmp" could be declared anywhere, and the location would affect
> the requirements for the sizes of both pointers equally.

The example I chose is unlikely to happen in real life; I probably
meant to say that a char* might be bigger than a char*, which is much
more plausible. It's true that you can convert an int* to a char*
without loss of information; the reverse is not necessarily true. So
this implies that a char* may contain more information than an int*
(for example, an int* might consist of a machine word pointer, while a
char* might contain a word pointer and a byte offset).

But it's still possible (in the sense that the standard doesn't forbid
it) that an int* could have "padding bits" that make it bigger than a
char*. (I used the quotation marks because the standard talks about
padding bits only for integer types.)

>> and a function pointer might be bigger than either.
>
> That I can understand though; functions cannot be relocated so it
> is possible that they exist in a dedicated memory segment that must
> be addressed differently from data.

And a function pointer might contain additional information. I think
the IBM AS/400 uses some kind of "descriptor" that can be
significantly bigger than an ordinary data pointer.

--
Keith Thompson (The_Other_Keith) <ks...@mib.org>

Nokia


"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

John Dallman

unread,
Jan 14, 2008, 1:49:47 PM1/14/08
to
In article <clcm-2008...@plethora.net>, bis...@iki.fi (Joel
Yliluoma) wrote:
> On 04 Sep 2007 17:08:59 GMT, Keith Thompson wrote:
> > There is no "default size of a pointer". Different pointer types
> > may be of different sizes; for example, an int* might be bigger than a
> > char*.
> Can that be true?

Absolutely. On many modern platforms, pointers to different types are
all the same size, but this is not required by C. It just makes life
simpler for all concerned.

Consider a machine with 32-bit words and four characters per word, but
whose instruction set uses word addresses rather than byte addresses. So
adding 1 to an address value changes it to address the next 32-bit word,
not the next byte. This allows the same size address field to handle
four times as much memory, but gives no direct way to address individual
bytes. A C char* thus needs to consist of an address, plus some kind of
indicator of which byte in the word is required. If C ints were 32 bits
on this machine, which would be plausible, then int* would also be 32
bits, while a char* would be at least 34 bits, and probably more to make
storing them reasonably simple. Addressing bytes on such machine can be
quite slow if they have to be extracted by masking and shifting.

That was, approximately, the Data General 32-bit Eclipse architecture,
whose history was described in _Soul of a New Machine_, and which was at
one time supported by the C-based product I work on. Some of the careful
and pedantic use of C necessary to survive this weirdness is still
visible in the code, although we haven't tried to compile for it for
many years, and would undoubtedly have to do a lot of tinkering to make
it work again. Most modern architectures use byte addresses, which
allows all pointers to be the same size.

--
John Dallman, j...@cix.co.uk, HTML mail is treated as probable spam.

Francis Glassborow

unread,
Feb 17, 2008, 1:03:48 PM2/17/08
to
John Dallman wrote:
> In article <clcm-2008...@plethora.net>, bis...@iki.fi (Joel
> Yliluoma) wrote:
>> On 04 Sep 2007 17:08:59 GMT, Keith Thompson wrote:
>>> There is no "default size of a pointer". Different pointer types
>>> may be of different sizes; for example, an int* might be bigger than a
>>> char*.
>> Can that be true?
>
> Absolutely. On many modern platforms, pointers to different types are
> all the same size, but this is not required by C. It just makes life
> simpler for all concerned.
>
> Consider a machine with 32-bit words and four characters per word, but
> whose instruction set uses word addresses rather than byte addresses. So
> adding 1 to an address value changes it to address the next 32-bit word,
> not the next byte. This allows the same size address field to handle
> four times as much memory, but gives no direct way to address individual
> bytes. A C char* thus needs to consist of an address, plus some kind of
> indicator of which byte in the word is required. If C ints were 32 bits
> on this machine, which would be plausible, then int* would also be 32
> bits, while a char* would be at least 34 bits, and probably more to make
> storing them reasonably simple. Addressing bytes on such machine can be
> quite slow if they have to be extracted by masking and shifting.
>

Well actually no. There is no connection between the number of bits in
an int and the number of bits in a pointer, even a pointer to an int.

Francis Glassborow

unread,
Feb 17, 2008, 1:03:51 PM2/17/08
to
John Dallman wrote:

> That was, approximately, the Data General 32-bit Eclipse architecture,
> whose history was described in _Soul of a New Machine_, and which was at
> one time supported by the C-based product I work on. Some of the careful
> and pedantic use of C necessary to survive this weirdness is still
> visible in the code, although we haven't tried to compile for it for
> many years, and would undoubtedly have to do a lot of tinkering to make
> it work again. Most modern architectures use byte addresses, which
> allows all pointers to be the same size.
>

Then I guess you don't program for a DSP unless you are content with
32-bit chars to go with your 32-bit ints.

Kenneth Brody

unread,
Feb 17, 2008, 1:04:10 PM2/17/08
to
Keith Thompson wrote:
>
> Joel Yliluoma <bis...@iki.fi> writes:
> > On 04 Sep 2007 17:08:59 GMT, Keith Thompson wrote:
[...]

> >> and a function pointer might be bigger than either.
> >
> > That I can understand though; functions cannot be relocated so it
> > is possible that they exist in a dedicated memory segment that must
> > be addressed differently from data.
>
> And a function pointer might contain additional information. I think
> the IBM AS/400 uses some kind of "descriptor" that can be
> significantly bigger than an ordinary data pointer.

And a data pointer can be bigger than a function pointer. Consider
the old 16-bit "real mode" x86 family, where C compilers had different
memory models (tiny, compact, small, medium, large, and huge) in which
different sizes were used for different pointers. I forget which one,
but it was either compact or medium, which used 16-bit pointers for
functions and 32-but pointers for data. (And all pointers could be
explicitly changed to 16- or 32-bits with the use of [non-Standard,
as far as clc is concerned, but "standard" in the DOS world] "near"
and "far" modifiers.) I believe I have seen "far" pointers still
available in some systems, which make the pointers 48 bits.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsA...@gmail.com>

Kenneth Brody

unread,
Feb 17, 2008, 1:04:12 PM2/17/08
to
John Dallman wrote:
>
> In article <clcm-2008...@plethora.net>, bis...@iki.fi (Joel
> Yliluoma) wrote:
> > On 04 Sep 2007 17:08:59 GMT, Keith Thompson wrote:
> > > There is no "default size of a pointer". Different pointer types
> > > may be of different sizes; for example, an int* might be bigger than a
> > > char*.
> > Can that be true?
>
> Absolutely. On many modern platforms, pointers to different types are
> all the same size, but this is not required by C. It just makes life
> simpler for all concerned.
>
> Consider a machine with 32-bit words and four characters per word, but
> whose instruction set uses word addresses rather than byte addresses. So
> adding 1 to an address value changes it to address the next 32-bit word,
> not the next byte. This allows the same size address field to handle
> four times as much memory, but gives no direct way to address individual
> bytes. A C char* thus needs to consist of an address, plus some kind of
> indicator of which byte in the word is required.
[...]

I've used such a beast (a DEC KL-10), but this was before I ever heard
of C, so I can't say how a C compiler would handle it.

In any case, the system had 36 bit "words", and 18-bit addresses. You
could also pick the size of your characters. For example, filenames
were stored as 6-bit characters, allowing 6.3 filenames consisting of
the ASCII range 0x20-0x5f. The FORTRAN compiler they had used 7-bit
characters (ASCII 0x00-0x7f). And so on.

I have no idea what a C compiler would do, though I suspect it might
pack four 8-bit chars into was 36-bit "word".

In any case, you could use 18 bits to address any "word" in the
system, but you needed 36 bits to address something smaller.

However, given the rules of C, where sizeof(char)==1, I don't know
how this would be handled specifically. Perhaps a char would be 9
bits (still packing four per "word", but now with nothing left over),
and each "word" would be treated as if it were a sizeof 4, with the
compiler hiding all of the nastiness from the developer? Or maybe
it would simplify everything, and just use 36-bit chars?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsA...@gmail.com>

John Dallman

unread,
Mar 8, 2008, 1:19:42 PM3/8/08
to
In article <clcm-2008...@plethora.net>, kenb...@spamcop.net
(Kenneth Brody) wrote:

> I've used such a beast (a DEC KL-10), but this was before I ever heard
> of C, so I can't say how a C compiler would handle it.
>
> In any case, the system had 36 bit "words", and 18-bit addresses. You
> could also pick the size of your characters. For example, filenames
> were stored as 6-bit characters, allowing 6.3 filenames consisting of
> the ASCII range 0x20-0x5f. The FORTRAN compiler they had used 7-bit
> characters (ASCII 0x00-0x7f). And so on.
>
> I have no idea what a C compiler would do, though I suspect it might
> pack four 8-bit chars into was 36-bit "word".

C is not wedded to 8-bit characters. They're just commonly used, being
the default on many of the machines it runs on.

> In any case, you could use 18 bits to address any "word" in the
> system, but you needed 36 bits to address something smaller.
>
> However, given the rules of C, where sizeof(char)==1, I don't know
> how this would be handled specifically. Perhaps a char would be 9
> bits (still packing four per "word", but now with nothing left over),
> and each "word" would be treated as if it were a sizeof 4, with the
> compiler hiding all of the nastiness from the developer? Or maybe
> it would simplify everything, and just use 36-bit chars?

9-bit characters sounds the most sensible for this machine, but it's a
decision that would be made by someone doing a C implementation, on the
basis of a lot more thought than my reading your posting.

--
John Dallman, j...@cix.co.uk, HTML mail is treated as probable spam.

John Dallman

unread,
Mar 8, 2008, 1:19:47 PM3/8/08
to
In article <clcm-2008...@plethora.net>,
francis.g...@btinternet.com (Francis Glassborow) wrote:

> John Dallman wrote:
> > Absolutely. On many modern platforms, pointers to different types
> > are all the same size, but this is not required by C. It just makes
> > life simpler for all concerned. > > Consider a machine with 32-bit
> > > > words and four characters per word, but whose instruction set
> > uses word addresses rather than byte addresses. So adding 1 to an
> > address value changes it to address the next 32-bit word, not the
> > next byte. This allows the same size address field to handle four
> > times as much memory, but gives no direct way to address individual
> > bytes. A C char* thus needs to consist of an address, plus some
> > kind of indicator of which byte in the word is required. If C ints
> > were 32 bits on this machine, which would be plausible, then int*
> > would also be 32 bits, while a char* would be at least 34 bits, and
> > probably more to make storing them reasonably simple. Addressing
> > bytes on such machine can be quite slow if they have to be
> > extracted by masking and shifting.
>
> Well actually no. There is no connection between the number of bits
> in an int and the number of bits in a pointer, even a pointer to an
> int.

Insufficiently careful writing allowing incorrect inference.

Given a machine with 32-bit words that uses word addresses, and
addresses 2^32 words, an easy and natural size for an int is a word. The
easy and natural size for an int* is also a word, but this is not
because an int "needs" to be the same size as an int* in any global
way. If you pack four characters per word, then a char* needs more than
a word of storage to allow you to identify a character within a word,
assuming you want to store characters anywhere in memory.

No, I have never worked with a DSP; they wouldn't be very relevant to
the code I mentioned upthread.

--
John Dallman, j...@cix.co.uk, HTML mail is treated as probable spam.

0 new messages