91 questions from the last 30 days
24
votes
4
answers
3k
views
What is the difference between iszero(x) and x == 0 for floating types?
C23 added the classification macro iszero for testing whether an argument value is zero, specified like:
int iszero(real-floating x);
What is the difference between using iszero(x) and writing x == 0?...
27
votes
2
answers
2k
views
Why does clang zero "eax" before calling a function with unspecified parameters, but gcc doesn't?
Consider this C (not C++!) code:
int g();
int f() {
return g();
}
Clang (with any optimization level above zero) compiles this to:
f:
xor eax, eax
jmp g@PLT
I am trying ...
15
votes
3
answers
2k
views
Why would one reset local variables at the end of a C function?
Take a look at the function SHA1Transform taken from an SHA1 algorithm on Github. Assuming SHA1HANDSOFF is defined, the function looks like this:
void SHA1Transform(
uint32_t state[5],
const ...
12
votes
4
answers
1k
views
Why don't C overflow checks use CPU flags?
Both amd64 and arm64 architecture processors have an overflow flag. However, in C, the most common method to detect whether an operation causes overflow/underflow is to make functions like these:
int ...
10
votes
1
answer
674
views
variable name change causes segfault (datatype same name as the variable, used with malloc)
Okay it probably doesn't. That would be weird. I'm guessing, I'm allocating my memory wrong somehow.
#include <stdio.h>
#include <stdlib.h>
/*I have two Structs. In one there is a pointer ...
Advice
2
votes
11
replies
389
views
I can't understand structure padding in C/C++
I watched a few tutorials on YouTube. They said to assume a 32-bit architecture and then said "a single CPU cycle can only access a word (equals the CPU's native size i.e 4 bytes) at a time from ...
2
votes
5
answers
330
views
Understanding undefined behaviour
I have read about undefined behaviour for the C3 language here:
https://c3-lang.org/language-rules/undefined-behaviour/
Here is the example from the page.
Assume following code:
uint x = foo();
uint z ...
0
votes
4
answers
442
views
Is there a way to determine if a shared object ( .so ) file was generated from a c or c++ code?
I have a Shared Object (.so) file. Is there like any specific way i can check if it was generated from a c or c++ code file. Can we get any hints or specifics from the file to make an educated guess ?
...
Best practices
0
votes
15
replies
217
views
Macro to return a value and with goto inside
Working in C with a library which has a very complicated set of return codes.
I have a function which takes a library's error code and converts it into my error code. If the resulting "my" ...
Advice
1
vote
11
replies
215
views
Is it safe to call fscanf after EOF?
I'm parsing a file, several fscanf attempts per line
Do I have to check for EOF in the returns after each one or can I do a big for-loop and check EOF in the skip-to-next-line routine?
cppreference ...
15
votes
3
answers
412
views
Workarounds for using __has_include when it may or may not be defined - Is it valid to pass __has_include as a macro argument?
Often, I'd like to chain preprocessor conditionals involving __has_include (defined in C++17 and C23, and supported in earlier versions as an extension by many compilers (GCC 4.9.2 and up, and Clang ...
7
votes
2
answers
303
views
What is the difference between fmax, fmaximum, and fmaximum_num, and which one should I use?
C23 now has four different ways of computing the maximum or minimum of two floating-point numbers:
x > y ? x : y for maximum (or the other way for minimum)
fmax / fmin
fmaximum / fminimum
...
Advice
0
votes
8
replies
195
views
Why does `tcc` compile local variables onto the stack?
I have the following C code (an MWE):
#include<time.h>
void fun() {
asm("arg1:");
struct timespec const a = { .tv_sec = 10, .tv_nsec = 0 };
asm("call_nanosleep:");
...
2
votes
3
answers
256
views
Display contents of allocated memory byte by byte? [closed]
I'm trying to display the contents of some allocated memory byte by byte:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int len;
int* vec[];
} vec_t;
int main(void) {
int ...
-7
votes
3
answers
299
views
Why does *(p+1) print 20 instead of 10 when using pointers in C?
I’m learning pointer arithmetic in C and trying to access array elements using a pointer. Here is my code:
#include <stdio.h>
int main() {
int a[3] = {10, 20, 30};
int *p = a;
...