libc

How to sleep for a few microseconds

白昼怎懂夜的黑 提交于 2019-11-29 14:20:02
问题 Consider the following code: #include <stdio.h> #include <time.h> #include <math.h> // Compile with gcc -lrt -lm -o test_clock test_clock.c #define CLOCK CLOCK_MONOTONIC int main(int argc, char** argv) { double temp, elapsed; int j; struct timespec requestStart, requestEnd, req; // Pseudo-sleep clock_gettime(CLOCK, &requestStart); temp = 0; for(j=0; j < 40; j++) temp += sin(j); clock_gettime(CLOCK, &requestEnd); elapsed = ( requestEnd.tv_sec - requestStart.tv_sec ) / 1e-6 + ( requestEnd.tv

How does libc provide functions with two names?

风格不统一 提交于 2019-11-29 11:12:41
Before the advent of direct binding (-B direct) libc provided many functions with two names. For example, getpwent() and _getpwent(). These two names referred to exactly the same function in libc. How does libc make two function names point to the same implementation? I think it should not be as easy as writing the same code twice though. It's done via weak aliases a "nonstandard" linker trick that's been around since early unices and that's supported by all unix compilers/linkers I know of. It's basically done as: void __foo(void); void foo(void) __attribute__((weak, alias("__foo"))); often

Catching libc error messages, redirecting from /dev/tty

橙三吉。 提交于 2019-11-29 10:16:21
I am trying to catch error messages that libc generates when it detects error conditions. For example, my test code: #include <stdlib.h> int main() { char* p = (char*)malloc(10); free(p); free(p); } Generates this output $ ./main *** Error in `./main': double free or corruption (fasttop): 0x000000000124b010 *** ======= Backtrace: ========= /lib64/libc.so.6(+0x7d1fd)[0x7f8c121291fd] ./main[0x400b86] /lib64/libc.so.6(__libc_start_main+0xf5)[0x7f8c120cdaf5] ./main[0x400a79] ... <snip> However, it is not being written to stderr or stdout, but /dev/tty (which I discovered using strace) open("/dev

Are posix regcomp and regexec threadsafe? In specific, on GNU libc?

帅比萌擦擦* 提交于 2019-11-29 09:31:36
Two separate questions here really: Can I use regexes in a multithreaded program without locking and, if so, can I use the same regex_t at the same time in multiple threads? I can't find an answer on Google or the manpages. http://www.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html 2.9.1 Thread-Safety All functions defined by this volume of POSIX.1-2008 shall be thread-safe, except that the following functions1 need not be thread-safe. ... regexec and regcomp are not in that list, so they are required to be thread-safe. See also: http://www.opengroup.org/onlinepubs/9699919799

Fatal signal 7 (SIGBUS) at 0x00000000 (code=2)

╄→尐↘猪︶ㄣ 提交于 2019-11-29 06:06:52
While using a OSGi Platform on Android i got this errormsg: Fatal signal 7 (SIGBUS) at 0x595302e0 (code=2) I don't think that my app needs that much space in memory or need a lot of computation power. Its just the OSGi Platform with 20 Bundles. My app is always restarting after that. Any ideas ? Even I too found a similar problem and happens to be (code=128) with about fault. Seems to be a data alignment error. To solve this issue, I tried putting a code something like this: *Data = (*Data)(Temp+i) becomes Temp1 = *(Temp+i); *Data = Temp1; Before and into the function call and passed back the

Is there any scenario where function fma in libc can be used?

天涯浪子 提交于 2019-11-29 04:50:54
I come across this page and find there is an odd floating multiply add function -- fma and fmaf . It says that the result is something like: (x * y) + z #fma(x,y,z) And the value is infinite precision and round once to the result format . However, AFAICT I've never seen such a ternary operation before. So I'm wondering what's the cumstom usage for this func. The important aspect of the fused-multiply-add instruction is the (virtually) infinite precision of the intermediate result. This helps with performance, but not so much because two operations are encoded in a single instruction — It helps

Unit testing for failed malloc()

此生再无相见时 提交于 2019-11-29 04:20:32
What is the best way for unit testing code paths involving a failed malloc() ? In most instances, it probably doesn't matter because you're doing something like thingy *my_thingy = malloc(sizeof(thingy)); if (my_thingy == NULL) { fprintf(stderr, "We're so screwed!\n"); exit(EXIT_FAILURE); } but in some instances you have choices other than dying, because you've allocated some extra stuff for caching or whatever, and you can reclaim that memory. However, in those instances where you can try to recover from a failed malloc() that you're doing something tricky and error prone in a code path that

Why does time(time_t *) function both return and set the by-ref?

我怕爱的太早我们不能终老 提交于 2019-11-29 03:28:10
I've always been curious, why does the time(time_t *) function both return a time_t , and set the time to the passed in pointer? Example of returning the time: time_t myTime = time(NULL); printf("The time is now %s", ctime(&myTime)); Example of setting the value to the pointer: time_t myTime; time(&myTime); printf("The time is now %s", ctime(&myTime)); I originally thought there would be a performance gain by writing to the memory instead of returning, but if it has to do both, doesn't that just make it slower? There's no real benefit in the way it's currently defined. I suspect that when the

How to write my own printf() in C?

两盒软妹~` 提交于 2019-11-28 21:34:26
Actually I am trying to write my own printf() in C by using varags. But I am not getting the correct solution for this. Can anyone help me out? vishal Before implementation of printf( ) function we have to deal with unusual problem which is variable arguments. As we know that printf can take many arguments besides string. So we have to use a standard library called stdarg.h to handle this variable argument problem. In this implementation context, we don’t need learn whole stdarg.h library because we use some macro functions of these library which is understandable directly by our C program.

Where can I browse the sourcecode for libc online (like doxygen) [closed]

一曲冷凌霜 提交于 2019-11-28 21:00:21
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . Sometimes I want to look up the implementations of functions in the stdlib, I've downloaded the sourcecode, but it's quite messy. Just greping is not really suitable because of the many hits. Does anyone know a webpage doxygen style that has the documentation. The same goes for the linux kernel. Thanks 回答1: How