Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Size of long

Reply
Thread Tools

Size of long

 
 
John Tsiombikas (Nuclear / the Lab)
Guest
Posts: n/a
 
      07-04-2003
Nikolai Borissov wrote:
> I know that C++ Standard does not impose any requirements for the size of
> _long_ type. I wonder what is the size of _long_ in reality?


What do you mean "in reality"? that depends on the implementation /
system. Just do a cout << sizeof(long); and find out for your system.
for my system, long is 4bytes.

-- Nuclear / the Lab --

 
Reply With Quote
 
 
 
 
Magnus
Guest
Posts: n/a
 
      07-04-2003

"Nikolai Borissov" <(E-Mail Removed)> skrev i melding
news:J93Na.1304$(E-Mail Removed).. .
> I know that C++ Standard does not impose any requirements for the size of
> _long_ type. I wonder what is the size of _long_ in reality? I don't think
> that there are architectures with 1-byte _long_. I think, the most

probable
> size for _long_ would be 4 bytes. What about 2-byte _long_, are there any
> architectures with that size?
>
> Thanks,
>
> Nikolai Borissov
>
>


AFAIK you have no guarantee on the size of the long type in your case, what
is guaranteed is the minimum size of this type, wich is 4 bytes for the long
type. The int type is 2 bytes on some systems and 4 on others, you are
however guaranteed that it is at the minimum 2 bytes.

Could someone confirm this ?

-Magnus


 
Reply With Quote
 
 
 
 
Jim Fischer
Guest
Posts: n/a
 
      07-04-2003
Nikolai Borissov wrote:
> Magnus wrote:
>
>
>>AFAIK you have no guarantee on the size of the long type in your case,

>
> what
>
>>is guaranteed is the minimum size of this type, wich is 4 bytes for the

>
> long
>
>>type. The int type is 2 bytes on some systems and 4 on others, you are
>>however guaranteed that it is at the minimum 2 bytes.

>
>
> If minimum of 4 bytes is guaranteed by the Standard it would be excellent.
> I've got only Draft Standard and couldn't find any specific information
> about _long_ type. This whole point about portability.
>
>
>>Could someone confirm this ?

>
>
> Someone who is the true expert in the Standard, please respond.


I don't claim to be a "true expert" in the C/C++ standards, but...

The 'int' type must be able to represent values in the range INT_MIN to
INT_MAX, as defined in the header <climits> (see: 3.9.1/2 and footnote
39). The 'long int' type must provide *at least* as much storage as the
'int' type (3.9.1/2).

According to the C(99) standard [which is NOT the same C standard that
C++ uses], the implementation-defined INT_MIN and INT_MAX values must be
equal or greater in magnitude (absolute value) to the following values,
with the same sign:

INT_MIN -32767
INT_MAX +32767

So assuming 8-bit bytes, and assuming the C(90) INT_MAX, INT_MIN
definitions are essentially the same as the C(99) definitions, then C++
guarantees,

2 <= sizeof(int) <= sizeof(long int)

IOW, the guaranteed minimum size of a 'long int' object is 2 bytes, not
4. At least, this is how I read the C(99)/C++(9 standards.

FWIW, on 32-bit platforms, a "typical" standard C library implementation
defines INT_MIN and INT_MAX so that (assuming 8-bit bytes),

4 <= sizeof(int) <= sizeof(long int)

But these are only "typical" values; they are not guaranteed values.

Comments from the real C/C++ standard experts are welcome...

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      07-04-2003

Nikolai Borissov <(E-Mail Removed)> wrote in message
news:J93Na.1304$(E-Mail Removed).. .
> I know that C++ Standard does not impose any requirements for the size of
> _long_ type.


No you don't know that, since it's not true.
Type 'long' is required to occupy at least 32 bits
(but is allowed to be larger).

> I wonder what is the size of _long_ in reality?


It's different among implementations. You can use
the 'sizeof' operator to determine the size of a
particular type.

sizeof(long)

>I don't think
> that there are architectures with 1-byte _long_.


There indeed could be. Note that the size of a byte
(char) is required to be at least eight bits, but
could be more (e.g. 32 bits), in which case a 'long'
might have size of one byte (but might not )

>I think, the most probable
> size for _long_ would be 4 bytes.


That's a common size for platforms with eight bit bytes.

>What about 2-byte _long_, are there any
> architectures with that size?


There could be if the host platform used 16-bit bytes.

Note that you can use the macros from <climits> or
member functions of 'std::numeric_limits' from <limits>
to determine the smallest/largest possible values for
specific types on your implementation.

-Mike



 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      07-04-2003

Magnus <(E-Mail Removed)> wrote in message
news:3f04cab2$(E-Mail Removed)...
>
> "Nikolai Borissov" <(E-Mail Removed)> skrev i melding
> news:J93Na.1304$(E-Mail Removed).. .
> > I know that C++ Standard does not impose any requirements for the size

of
> > _long_ type. I wonder what is the size of _long_ in reality? I don't

think
> > that there are architectures with 1-byte _long_. I think, the most

> probable
> > size for _long_ would be 4 bytes. What about 2-byte _long_, are there

any
> > architectures with that size?
> >
> > Thanks,
> >
> > Nikolai Borissov
> >
> >

>
> AFAIK you have no guarantee on the size of the long type in your case,

what
> is guaranteed is the minimum size of this type, wich is 4 bytes for the

long
> type. The int type is 2 bytes on some systems and 4 on others, you are
> however guaranteed that it is at the minimum 2 bytes.
>
> Could someone confirm this ?


I can 'disconfirm' it.

There's no requirement that the size of a byte not
be more than eight bits. E.g. a platform with sixteen
bit bytes can have a conforming type 'long' which is
only two bytes in size.

-Mike



 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      07-04-2003

Nikolai Borissov <(E-Mail Removed)> wrote in message
news:R74Na.1328$(E-Mail Removed).. .
> Magnus wrote:
>
> > AFAIK you have no guarantee on the size of the long type in your case,

> what
> > is guaranteed is the minimum size of this type, wich is 4 bytes for the

> long
> > type. The int type is 2 bytes on some systems and 4 on others, you are
> > however guaranteed that it is at the minimum 2 bytes.

>
> If minimum of 4 bytes is guaranteed by the Standard it would be excellent.


First, drop the assumption that all bytes are eight bits.
They're not. The standard does require a byte have
*at least* eight bits, but allows more.

The smallest allowed size for type 'long' is sixteen bits.
However many bytes that takes on the host platform, depends
upon that platform.

> I've got only Draft Standard and couldn't find any specific information
> about _long_ type. This whole point about portability.


I think you need to look harder.
All the types' requirements are specified.

>
> > Could someone confirm this ?


Nope. Can't confirm what ain't true.


>
> Someone who is the true expert in the Standard, please respond.


No such person exists imo.

-Mike



 
Reply With Quote
 
Nikolai Borissov
Guest
Posts: n/a
 
      07-04-2003
I know that C++ Standard does not impose any requirements for the size of
_long_ type. I wonder what is the size of _long_ in reality? I don't think
that there are architectures with 1-byte _long_. I think, the most probable
size for _long_ would be 4 bytes. What about 2-byte _long_, are there any
architectures with that size?

Thanks,

Nikolai Borissov


 
Reply With Quote
 
Nikolai Borissov
Guest
Posts: n/a
 
      07-04-2003
Magnus wrote:

> AFAIK you have no guarantee on the size of the long type in your case,

what
> is guaranteed is the minimum size of this type, wich is 4 bytes for the

long
> type. The int type is 2 bytes on some systems and 4 on others, you are
> however guaranteed that it is at the minimum 2 bytes.


If minimum of 4 bytes is guaranteed by the Standard it would be excellent.
I've got only Draft Standard and couldn't find any specific information
about _long_ type. This whole point about portability.

> Could someone confirm this ?


Someone who is the true expert in the Standard, please respond.

Nikolai Borissov


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      07-04-2003
On Thu, 3 Jul 2003 19:41:02 -0700, "Nikolai Borissov"
<(E-Mail Removed)> wrote in comp.lang.c++:

> I know that C++ Standard does not impose any requirements for the size of
> _long_ type. I wonder what is the size of _long_ in reality? I don't think
> that there are architectures with 1-byte _long_. I think, the most probable
> size for _long_ would be 4 bytes. What about 2-byte _long_, are there any
> architectures with that size?
>
> Thanks,
>
> Nikolai Borissov


Sigh, another desk-top chauvinist. You think the platforms you work
on are all there is, whereas C and C++ are quite extensively used in
embedded systems using processor architectures you have never heard
of.

I am literally writing code for a Texas Instruments 2812 16/32 bit
digital signal processor which comes with (subset) free-standing C and
C++ compilers.

This processor can't access memory in anything smaller than 16 bit
words. So the character types are 1 byte (but have 16 bits). The
short and int types are also 1 byte (with 16 bits). And signed and
unsigned long are 2 bytes long and have 32 bits.

And there are 32 bit DSPs (from TI, AD, and probably Motorola) where
everything is 32 bits, char, short, int, and long. So sizeof(long) ==
1.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      07-04-2003
Jim Fischer wrote:
>
> According to the C(99) standard [which is NOT the same C standard that
> C++ uses], the implementation-defined INT_MIN and INT_MAX values must be
> equal or greater in magnitude (absolute value) to the following values,
> with the same sign:
>
> INT_MIN -32767
> INT_MAX +32767
>
> So assuming 8-bit bytes, and assuming the C(90) INT_MAX, INT_MIN
> definitions are essentially the same as the C(99) definitions, then C++
> guarantees,
>


That was true in the C90 standard, so it applies in C++ as well. But
there's something even more useful: LONG_MIN and LONG_MAX are also
specified, and impose a minimum size of 32 bits.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
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
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
Use of Long and Long Long Bart C C Programming 27 01-15-2008 05:27 AM
long long and long Mathieu Dutour C Programming 4 07-24-2007 11:15 AM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments