libc

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

倖福魔咒の 提交于 2019-11-27 18:55:57
问题 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. 回答1: The important aspect of the fused-multiply-add instruction is the (virtually) infinite precision of the intermediate result. This

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

会有一股神秘感。 提交于 2019-11-27 17:32:14
问题 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

Unit testing for failed malloc()

前提是你 提交于 2019-11-27 16:30:50
问题 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

Linux function to get mount points

試著忘記壹切 提交于 2019-11-27 14:04:51
Is there a function (or interface; ioctl, netlink etc) in the standard Linux libs that will return the current mounts directly from the kernel without parsing /proc? strace ing the mount command, it looks like it parses files in /proc Please see the clarification at the bottom of the answer for the reasoning being used in this answer. Is there any reason that you would not use the getmntent libc library call? I do realize that it's not the same as an 'all in one' system call, but it should allow you to get the relevant information. #include <stdio.h> #include <stdlib.h> #include <mntent.h> int

What's the difference between hard and soft floating point numbers?

二次信任 提交于 2019-11-27 09:12:13
问题 When I compile C code with my cross toolchain, the linker prints pages of warnings saying that my executable uses hard floats but my libc uses soft floats. What's the difference? 回答1: Hard floats use an on-chip floating point unit. Soft floats emulate one in software. The difference is speed. It's strange to see both used on the same target architecture, since the chip either has an FPU or doesn't. You can enable soft floating point in GCC with -msoft-float. You may want to recompile your

Initializing sigset_t in Rust

回眸只為那壹抹淺笑 提交于 2019-11-27 08:37:10
问题 I'm trying to learn more about the FFI in Rust and linking with C libraries (specifically libc ). While on my "quest" I came accross the following problem. Normal pattern in C void(* sig_set(int sig, void(*handler)(int))) { // uninitialized sigaction structs struct sigaction new_action, old_action; // assign options to new action new_action.sa_flags = SA_RESTART; new_action.sa_handler = handler; sigemptyset(&new_action.sa_mask); if(sigaction(sig, &new_action, &old_action) < 0) { fprintf

Is snprintf() ALWAYS null terminating?

本小妞迷上赌 提交于 2019-11-27 07:21:36
Is snprintf always null terminating the destination buffer? In other words, is this sufficient: char dst[10]; snprintf(dst, sizeof (dst), "blah %s", somestr); or do you have to do like this, if somestr is long enough? char dst[10]; somestr[sizeof (dst) - 1] = '\0'; snprintf(dst, sizeof (dst) - 1, "blah %s", somestr); I am interested both in what the standard says and what some popular libc might do which is not standard behavior. As the other answers establish: It should : snprintf ... Writes the results to a character string buffer. (...) will be terminated with a null character, unless buf

GDB debugging warnings

落爺英雄遲暮 提交于 2019-11-27 07:16:47
问题 When I try to debug my core-dump via gdb either in Qt or directly from terminal, it gives me bunches of warnings like below. Therefore my backtrace is not working properly. warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available. warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available. warning: Could not load shared library symbols for ). Do you need "set solib-search-path" or "set

How to tell which interface the socket received the message from?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 06:42:29
问题 If a socket is bound to IN6ADDR_ANY or INADDR_ANY and you use a call such as recvfrom() to receive messages on the socket. Is there a way to find out which interface the message came from? In the case of IPv6 link-scope messages, I was hoping that the from argument of recvfrom() would have the scope_id field initialized to the interface Id. Unfortunately it is set to 0 in my test program. Anybody know of a way to find out this information? 回答1: Apart from binding to each interface, I'm not

How do I reimplement (or wrap) a syscall function on Linux?

孤者浪人 提交于 2019-11-27 06:34:09
Suppose I want to completely take over the open() system call, maybe to wrap the actual syscall and perform some logging. One way to do this is to use LD_PRELOAD to load a (user-made) shared object library that takes over the open() entry point. The user-made open() routine then obtains the pointer to the glibc function open() by dlsym() ing it, and calling it. The solution proposed above is a dynamic solution, however. Suppose I want to link my own open() wrapper statically. How would I do it? I guess the mechanism is the same, but I also guess there will be a symbol clash between the user