Re: what am I missing?
- Posted by DerekParnell (admin) Jun 18, 2009
- 1177 views
here C code from termbits.h
typedef unsigned char cc_t; typedef unsigned int speed_t; typedef unsigned int tcflag_t; #define NCCS 19 struct termios2 { tcflag_t c_iflag; /* input mode flags */ tcflag_t c_oflag; /* output mode flags */ tcflag_t c_cflag; /* control mode flags */ tcflag_t c_lflag; /* local mode flags */ cc_t c_line; /* line discipline */ cc_t c_cc[NCCS]; /* control characters */ speed_t c_ispeed; /* input speed */ speed_t c_ospeed; /* output speed */ };
from this I compute 44 bytes for termios2 struct:
4*4 + 1 + 19 + 2*4 = 44
But if I allocate 44 bytes my program crash. I must allocate 45 bytes for it to works. Where is the missing byte?
Jacques
The C language leaves the packaging of data within a struct up to the compiler. Most compliers add padding bytes in order to ensure alignment of data on certain boundaries. In this case, it would appear that it tries to make sure that each new field begins on an even address, so I'm guessing there is a hidden padding byte between c_line and c_cc fields. However, that would also mean that you need to allocate 46 bytes because there should also be a padding byte after c_cc field.
By the way, in this forum you can use {{{ }}} to set aside a block of text that is not inspected for markup tags such as '#'.