Re: what am I missing?
- Posted by mattlewis (admin) Jun 19, 2009
- 1161 views
jacques_desch said...
Matt, Following you suggestion, here de C code I tested and it crash too, but only if I free t pointer. The same is true in my euphoria binding.
#include <stdio.h> #include <asm/termbits.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> int main(){ int fd, fnVal; struct termios2 *t; fd = open("/dev/tty0",O_RDONLY); printf("file handle %d\n", fd); t = malloc(sizeof(struct termios2)); fnVal = tcgetattr(fd, t); printf("ispeed %d, ospeed %d\n",t->c_ispeed, t->c_ospeed); free(t); // it doesn't crash if I don't free t. puts("t freed\n"); close(fd); }Now the question is why freeing t, which is no more used, crash the program?
I think you're using the wrong header. See the man page, here.
Looks like the correct size is actually 60. Try this code:
#include <stdio.h> #include <termios.h> #include <fcntl.h> int main(){ int fd, fnVal; struct termios *t; printf("termios %d\n", sizeof( struct termios ) ); fd = open("/dev/tty",O_RDONLY); printf("file handle %d\n", fd); t = malloc(sizeof(struct termios)); fnVal = tcgetattr(fd, t); printf("fnVal %d ispeed %d, ospeed %d\n", fnVal, t->c_ispeed, t->c_ospeed); free(t); // it doesn't crash if I don't free t. puts("t freed\n"); close(fd); }Matt