开发者

c passing unsigned long long value

How can the last two lines of one function be

printf("disk_free_blocks returning %llu\n",item_int3);
return (item_int3);

and out pu开发者_StackOverflow社区t

disk_free_blocks returning 233012428800

returning to calling function as

part_avail=disk_free_blocks(DiskParts[part_index].part_name,DISK_AVAIL);
if (DEBUG) printf("DiskParts[%d].part_name=%s has %llu free.\n",part_index,DiskParts[part_index].part_name,part_avail);

and output be

DiskParts[0].part_name=/dev/sda1 has 1084194816 free.

??

unsigned long long part_avail, item_int3;


The two output numbers are:

0x00000036409F8000

and

        0x409F8000

It appears that the return type (which you haven't shown) isn't large enough to accomodate a 64-bit value, so the compiler simply truncates (which is the behavior required by the standard, for narrowing conversions on unsigned integers).


I tend to think these are both wrong: 233 billion blocks, with a 512 byte block, that is 100 TB. Not likely. 1 billion blocks is then around 512 GB, which might or might not match your actual /dev/sda1 free space.

unsigned long long part_avail, item_int3;

This doesn't make sense, since the usage of these two variables is in different scopes. Are there two variables with the same name? Or are you using global variables?

Possibly item_int3 inside the function is only 32 bits, printf reads past the end of the argument list during vararg processing, and the stack just happened to have 0x00000036 on it, so that got printed by the %llu specifier. When returned as an unsigned long long, the compiler properly zero-extends the value, and then the caller pushes the full 64-bit value onto the stack, which is retrieved and printed correctly during printf vararg processing of the %llu format code.


The return value was truncated to 32 bits somewhere along the way. Perhaps disk_free_blocks is declared as returning an int?


RESOLVED

disk_free_blocks() resided in a different file than process_copy_out(), the function making the call to disk_free_blocks.

The fix was adding a function prototype to inform the compiler.

Thank you everyone for your help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜