Hi,
On Tue, Jan 13, 2009 at 10:51 AM, Henrik Austad <[email protected]> wrote: > I'm using gettimeofday to get the most accurate time from the kernel > > When I use calloc to create and initalize memory for the struct timeval *tv, > everything works fine: > > struct timeval *tv; > tv = (struct timeval *)calloc(1, sizeof(struct timeval)); > if (!tv) { > /* handle error... */ > exit > } > gettimeofday(tv); according http://dell5.ma.utexas.edu/cgi-bin/man-cgi?gettimeofday+2 gettimeofday has 2 input agruments. I get you code and compile and works ok for both (mallo, calloc). My source code: #include <stdio.h> #include <stdlib.h> #include <sys/time.h> /* * struct timeval { * time_t tv_sec; * suseconds_t tv_usec; * }; */ void gettime_1(void) { struct timeval *tv; tv = (struct timeval *)malloc(sizeof (*tv)); if (!tv) { fprintf(stderr, "Could not allocate memory for timestruct\n"); return; } gettimeofday(tv, NULL); printf("Time1: %lu, %lu\n", (long unsigned)tv->tv_sec, (long unsigned)tv->tv_usec); } void gettime_2(void) { struct timeval *tv; tv = (struct timeval *)calloc(1,sizeof (*tv)); if (!tv) { fprintf(stderr, "Could not allocate memory for timestruct\n"); return; } gettimeofday(tv, NULL); printf("Time2: %lu, %lu\n", (long unsigned)tv->tv_sec, (long unsigned)tv->tv_usec); } int main (int argc, char *argv[]) { gettime_1(); gettime_2(); return 0; } > > But, when I use malloc, gettimeofday segfaults, even though tv != NULL, and I > can set tv_sec and tv_usec manually without getting a segfault. Normally I'm > able to figure these kind of things out, but right now, I'm stuck. Especially > as it works if I use calloc and gettimeofday on a separate struct, or, if i > print the address of the malloc'ed timeval. > > strace of program: > > execve("./test_time", ["./test_time"], [/* 49 vars */]) = 0 > brk(0) = 0x2330000 > mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = > 0x7f42ca271000 > access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or > directory) > mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = > 0x7f42ca26f000 > access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or > directory) > open("tls/x86_64/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or > directory) > open("tls/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or > directory) > open("x86_64/libc.s#include <stdio.h> > #include <stdlib.h> > > /* > * struct timeval { > * time_t tv_sec; > * suseconds_t tv_usec; > * }; > */ > > void gettime_1(void) > { > struct timeval *tv; > tv = (struct timeval *)malloc(1*sizeof (struct timeval)); > if (!tv) { > fprintf(stderr, "Could not allocate memory for timestruct\n"); > return; > } > gettimeofday(tv); > printf("Time1: %lu, %lu\n", (long unsigned)tv->tv_sec, (long > unsigned)tv- >>tv_usec); > } > void gettime_2(void) > { > struct timeval *tv; > tv = (struct timeval *)calloc(1,sizeof (struct timeval)); > if (!tv) { > fprintf(stderr, "Could not allocate memory for timestruct\n"); > return; > } > gettimeofday(tv); > printf("Time2: %lu, %lu\n", (long unsigned)tv->tv_sec, (long > unsigned)tv- >>tv_usec); > } > int main (int argc, char *argv[]) > { > gettime_1(); > gettime_2(); > return 0; > } > o.6", O_RDONLY) = -1 ENOENT (No such file or directory) > open("libc.so.6", O_RDONLY) = -1 ENOENT (No such file or > directory) > open("/usr/lib32/tls/x86_64/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or > directory) > stat("/usr/lib32/tls/x86_64", 0x7fffd226fb20) = -1 ENOENT (No such file or > directory) > open("/usr/lib32/tls/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or > directory) > stat("/usr/lib32/tls", 0x7fffd226fb20) = -1 ENOENT (No such file or > directory) > open("/usr/lib32/x86_64/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or > directory) > stat("/usr/lib32/x86_64", 0x7fffd226fb20) = -1 ENOENT (No such file or > directory) > open("/usr/lib32/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or > directory) > stat("/usr/lib32", {st_mode=S_IFDIR|0755, st_size=36864, ...}) = 0 > open("/etc/ld.so.cache", O_RDONLY) = 3 > fstat(3, {st_mode=S_IFREG|0644, st_size=115573, ...}) = 0 > mmap(NULL, 115573, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f42ca252000 > close(3) = 0 > access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or > directory) > open("/lib/libc.so.6", O_RDONLY) = 3 > read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\345"..., 832) = > 832 > fstat(3, {st_mode=S_IFREG|0755, st_size=1502520, ...}) = 0 > mmap(NULL, 3609304, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = > 0x7f42c9ce2000 > mprotect(0x7f42c9e4b000, 2093056, PROT_NONE) = 0 > mmap(0x7f42ca04a000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED| > MAP_DENYWRITE, 3, 0x168000) = 0x7f42ca04a000 > mmap(0x7f42ca04f000, 17112, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED| > MAP_ANONYMOUS, -1, 0) = 0x7f42ca04f000 > close(3) = 0 > mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = > 0x7f42ca251000 > mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = > 0x7f42ca250000 > arch_prctl(ARCH_SET_FS, 0x7f42ca2506e0) = 0 > mprotect(0x7f42ca04a000, 16384, PROT_READ) = 0 > mprotect(0x600000, 4096, PROT_READ) = 0 > mprotect(0x7f42ca272000, 4096, PROT_READ) = 0 > munmap(0x7f42ca252000, 115573) = 0 > brk(0) = 0x2330000 > brk(0x2351000) = 0x2351000 > --- SIGSEGV (Segmentation fault) @ 0 (0) --- > +++ killed by SIGSEGV +++ > Process 9387 detached > > > The program itself (note the usage of gettime_1 and gettime_2 (if I call > gettime_2 before gettime_1, it works, if I call gettime_1 first, it > segfaults). > > #include <stdio.h> > #include <stdlib.h> > > /* > * struct timeval { > * time_t tv_sec; > * suseconds_t tv_usec; > * }; > */ > > void gettime_1(void) > { > struct timeval *tv; > tv = (struct timeval *)malloc(1*sizeof (struct timeval)); > if (!tv) { > fprintf(stderr, "Could not allocate memory for timestruct\n"); > return; > } > gettimeofday(tv); > printf("Time1: %lu, %lu\n", (long unsigned)tv->tv_sec, (long > unsigned)tv- >>tv_usec); > } > void gettime_2(void) > { > struct timeval *tv; > tv = (struct timeval *)calloc(1,sizeof (struct timeval)); > if (!tv) { > fprintf(stderr, "Could not allocate memory for timestruct\n"); > return; > } > gettimeofday(tv); > printf("Time2: %lu, %lu\n", (long unsigned)tv->tv_sec, (long > unsigned)tv- >>tv_usec); > } > int main (int argc, char *argv[]) > { > gettime_1(); > gettime_2(); > return 0; > } > > > Any help appreciated :-) > > -- > Henrik > > -- > To unsubscribe from this list: send an email with > "unsubscribe kernelnewbies" to [email protected] > Please read the FAQ at http://kernelnewbies.org/FAQ > > -- as simple as primitive as possible ---------------------------------------------- Marek Beliško - open-nandra Ruská Nová Ves 219 08005 Prešov Slovakia http://open-nandra.com N�����r��y����b�X��ǧu�ޙ���+a�{.n�+���z�ޖw�n'����j�b�ye�{������z��v�^�m���z�ޖw�n'���?
