Velocity Reviews

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

siliconwafer 09-29-2005 09:18 AM

size of pointer in C?
 
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


Giannis Papadopoulos 09-29-2005 09:30 AM

Re: size of pointer in C?
 
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


It is sizeof(void*)...
It may be sizeof(void*)==sizeof(long), but it may not be as well.
Whatever depends on the conditional above is considered not portable.


--
one's freedom stops where others' begin

Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.users.uth.gr

pete 09-29-2005 10:52 AM

Re: size of pointer in C?
 
Giannis Papadopoulos 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

>
> It is sizeof(void*)


Pointers to void are the same size as pointers to char.

Pointers to structures are the same size as each other.

The sizes of most other types of pointers
have no special specifications.

--
pete

Chris Hills 09-29-2005 11:00 AM

Re: size of pointer in C?
 
In article <1127985513.958264.171150@o13g2000cwo.googlegroups .com>,
siliconwafer <spdandavate@yahoo.com> writes
>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


plz wrt in engsh

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ chris@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/




siliconwafer 09-29-2005 12:12 PM

Re: size of pointer in C?
 

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?


Chris Dollin 09-29-2005 12:21 PM

Re: size of pointer in C?
 
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?


(a) Anything. You have undefined behaviour there. (Clue: what type
does %d expect?)

(b) The sizeof a pointer-to-char in your implementation. Possibilities
include 2, 4, 8, 3, 1, and 17.

--
Chris "Dragaeran software written in C?!" Dollin
The software engineer's song: "who knows where the time goes".

siliconwafer 09-29-2005 12:38 PM

Re: size of pointer in C?
 
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.So size of
pointer *must* be either 2 or 4
-Siliconwafer


Chris Dollin 09-29-2005 12:58 PM

Re: size of pointer in C?
 
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?


Is it? It need not be.

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


That depends on the implementation. I imagine a DOS compiler is
free to choose whatever it finds convenient.

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


If you say so. Such an answer isn't required by the Standard, though.

It is wise to arrange that your code doesn't care.

--
Chris "electric hedgehog" Dollin
The software engineer's song: "who knows where the time goes".

Lawrence Kirby 09-29-2005 01:03 PM

Re: size of pointer in C?
 
On Thu, 29 Sep 2005 02:18:33 -0700, siliconwafer wrote:

> What is size of pointer in C on DOS?


Whatever the compiler chooses to make it. For most DOS compilers that
depends on the memory model you use to compile. However memory models are
a DOSism or at least processor related, and not part of standard C. A good
place to discuss this is comp.os.msdos.programmer.

> is it sizeof(int ) or
> size of (long int)? If this ans is present in FAQ
> pls direct me to tht ouestion


So it could be either depending on what compiler and compiler options you
use. As far as writing C code is concerned just write you program so that
it wirks either way, that is without making assumptions about pointer size
and representation.

Lawrence

Flash Gordon 09-29-2005 01:11 PM

Re: size of pointer in C?
 
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);


Don't cast the return value of malloc, it's not required.

> printf("%d",sizeof(str));
> what should get printed?


Anything or nothing since the result of sizeof is size_t which is *not*
int. If you correct all the bugs and produce a conforming C program then
the answer, al others have already stated, is, "whatever the implementer
decided the size of a char* pointer should be." On DOS that is *likely*
to be either 2 or 4 and will probably depend on the options you provide
the compiler.

So, once again, don't write SW that depends on knowing the size of a
pointer.

I believe a corrected program is:

#include <stdio.h> /* Required for printf */
/* If you use malloc, which I don't, you need to include stdlib.h */

int main(void)
{
char *ptr;
/* No need to assign anything since we just want the size of the
pointer */
printf("%lu\n",(unsigned long)sizeof ptr);
/* You get 0 on the Deathstation 9000 in super large pointer
mode where the size of a pointer is one larger than can be
represented in an unsigned long and size_t has a larger range
than unsigned long. */
return 0;
}
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


All times are GMT. The time now is 12:10 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.