Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 23 Jun 2005 13:00:50 +0400
From:      Dmitry Mityugov <dmitry.mityugov@gmail.com>
To:        Olivier Nicole <on@cs.ait.ac.th>
Cc:        questions@freebsd.org
Subject:   Re: Using regex(3)
Message-ID:  <b7052e1e0506230200599029f2@mail.gmail.com>
In-Reply-To: <200506230143.j5N1hkWI097845@banyan.cs.ait.ac.th>
References:  <200506220411.j5M4BQ6G087888@banyan.cs.ait.ac.th> <42B91313.9050807@bhi-hamburg.de> <200506230143.j5N1hkWI097845@banyan.cs.ait.ac.th>

next in thread | previous in thread | raw e-mail | index | archive | help
On 6/23/05, Olivier Nicole <on@cs.ait.ac.th> wrote:
> Thanks Titus,
>=20
> > no, you're misunderstanding regoff_t or printf.
>=20
> 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.
>=20
> I am no C guru, but that sound very bad to me.

--=20
Dmitry

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?b7052e1e0506230200599029f2>