3

I printed sizeof(struct tm) in C using sizeof() operator it gives me 44 bytes.But in man page of ctime it has 9 int variables for time.then its size should be 36. How it is giving 44?

Radha
  • 53
  • 1
  • 8

3 Answers3

5

http://linux.die.net/man/3/ctime

The glibc version of struct tm has additional fields

long tm_gmtoff;           /* Seconds east of UTC */
const char *tm_zone;      /* Timezone abbreviation */

That's where you extra bytes come from (probably).

Community
  • 1
  • 1
RedX
  • 14,749
  • 1
  • 53
  • 76
2

Apart from the very true answers of RedX and Adeel, padding inside the structure can also lead to a size greater than the sum of the size of all elements. To prevent this with custom structures, you can use GCCs __attribute__((__packed__)) feature.

matthias
  • 2,161
  • 15
  • 22
2

The glibc version of struct tm has additional fields...

long tm_gmtoff;           /* Seconds east of UTC */
const char *tm_zone;      /* Timezone abbreviation */

Read again man ctime..

Adeel Ahmed
  • 1,591
  • 8
  • 10