Long int as big as pointer? 
Author Message
 Long int as big as pointer?

Please tell me if I am guaranteed in ANSI C that
sizeof(long int) >= sizeof(char *)

Thanks.



Fri, 27 Sep 1996 13:27:54 GMT  
 Long int as big as pointer?

Quote:

>>Please tell me if I am guaranteed in ANSI C that
>>sizeof(long int) >= sizeof(char *)

>You are guaranteed that *some* integer type is large enough to hold a
>pointer, but you aren't guaranteed *which* type will.  OTOH, it's a
>pretty safe bet.

>--
>#include    <standard.disclaimer>
> _
>Kevin D Quitt 91351-4454            96.37% of all statistics are made up

No, you're not guaranteed.  TOTALLY dependant on architecture.
Ex. The AS/400: sizeof(char *)== 16 bytes, sizeof(long int) == 4 bytes.
    Think about future architectures, then think if you'll ever need to run on them.
    if not, code away... 8-)

------------------------------------------------------------------------------
Fred A. Kulack



t/l: 553.5982                (CompuServe          : 71163,3065)

Team OS/2 is for EVERYONE! - Bill Gates.    ;-)



Sat, 28 Sep 1996 10:30:53 GMT  
 Long int as big as pointer?


Quote:

>>Please tell me if I am guaranteed in ANSI C that
>>sizeof(long int) >= sizeof(char *)

>You are guaranteed that *some* integer type is large enough to hold a
>pointer, but you aren't guaranteed *which* type will.  

No, you are not guaranteed this.  Please read sections 6.1.2.5 and
6.3.4 of the ISO standard.

Quote:
>OTOH, it's a pretty safe bet.

I'm not going to disagree with this for char *, a particular case of
pointer to object, but I will point out that for pointers to functions
it pretty unsafe on several common architectures.

--
Alan Watson                        | Assembly: You try to shoot yourself in

Department of Astronomy            | reinvent the gun, the bullet, and your
University of Wisconsin -- Madison | foot. -- Anon.



Sat, 28 Sep 1996 05:13:46 GMT  
 Long int as big as pointer?

Quote:

>Please tell me if I am guaranteed in ANSI C that
>sizeof(long int) >= sizeof(char *)

No.  Even if there was such a guarantee, what good would it do you?  You
wouldn't be able to use the information in a portable way so it would
have no relevance to an ANSI conforming program.

--

PlanIX, Inc.                        |   There's no government
Toronto, Ontario, Canada            |   like no government!
+1 416 424 2871  (DoD#0082) (eNTP)  |



Sat, 28 Sep 1996 19:17:38 GMT  
 Long int as big as pointer?
|> Thanks for all your help!
|>
|> OK, so I'm not *guaranteed* that
|>   sizeof(char *) <= sizeof(long int).
|>
|> I'm trying to write a linked list module that is as portable as possible, and
|> which uses very generalized data.  Currently it uses a long int for the data,
|> and if your data is bigger you use a pointer to your (previously allocated)
|> data (which the module casts to a long int and uses as normal).  I did this
|> instead of using a simple pointer so if your data is small, the code is fast.
|>
|> This obviously is not universally portable.  How many machines would it compile
|> on, though?  Most?  Many? Some?
|>
|> And is there another way?  Or should I just make *all* users send me pointers
|> to their data?

Would this fit your needs?

   typedef struct listrec {
     struct listrec *next;
     union {
       long int fast;
       void *slow;
     } data;
   } listrec, *list;

--
                                       ^^

University of Twente                 =x=  \        tel. +31 53 893747
Tele-Informatics & Open Systems        |   \       tfx. +31 53 333815
P.O. Box 217   7500 AE Enschede       /|__  \
The Netherlands                      (____)_/



Sat, 28 Sep 1996 19:19:40 GMT  
 Long int as big as pointer?

Quote:

>>Please tell me if I am guaranteed in ANSI C that
>>sizeof(long int) >= sizeof(char *)

>You are guaranteed that *some* integer type is large enough to hold a
>pointer, but you aren't guaranteed *which* type will.  OTOH, it's a
>pretty safe bet.

No, you're only guaranteed that there is some integral type to which you can
cast a pointer, and you're not guaranteed that the original pointer value
is recoverable from the result of that cast. In other words, there is no
guarantee whatsoever about the relative sizes of pointers and longs.

There aren't many machines about where sizeof(long) < sizeof(char*), but it's
not an unreasonable situation for new architectures. Look at why you want to
make the assumption, how you could work around it, and what that would cost.
--

Thinkage, Ltd.           Kitchener, Ontario, Canada          [43.40N 80.47W]



Sun, 29 Sep 1996 00:37:51 GMT  
 Long int as big as pointer?

Quote:
>Please tell me if I am guaranteed in ANSI C that
>sizeof(long int) >= sizeof(char *)

You are guaranteed that *some* integer type is large enough to hold a
pointer, but you aren't guaranteed *which* type will.  OTOH, it's a
pretty safe bet.

--
#include        <standard.disclaimer>
 _
Kevin D Quitt 91351-4454                96.37% of all statistics are made up



Sat, 28 Sep 1996 03:44:43 GMT  
 Long int as big as pointer?
Thanks for all your help!

OK, so I'm not *guaranteed* that
        sizeof(char *) <= sizeof(long int).

I'm trying to write a linked list module that is as portable as possible, and
which uses very generalized data.  Currently it uses a long int for the data,
and if your data is bigger you use a pointer to your (previously allocated)
data (which the module casts to a long int and uses as normal).  I did this
instead of using a simple pointer so if your data is small, the code is fast.

This obviously is not universally portable.  How many machines would it compile
on, though?  Most?  Many? Some?

And is there another way?  Or should I just make *all* users send me pointers
to their data?




Sat, 28 Sep 1996 13:17:41 GMT  
 Long int as big as pointer?

Quote:
>Path:
>volcane!titan.wordperfect.com!u.cc.utah.edu!hellgate.utah.edu!dog.ee.lbl.gov!ihn
>p4.ucsd.edu!usc!elroy.jpl.nasa.gov!netline-fddi.jpl.nasa.gov!marsupial.jpl.nasa.
>gov!not-for-mail

>Newsgroups: comp.lang.c
>Subject: Re: Long int as big as pointer?
>Date: 11 Apr 1994 12:44:43 -0700
>Organization: Jet Propellor Laboratory
>Lines: 13
>Distribution: na


>NNTP-Posting-Host: prof.jpl.nasa.gov

On IBM's AS/400 (using the C/400 compiler), pointers are 16 bytes (128 bits)
[!!].  On the other hand, C/400's longest integer (either int or long int) is
4 bytes (32 bits).  C/400 claims to be fully ANSI-compliant.  (And, having
used it, I must admit that it seems to be VERY strictly ANSI in almost every
way.)

I don't have the ANSI C standard in front of me, but I doubt that ANSI
guarantees that *some* integer type is necessarily long enough to hold a
pointer.   It does guarantee, however, that

length(char) <= length(short) <= length(int) <= length(long)

Weldon Whipple
WordPerfect Corporation

Quote:

>>Please tell me if I am guaranteed in ANSI C that
>>sizeof(long int) >= sizeof(char *)
>You are guaranteed that *some* integer type is large enough to hold a
>pointer, but you aren't guaranteed *which* type will.  OTOH, it's a
>pretty safe bet.
>--
>#include        <standard.disclaimer>
> _
>Kevin D Quitt 91351-4454                96.37% of all statistics are made up



Mon, 30 Sep 1996 23:08:29 GMT  
 Long int as big as pointer?

        ...
:There aren't many machines about where sizeof(long) < sizeof(char*), but it's

Intel 386-class-and-up processors can easily end up like that (depending
on memory-model and compiler details), since a (far) pointer in 32-bit
mode has a 16-bit segment and 32-bit offset (48-bit total), while "long"
is mapped to 32 bits in all C compilers I know for those chips.

Most 32-bit compilation for the 386 (and 486, and Pentium) so far uses
the "flat memory model" (segment stays implicit, so a pointer is made
up of just its offset part, i.e. 32 bits) -- but compilers already have
switches to force other memory models (in particular, "large", which
gives 48-bit pointers)...

[This is, in a way, a nit -- just pointing out that there ARE a huge
LOTS of machines where sizeof(long) "CAN BE" < sizeof(char*)... since
Intel 386 and compatibles no doubt wildly outnumber any other group
of chips in the general-purpose-computer arena...:-)]

Alex
--

CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: ++39 (51) 6130294



Sun, 06 Oct 1996 16:24:47 GMT  
 Long int as big as pointer?

Quote:

>    ...
>:There aren't many machines about where sizeof(long) < sizeof(char*), but it's


Quote:
>Intel 386-class-and-up processors can easily end up like that (depending
>on memory-model and compiler details), since a (far) pointer in 32-bit
>mode has a 16-bit segment and 32-bit offset (48-bit total), while "long"
>is mapped to 32 bits in all C compilers I know for those chips.

...

I recall reading somewhere that the C language guarantees that a long is at
least as big as an address.  Is this part of the ANSI standard?

Dave Hayden



Wed, 09 Oct 1996 05:13:20 GMT  
 Long int as big as pointer?


Quote:


>>I recall reading somewhere that the C language guarantees that a long is at
>>least as big as an address.  Is this part of the ANSI standard?

>There is no such guarantee in the ANSI/ISO standard.

>--

Well, it seems that this horse is not yet dead. So, I'll take a flog.
ANSI says that "a pointer may be converted to an integral type." So this
means that a pointer may be converted to a long -- maybe smaller if the
implementation so defines.

The mapping function from pointer to integer is implementation-defined.
Further, as I understand it, the reason for having such a conversion
(pointer <-> integer) is that, on some/many machines, the integer may be
interpreted as a machine address.

--



Thu, 10 Oct 1996 05:07:46 GMT  
 Long int as big as pointer?

Quote:
>ANSI says that "a pointer may be converted to an integral type." So this
>means that a pointer may be converted to a long -- maybe smaller if the
>implementation so defines.

The standard also says that you can convert an unsigned long to an
unsigned char, but that doesn't mean that an unsigned char cannot be
smaller than an unsigned long.

Furthermore, conversions between pointers and integral types are
undefined by the standard (I don't have my standard here so I cannot
say if they need to be defined by the implementation); there are no
requirements on the conversions to preserve information or be inverse
operations.  One valid implementation would be to have the result of
converting any pointer yield zero and the result of converting any
integral value yield a null pointer.

--
Alan Watson                        | [Feminism is] the expectations that

Department of Astronomy            | their sons, too. -- Anna Quindlen
University of Wisconsin -- Madison |



Thu, 10 Oct 1996 06:57:12 GMT  
 Long int as big as pointer?


Quote:
>Well, it seems that this horse is not yet dead. So, I'll take a flog.
>ANSI says that "a pointer may be converted to an integral type." So this
>means that a pointer may be converted to a long -- maybe smaller if the
>implementation so defines.

All this means is that the compiler will let you do it. It doesn't mean you
will get a meaningful result as it doesn't guarantee an implementation will
have an integral type wide enough to convert a pointer without loss of
nformation.

Quote:
>The mapping function from pointer to integer is implementation-defined.
>Further, as I understand it, the reason for having such a conversion
>(pointer <-> integer) is that, on some/many machines, the integer may be
>interpreted as a machine address.

It is true that many machine archetectures use the same registers to hold
integers and machine addresses. However use of pointer<-->integer conversion
results in non-portable code.

--
-----------------------------------------


-----------------------------------------



Thu, 10 Oct 1996 19:36:27 GMT  
 Long int as big as pointer?


Quote:
>I recall reading somewhere that the C language guarantees that a long is at
>least as big as an address.  Is this part of the ANSI standard?

There is no such guarantee in the ANSI/ISO standard.

--
-----------------------------------------


-----------------------------------------



Wed, 09 Oct 1996 21:04:56 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. char size (was long long long long long int)

2. typecasting int to long long int

3. calculator DIVision Big Numbers (something like Big Int)

4. Pointer to long int

5. long long int

6. long long int into octets (and back again)

7. long long int

8. cast long int to short int

9. unsigned long long int

10. long long int

11. Assigning and printing long long int's

12. long int versus long

 

 
Powered by phpBB® Forum Software