Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > perils of sizeof

Reply
Thread Tools

perils of sizeof

 
 
Mantorok Redgormor
Guest
Posts: n/a
 
      01-25-2004
Under c99 one can use %zu to portably print the value returned by
sizeof, since it is represented by a size_t which is an unsigned inter
type, you just don't know which unsigned integer type it actually is.

So under c89, a portable hack for printing the value returned from
sizeof(since we can't use %zu) would be to use %lu and cast sizeof to
unsigned long?



--
nethlek
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      01-25-2004
"Mantorok Redgormor" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Under c99 one can use %zu to portably print the value returned by
> sizeof, since it is represented by a size_t which is an unsigned inter
> type, you just don't know which unsigned integer type it actually is.


The use of z as a modifier was based on a popular extension, so it's not
uncommon to find it in code that predates the '99 standard.

> So under c89, a portable hack for printing the value returned from
> sizeof(since we can't use %zu) would be to use %lu and cast sizeof to
> unsigned long?


I'm not sure if you're asking or telling us, but yes.

In practice you don't always have to use unsigned long in C89. If you know
the size value is less than 32768 (e.g. very few strings are likely to be
longer!), then casting to unsigned and using %u is sufficient.

--
Peter


 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      01-25-2004
Peter Nilsson wrote:

> In practice you don't always have to use unsigned long in C89. If you know
> the size value is less than 32768 (e.g. very few strings are likely to be
> longer!), then casting to unsigned and using %u is sufficient.


ITYM less than 65536

--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      01-25-2004
"Richard Heathfield" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Peter Nilsson wrote:
>
> > In practice you don't always have to use unsigned long in C89. If you
> > know the size value is less than 32768 (e.g. very few strings are
> > likely to be longer!), then casting to unsigned and using %u is
> > sufficient.

>
> ITYM less than 65536


Yes, I did mean that. Thanks.

I was distracted by my parenthetic situation. Whilst size_t's maximum is
often used, ptrdiff_t is only required to represent offsets in line with
the minimum object size (for each of the two standards).

I have written, and have certainly seen, a great many string coding snippets
that implicitly rely on ptrdiff_t being able to represent the difference
between two char pointers. Since 32767 is the smallest such difference a
strictly conforming program can utilise, that's the value I often use to
represent the largest valid string length.

I believe that PTRDIFF_MAX is a better bound on object size than SIZE_MAX.
But I'm probably just being paranoid!

--
Peter


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      01-25-2004
On 24 Jan 2004 23:42:01 -0800, (E-Mail Removed) (Mantorok Redgormor)
wrote in comp.lang.c:

> Under c99 one can use %zu to portably print the value returned by
> sizeof, since it is represented by a size_t which is an unsigned inter
> type, you just don't know which unsigned integer type it actually is.
>
> So under c89, a portable hack for printing the value returned from
> sizeof(since we can't use %zu) would be to use %lu and cast sizeof to
> unsigned long?


No problem at all. C89/90 particularly specified, in response to a
DR, that size_t could not be larger than unsigned long, although this
is no longer true in C99. So there is no possible loss of information
in casting a size_t to unsigned long under C89/90.

--
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++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
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
The perils of misplaced decimal places Rhino Java 1 04-14-2006 07:31 AM
Interesting article by Joel Spolsky: The Perils of JavaSchools Casey Hawthorne Java 14 01-16-2006 07:17 PM
Perils of getting Paid Tom A+ Certification 4 04-15-2004 02:01 AM
perils of memset Mantorok Redgormor C Programming 2 02-22-2004 07:45 PM
TinyURL perils Petit Alexi Computer Support 13 11-10-2003 04:37 AM



Advertisments