tags:

views:

432

answers:

3

Hi All,

I wrote this tiny code:

#include <stdio.h>
int main() {
    size_t temp;
    temp = 100;

    printf("lld=%lld, ld=%ld, u=%u\n", temp, temp, temp);

    return 0;
}

I am running this on a i386 GNU/Linux machine with gcc version 4.1.1 20070105 (Red Hat 4.1.1-52). This is the output that I got:

lld=429496729700, ld=100, u=7993461

I can understand that the first (lld) was printed as garbage because the printf tries to print 8 bytes (for signed long long as signified by lld) when only 4 bytes are available from variable temp. But, I fail to understand why the last identifier, u is getting printed as garbage - whereas, in my understanding this is the closest applicable identifier for size_t.

Here I have assumed that size_t is unsigned int (which is signed 4 bytes for my i386).

Now, I did a little tweaking with the printf line:

...
printf("ld=%ld, u=%u, lld=%lld\n", temp, temp, temp);
...

and I have a perfectly fine answer (except the lld part).

ld=100, u=100, lld=34331653576851556

Can someone please help me in understanding what exactly am I missing here?

Thanks a lot for any help!

[side note: I tried switching optimization using gcc -O[0,2] tag on/off without any difference in the observation.]

+7  A: 

That's because what you've pushed on the stack is three 32-bit values and your format string tries to use four of them or, more accurately, one 64-bit value and two 32-bit values.

In the first case, the lld sucks up two 32-bit values, the ld sucks up the third one and the u gets whatever happens to be on the stack after that, which could really be anything.

When you change the order of the format specifiers in the string, it works differently because the ld sucks up the first 32-bit value, the u sucks up the second and the lld sucks up the third plus whatever happens to be on the stack after that. That's why you're getting different values, it's a data alignment/availability issue.

You can see this in action with the first value. 429496729700 is equal to (4294967296 + 1) * 100, i.e., (232+1)*100. Your code snippet

printf("lld=%lld, ld=%ld, u=%u\n", temp, temp, temp);

actually has the following effect:

What you pass     Stack     What printf() uses
-------------     -----     ------------------
                 +-----+
100              | 100 | \
                 +-----+  = 64-bit value for %lld.
100              | 100 | /
                 +-----+
100              | 100 |    32-bit value for %ld.
                 +-----+
                 | ?   |    32-bit value for %u (could be anything).
                 +-----+

In the second case

printf("ld=%ld, u=%u, lld=%lld\n", temp, temp, temp);

the following occurs:

What you pass     Stack     What printf() uses
-------------     -----     ------------------
                 +-----+
100              | 100 |    32-bit value for %ld.
                 +-----+
100              | 100 |    32-bit value for %u.
                 +-----+
100              | 100 | \
                 +-----+  = 64-bit value for %lld (could be anything).
                 | ?   | /
                 +-----+
paxdiablo
Yup, this sounds to perfectly the answer I was looking for. I read through the assembly code and it too does what you have mentioned (the part of pushing 3 variables of 32 byte on stack). I just could not confirm how printf was consuming them. Thanks for confirming!
Shrey
A: 

You are passing to printf the wrong number of bytes. %lld requires a larger integer, in your case the way %lld taked its argument is completely messed up, since it would expect a 64-bit value.

Giuseppe Guerrini
+2  A: 

Your code aptly demonstrates Undefined Behavior. Note that in case of variadic arguments no type checking is done for parameters. This is when an explicit cast becomes necessary. In fact the following should therefore be used:

 printf("lld=%lld, ld=%ld, u=%u\n", 
         (unsigned long long)temp, 
         (unsigned long)temp, 
         (unsigned int)temp);

As an aside remember the specifier for size_t is z. So:

 printf("zd=%zd\n", temp);
dirkgently