Using regex(3)

Dmitry Mityugov dmitry.mityugov at gmail.com
Thu Jun 23 09:00:51 GMT 2005


On 6/23/05, Olivier Nicole <on at cs.ait.ac.th> wrote:
> Thanks Titus,
> 
> > no, you're misunderstanding regoff_t or printf.
> 
> I definitely misunderstand printf. Until now I thought that each place
> holder (%d) was associated with one variable and if the type
> missmatched, the display could be incorrect.

Not exactly. Each placeholder like %d is associated with a
corresponding number of bytes placed on the stack for values passed to
printf(). In addition, many compilers expand values being placed on
the stack to the native data boundary. For example, compilers that
produce 32-bit code often expand chars and shorts passed to printf()
to ints; corresponding placeholders like %c etc know that those 32-bit
values are just chars and shorts and treat them accordingly. If you by
mistake use %u instead of %c, printf() may print garbage instead of
corresponding char value, but subsequent placeholders will still
correspond to correct 32-bit portions of the stack.

64-bit values in 32-bit code are different, because they occupy 2
32-bit chunks on the stack, and if you try to deal with them with
%d's, without telling printf() that those are in fact 64-bit values
with %lld or similar format specifiers, you'll cause subsequent format
specifier to access the high-half of the 64-bit value instead of the
corresponding value, and so on; from now on, the entire stack during
the call to printf() is misaligned, and all subsequent format
specifiers will print garbage. This may explain why you're seeing 0's
instead of valid output in your program.

Hope this helps.

> But in that case, printf seems to take 2 successive %d and split the
> variable upon them to make it a %lld.
> 
> I am no C guru, but that sound very bad to me.

-- 
Dmitry

"We live less by imagination than despite it" - Rockwell Kent, "N by E"


More information about the freebsd-questions mailing list