perils of sizeof

M

Mantorok Redgormor

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?
 
P

Peter Nilsson

Mantorok Redgormor said:
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.
 
R

Richard Heathfield

Peter said:
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
 
P

Peter Nilsson

Richard Heathfield said:
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! ;)
 
J

Jack Klein

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top