156 questions from the last 30 days
28
votes
1
answer
4k
views
Why do C compilers still prefer push over mov for saving registers, even when mov appears faster in llvm-mca?
I noticed that modern C compilers typically use push instructions to save caller-saved registers, rather than explicit mov + sub sequences. However, based on llvm-mca simulations, the mov approach ...
10
votes
3
answers
1k
views
Why doesn't the Windows C compiler reuse incoming shadow space in noreturn functions?
I mainly use Clang, but I have also explored other compilers during my experiments, such as MinGW GCC and MSVC, but they all have this problem.
cd C:\Users\Moi5t
clang -v
Output:
clang version 20.1.7
...
17
votes
2
answers
1k
views
How to set up a simple hello-world example where a C function calls a Cython function calling a Python function?
I am having trouble making a simple "Hello World" python function which I can call from a C program.
Here is the contents of my helloworld.py file:
def hw():
print("Hello World")
...
5
votes
5
answers
367
views
Why Zero has no decimal integer spelling in C?
I am reading modern C, and on page 66, I come across following section:
Remember that value 0 is important. It is so important that it has a lot of equivalent
spellings: 0, 0x0, and ’\0’ are all the ...
11
votes
1
answer
759
views
Is CPP TrivialCopyable class effectively a C struct?
During coding of std::atomic, CAS, etc, I always struggle to memorize the definition of CPP class being "TriviallyCopyable".
Now I am gradually switching to C world, I accidentally found ...
15
votes
1
answer
683
views
Weird behaviour of Java FFM on Windows platform when creating upcalls accepting both structure and pointer parameters
On Windows platform, when creating upcall stubs with Java 22 FFM APIs, if the callback functions has both structure (larger than pointer size) and pointer parameters, the MemorySegments accepting ...
0
votes
7
answers
260
views
How does '0' + (n % 10) convert an integer digit to its character representation in C?
I'm learning about converting numbers to characters in C, and I came across the expression:
char c = '0' + (n % 10);
I understand that '0' is a character and n % 10 extracts the last digit of a ...
6
votes
2
answers
209
views
How do I calculate the end address of a C struct in memory?
I have the following structure in my .c file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student {
char* name;
int age;
int id;
} Student;
...
10
votes
4
answers
280
views
Why does this lookup table sine estimation perform worse when using float instead of double?
I've written a simple sine estimation function which uses a lookup table. Out of curiosity, I tried both float and double types, expecting float to perform a bit better because of being able to pack ...
4
votes
3
answers
192
views
Is sizeof(pointer) the same as processor's native word size?
Say, is sizeof(void*) the same as the size processor can atomically access per instruction?
For example, 32-bit processor can read aligned 4 bytes atomically, 64-bit processor can read aligned 8 bytes ...
6
votes
4
answers
528
views
how to avoid buffered printf() blocking
Is there some way to know ahead of time when buffered printf() will block, giving an opportunity to avoid the call and either delay or discard output? For example a low level method of knowing when ...
3
votes
4
answers
173
views
Is there any better way to do count 1s of a binary number when input is given in decimal? [duplicate]
The problem is to find out what is the least number that has the same number of 1s in its binary as the given number by user. I believe there might be functions that I definitely don't know about or I ...
5
votes
1
answer
230
views
Why does the C compiler save registers in a noreturn function?
I mainly use clang, but I have also explored other compilers during my experiments, such as MinGW GCC and MSVC, but they all have this problem.
E:\code\test>clang -v
clang version 20.1.7
Target: ...
3
votes
2
answers
139
views
C How to generate an arbitrary permutation within range [i,j]? [closed]
Does C standard library provide any function that generate a random permutation of consecutive numbers within a range?
How to make a function that efficiently does it? I'm thinking of making a random ...
5
votes
2
answers
238
views
How do I properly use scanf and what are its limits?
I am currently trying to make a simple little program (BMI index calculator), since I have just started learning C. Currently using Eclipse.
Here is my code:
#include <stdio.h>
int weight, ...
-1
votes
2
answers
324
views
standards compliant way to write array bound checks in C or C++ that can't be optimized away? [closed]
is there a standards compliant way that can't be optimized away of writing a bounds check in C or C++? since the compiler can assume that the UB behaviour never happens, this means it can just throw ...
5
votes
2
answers
159
views
Differentiating pointer declaration and multiplicative expression
According to the C language grammar defined in the standard, how will the statement a * b; be parsed?
Is it considered as a declaration of a pointer b to an object of type a? Or is it considered as an ...
1
vote
3
answers
238
views
What is the "cleanest" way to clear the console in C
First, I'll explain my purpose. For fun I'm wanting to create a character-based renderer in the terminal, something simple that can just draw shapes by printing "#" and " ". ...
5
votes
2
answers
109
views
Using sizeof(pointer) with strncpy
I have this parser, I want to use the size of the pointer (that is 8 bytes) in the function strncpy, I was able do it with the "method" part, why does it crash the "path" part ?
It ...
-1
votes
2
answers
139
views
What does the macro condition "#ifdef __augmented" check in C?
#ifdef __augmented
#define Prefix "@"
#else
#define Prefix ""
#endif
#ifdef __cplusplus
#define Suffix "++"
#else
#define Suffix "&...
2
votes
3
answers
151
views
how to force compiler error with strcpy() when destination is const
How can I make the strcpy() function trigger a compiler error when the destination is a const pointer?
The prototype is strcpy(char *dest, const char *src) however if I specify a const char * for ...
3
votes
2
answers
125
views
Understanding static/dynamic array access with pointer arithmetic in C
I'm trying to understand how pointer arithmetic works in C.
Say if I have two arrays, one static and the other dynamic like so,
int a[10];
int * b = malloc(sizeof(int) * 10);
And lets assume each ...
-1
votes
3
answers
236
views
Does there exist a reversed comma operator in C?
In C, if we want to evaluate multiple statements as one, we can use the comma. This evaluates both args in order, then returns the value of the second. However, what if I want to evaluate two ...
2
votes
2
answers
119
views
Why does my function replace my letters with weird characters? [closed]
My goal is to recreate the strcat() function without libraries:
#include <stdio.h>
char *ft_strcat(char *dest, char *src)
{
int i;
int j;
i = 0;
j = 0;
while (dest[i] !...
1
vote
2
answers
124
views
Can I tell my compiler that a floating-point value is not NaN nor +/-infinity?
I'm writing a C function float foo(float x) which manipulates a floating-point value. It so happens, that I can guarantee the function will only ever be called with finite values - neither NaN's, nor +...
3
votes
1
answer
120
views
Are `asctime_r` and `ctime_r` standard in C?
As we already know, asctime and ctime are not thread-safe since they return a pointer of the internal static variables, cppreference only list the alternatives asctime_s and ctime_s which receive ...
0
votes
3
answers
153
views
C program won't skip second half of While statement [closed]
I have this homework program to calculate mpg. The -1 is the sentinel value which is supposed to skip the questions asking the user for gas and miles when inputted. At the moment, the program ...
1
vote
3
answers
139
views
Inputting to a cstring of undefined size without wasting memory [closed]
I know beginner level C++. In C++ when I want to, for example. input the user's name, all I need to dos is:
getline(cin,str);
but I am trying to learn C.( I want to play with kernels and embedded ...
3
votes
3
answers
157
views
Simple XOR loop in NASM
I am making a PE .exe packer in C and assembly. In C, I do the things like create a new .packed section header, changing Entry Point to that new section, changing sizeofimage, etc. In my C code, I ...
4
votes
2
answers
107
views
Using multi-char literals as enum values
To make debugging easier, one could set enum values to be multi-char literals, which could then be printed. I never saw this being done, and so I'm wondering if there are any reasons why this wouldn't ...
3
votes
2
answers
140
views
Programme works but says "warning: integer constant is so large that it is unsigned", solution?
I am trying solve the problem posed in this question that asks << 1 operation be performed on a 64 bit number using NAND operation only and without using any arithmetic operation. My attempted ...
3
votes
3
answers
195
views
While loop in C clipboard program doesn't work properly?
I want this program to first ask user to start or exit which works fine:
#include <stdio.h>
int main()
{
int ask;
char ask2;
char clip[100];
printf("1.start\n2.exit\n&...
2
votes
3
answers
173
views
Writing a Von Neumann Ordinal generator in C : Problem with malloc
I want to write a computer programme that will do the following things :
1a. It will make an array 3 characters long.
£££
2a. It will then initialize the array with the string "{_}" and ...
3
votes
3
answers
107
views
How to handle EINTR in connect()?
I'm trying to handle EINTR error from POSIX connect call. I'm running on OSX this connect code:
JNIEXPORT jint JNICALL Java_io_questdb_network_Net_connect
(JNIEnv *e, jclass cl, jint fd, jlong ...
3
votes
1
answer
174
views
What's wrong with this thread-safe circular buffer?
I am playing with a toy circular buffer where MT involves:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define N 8
int *arr;
int ...
3
votes
2
answers
139
views
Justification of storing metadata in 2 least significant bits of address
I read this code that seems to use lower 2 bits of addresses to store binary flag, see below (obfuscated due to sensitivity):
void *setFlag4Addr( void *addr, BOOLEAN flag )
{
// long for 32-bit ...
4
votes
2
answers
191
views
Can I printf a half-precision floating-point value?
I have a _Float16 half-precision variable named x in my C program, and would like to printf() it. Now, I can write: printf("%f", (double) x);, and this will work; but - can I printf x ...
3
votes
2
answers
92
views
Codeforces 39J: sample input pass but fail on submission. I can't find logic error
The problem gives 2 lower-case words where one is exactly 1 character bigger than the other. The problem asks you to write a program to find if when removing a single character from the longer word ...
4
votes
2
answers
153
views
Unexpected compiler warning - printf format specifiers
I have the following warning generated by a printf():
warning: format '%llu' expects argument of type 'long long unsigned int', but argument 7 has type 'uint64_t' {aka 'long unsigned int'} [-Wformat=]...
0
votes
4
answers
173
views
C Switch Function
I created a switch function, and when the user enters anything other than 1-4, I want the main() function to be called so the user can be prompted to enter a number 1-4 again. Here is my code:
#...
1
vote
3
answers
80
views
How do I update an address with a constant minus an address in a circular buffer
I have pointer into a delay line that I wish to update.
If the pointer (p) goes past the end of the buffer, I wish to wrap around to the start plus some value.
_Complex float buffer[BUF_LEN];
...
3
votes
1
answer
113
views
Why does setjmp/longjmp cause 0xC0000028 in LLVM IR?
I write a compiler that generates LLVM IR code and then .obj file. Then I link it with some required .lib files. (msvcrt.lib, ucrt.lib, vcruntime.lib). I declared setjmp and longjmp there as follows:
...
4
votes
1
answer
112
views
Pass C struct as "deep" const
I have a struct which holds some pointers to data it does not own, e.g.:
struct s {
int* x;
int* y;
};
I can then pass it as a const pointer itself:
int foo(const struct s* s) {
return *s-...
0
votes
2
answers
144
views
How can I call free and set the pointer to NULL inside a function correctly and why?
I am confused about &str[0], which is equal to str.
If I can do str = NULL, why can’t I do &str[0] = NULL or why does it not work?
Also, since free(str) and free(&str[0]) both work to free ...
3
votes
1
answer
128
views
How can I read environment variables in the early stages?
Use LD_PRELOAD to load shared objects with the initfirst flag. Calling the getenv() function from a function with __attribute__((constructor)) returns NULL. I think this is probably because the ...
3
votes
3
answers
106
views
Stack Smashing with strtol
I'm trying to learn about C and how to use its functions, etc. The man pages suggested to use strtol instead of atoi, so I came up with this code which works without issues:
#include <stdio.h>
#...
-4
votes
3
answers
253
views
Can linking be nested (e.g. by using intermediate object files)? [duplicate]
Commonly the linker is only invoked once. A linear list of input files can be specified for symbol resolution; there are flags for looping through the linker inputs. But for more sophisticated ...
4
votes
3
answers
93
views
What determines when an array is considered a variable length array
devices is a large, hardcoded array of structs. It is useful to have an easy way of storing the length of the array that is automatically updated when changes are made to the hardcoded values. my ...
3
votes
2
answers
149
views
Mixed-type hash table design
I think this is a design problem. I’m trying to implement a hash table library in C89 in which the user will be able to insert mixed-type literal keys and values, e.g., HT_SET_LITERAL(&ht, "...
4
votes
2
answers
58
views
Lex Setup Correctly Validating Assignments but not Expressions
I recently revisited an old assignment that I did not get to work and I am still curious as to why it isn't.
This assignment was to create a lex regular expression in C that would validate both ...