Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > size of pointer in C?

Reply
Thread Tools

size of pointer in C?

 
 
Dik T. Winter
Guest
Posts: n/a
 
      09-29-2005
In article <(E-Mail Removed) .com> "siliconwafer" <(E-Mail Removed)> writes:
> does it make any difference that size of pointer to char is different
> than size of pointer to say long int?
> A pointer stores address and address is integer or long int.So size of
> pointer *must* be either 2 or 4


Wrong. An address is not necessarily an integer. It can contain things
like ring number, segment number, address within segment, byte number
within a word and a host of other things.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
 
Reply With Quote
 
 
 
 
Clark S. Cox III
Guest
Posts: n/a
 
      09-29-2005
On 2005-09-29 08:38:44 -0400, "siliconwafer" <(E-Mail Removed)> said:

> Hi Chris,
> does it make any difference that size of pointer to char is different
> than size of pointer to say long int?


There is nothing wrong with a platform where sizeof(char*) != sizeof(int*).

> A pointer stores address and address is integer or long int.


Who says that a pointer must be an integer? The standard certainly doesn't

> So size of
> pointer *must* be either 2 or 4


That's odd, considering that I'm working on a platform at this very
moment where
(sizeof(void*) == .


--
Clark S. Cox, III
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      09-29-2005
"Clark S. Cox III" wrote:
>
> On 2005-09-29 08:38:44 -0400, "siliconwafer" <(E-Mail Removed)> said:

[...]
> > So size of
> > pointer *must* be either 2 or 4

>
> That's odd, considering that I'm working on a platform at this very
> moment where
> (sizeof(void*) == .


Although I never worked in C on this platform, the computer I used at
college had 36-bit words and 18-bit addresses. Although there were
opcodes for accessing sub-words, the smallest unit of storage was 36
bits.

I have no idea what "sizeof(void *)" would be on such a platform, but
it is quite possible that it would be "1".

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-29-2005
siliconwafer wrote:
> siliconwafer wrote:
>
>>What is size of pointer in C on DOS?
>>is it sizeof(int ) or size of (long int)?
>>If this ans is present in FAQ pls direct me to tht ouestion

>
>
> Lets take an example:
> char*str = (char*)malloc(1024);
> printf("%d",sizeof(str));
> what should get printed?


Someone who can write
printf("%d",sizeof(str));
has no business worrying about the size of a pointer.
The value from sizeof(str) is an unsigned integer that may be wider than
an int (and often is). "%d" expects a signed value, and so is wrong, and
an integral object no larger than an int, and is wrong a second time.

Also, writing
char*str = (char*)malloc(1024);
strongly suggests a failure to include <stdlib.h>, since there is no
excuse for the (char *) cast.

It would probably be better to concentrate on learning the basics of
writing C programs instead of trying to look under the hood at details
you are not ready to explore and need not be the same on the next model
implementation you use.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-29-2005
siliconwafer wrote:
> Hi Chris,
> does it make any difference that size of pointer to char is different
> than size of pointer to say long int?
> A pointer stores address and address is integer or long int.


Wrong. You premise is false.
> So size of
> pointer *must* be either 2 or 4


Wrong. Not only is your premise wrong, buteven if it were true there is
no logical connection to your conclusion. A non-sequitor based on false
premises is hardly worth considering.

> -Siliconwafer
>

 
Reply With Quote
 
Serge Paccalin
Guest
Posts: n/a
 
      09-29-2005
Le jeudi 29 septembre 2005 à 14:38:44, siliconwafer a écrit dans
comp.lang.c*:

> A pointer stores address and address is integer or long int.


Bullshit. Remember MS-DOS where pointers were often two ints: a 16-bit
segment and a 16-bit offset.

> So size of pointer *must* be either 2 or 4


More bullshit. In Win64 programming:

sizeof (int) == sizeof (long) == 4 [32 bits]
sizeof (void *) == 8 [64 bits]

--
___________ 29/09/2005 18:28:31
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-29-2005
"siliconwafer" <(E-Mail Removed)> writes:
> siliconwafer wrote:
>> What is size of pointer in C on DOS?
>> is it sizeof(int ) or size of (long int)?
>> If this ans is present in FAQ pls direct me to tht ouestion

>
> Lets take an example:
> char*str = (char*)malloc(1024);
> printf("%d",sizeof(str));
> what should get printed?


Let's fix your example:

char *str = malloc(1024);
printf("%d\n", (int)sizeof str);

The sizeof operator does not evaluate its operand; all it does is
yield its size in bytes. (There is an exception for VLAs
(variable-length arrays), but that's not relevant here.) So the fact
that you initialized str has no effect on the behavior of the code.
(You should normally check whether the malloc() succeeded, but that
doesn't matter here since you're not using the result.) So:

char *str;
printf("%d\n", (int)sizeof str);

The expression "sizeof str" yields the size in bytes of str, which is
the same as sizeof(char*). The standard says very little about what
this value will be. It's guaranteed to be at least 1; on some
systems, it can be 1. Certain other requirements imply that it needs
to be at least 16 bits in a hosted environment, so sizeof(char*)==1
implies CHAR_BIT>=16. If you don't understand these details yet,
don't worry about it.

There is very little guaranteed relationship between integers and
pointers. They are two entirely different things. A C pointer is
*not* some kind of integer in disguise; it's just a pointer. Its
value refers to some memory location, but the manner in which it does
so is implementation-specific. Different kinds of pointers can have
different sizes (though they tend to be the same on most systems).
You can convert integers to pointers and vice versa, but the results
of doing so are system-specific.

A pointer is not necessarily the same size as any particular integer
type; a system where pointers are 128 bits and the largest integer
type is 64 bits would be perfectly legal.

Pointers point to things. Forget everything else you think you know
about them, and start from there. Re-read the section on pointers in
your C textbook; if you don't have one, K&R2 is excellent. Read
section 4 of the C FAQ.

On a typical modern system, CHAR_BIT (the number of bits in a byte) is
likely to be 8, int is likely to be 32 bits, long is likely to be
either 32 or 64 bits, and pointers are likely to be 32 or 64 bits.
Converting a pointer to an unsigned integer of the same size is likely
to yield something that looks meaningful if you know anything about
the underlying memory addressing of the system. But the C standard is
designed to allow for exceptions to all of these things. You can
write useful code that doesn't depend on any of these assumptions, and
it will work everywhere, not just on "typical" systems.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      09-30-2005
siliconwafer wrote on 29/09/05 :
> What is size of pointer in C on DOS?
> is it sizeof(int ) or size of (long int)?


size_t size_of_a_pointer_of_type_T = sizeof (T*);

Out of this, you are not portable.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      09-30-2005
siliconwafer wrote on 29/09/05 :
> siliconwafer wrote:
>> What is size of pointer in C on DOS?
>> is it sizeof(int ) or size of (long int)?
>> If this ans is present in FAQ pls direct me to tht ouestion

>
> Lets take an example:
> char*str = (char*)malloc(1024);


Useless for the example.

char *str;

> printf("%d",sizeof(str));


printf ("%u\n", (unsigned) sizeof str);

BTW, you just need the type...

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

> what should get printed?


Who knows, it's implementation-dependent.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair


 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      09-30-2005

siliconwafer wrote:
> What is size of pointer in C on DOS?
> is it sizeof(int ) or size of (long int)?
> If this ans is present in FAQ pls direct me to tht ouestion


The quickest way to find out what pointer sizes are on your particular
implementation:

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

etc.

Note that pointers to different types may be different sizes, and may
be different between implementations.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments