hp ux - c: unsigned long long being assigned wrong value on HP-UX Only -


had bit of trouble coming title question.

i fumbled in world of c.

i have little bit of code shows capacity , free space of drive. works fine on few different linux distros i've tried solaris , aix. compiled on hp-ux pa-risc box , got (in opinion) strange error.

struct statfs   fsstat; err = statfs(rootpath,&fsstat); unsigned long long totalbytes = (unsigned long long)(fsstat.f_bsize * fsstat.f_blocks); 

in gdb when do:

p (fsstat.f_bsize * fsstat.f_blocks) 

the result 1335205888 after calculation run, when do

p totalbyes 

the result 18446744071562067968

any information might give me idea of try here great. used think knew how program until started doing multi platform c :(

hypothesis:

the multiplication overflowed, fsstat.f_bsize * fsstat.f_blocks produced overflow result of -2147483648. when converted unsigned long long, produced 18446744071562067968, 0xffffffff80000000, result of wrapping -2147483648 in 64-bit unsigned format. gdb uses different arithmetic c, displayed mathematically correct result.

to fix this, change (unsigned long long) (fsstat.f_bsize * fsstat.f_blocks) (unsigned long long) fsstat.f_bsize * fsstat.f_blocks, convert wider integer format before multiplication.

better unsigned long long use either uint64_t (from <stdint.h>) or type supplied platform (some linux header) working disk sizes.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -