408,324 questions
2
votes
0
answers
64
views
How to properly solve "invalid use of undefined type ‘struct Pixa’"?
I am including OCR with Leptonica and Tesseract capi.h in my C project. For regular images loaded as Pix, all is good, but for multipage TIFFs loaded as Pixa, I get the following compiler error:
ocr.c:...
2
votes
0
answers
36
views
Why is my “Second naive” SGEMM kernel faster than the “global memory coalesced” version?
I am benchmarking several very simple CUDA SGEMM kernels on an NVIDIA Hopper GPU (H800, sm_90), and I observed something that I do not fully understand.
I have two kernels that, to my understanding, ...
4
votes
1
answer
62
views
Reproducing buffer overflow
Trying to reproduce buffer overflow on Ubuntu 24 for learning purposes.
So the goal is to call call_me_twice second time by passing its address into input string.
$ cat bof.c
#include <stdio.h>
...
0
votes
0
answers
80
views
prof implementation of The Practice of Programming (1999) by Kernighan/Pike [closed]
I've been reading chapter 7 on Performance in The Practice of Programming from 1999 by Brian W. Kernighan and Rob Pike. On page 172, they show the following commands for profiling a C program:
% cc -p ...
-4
votes
0
answers
81
views
CUDA: Why is my GPU not detected by cudaGetDeviceCount()? [closed]
For some reason when I build such a simple program in Visual Studio 2022:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "cuda.h"
#include <...
Advice
0
votes
1
replies
27
views
How does the version of Bluez correspond to the version of the Bluetooth specification?
Recently, I have just started to get acquainted with the bluez protocol stack. I would like to know how the version of bluez corresponds to the official Bluetooth specification version. For instance, ...
6
votes
1
answer
101
views
ungetc after a fwrite, fseek and fwrite fails
After reading a draft of the c23 standard (n3220), I was trying to gain an understanding of ungetc by writing a simple example.
In This Example I:
fopen a file named file.txt for updating, either ...
1
vote
0
answers
38
views
Readers-writer spinlock implementation for lab locks MIT OCW OS
I am trying to learn some operating system engineering so I came about MIT's operating system engineering course which has been wonderful so far.
The problem I'm having is with the second problem read-...
10
votes
3
answers
202
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 ...
Advice
0
votes
5
replies
101
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:");
...
1
vote
3
answers
228
views
Why doesn't my float comparison work in C?
My expected outcome is that I'm able to compare floats up to the sixth decimal point. The actual results are that they that it's incorrect, only sometimes though.
Bonus if you can tell me why without ...
5
votes
1
answer
141
views
Why don't function pointers allow adding CV-qualifiers to pointer arguments?
I am work a mature codebase, one of the signs of its age is lack of consts which make the code difficult to reason about when refactoring.
I have found a function which takes an argument as a mutable ...
Advice
1
vote
6
replies
107
views
How do I hint CPU to move data to a higher/lower cache level without flushing it beforehand
I''m writing a cache-friendly program with a lot of random read-writes and let's say I use a _mm_prefetch _MM_HINT_T0 intrinsic to load some data into L1 cache. Then I wanna prefetch another things to ...
3
votes
2
answers
141
views
How can I get the file size with FindFirstFileW and FindNextFileW? (beginner)
I'm a beginner with the Windows API and C Programming, I am trying to get the file size from my files, but my output is not so as expected.
That's my code:
#include <Windows.h>
#include <...
3
votes
1
answer
138
views
Getting the same result -> hot potato game in C using Deque
I’m currently learning about data structures and algorithms (DSA) by reading the book “Learning JavaScript Data Structures and Algorithms – 2nd Edition” by Loiane Groner. I am implementing the DSAs in ...
5
votes
2
answers
136
views
Error using _Generic selection to distinguish between char* and struct pointer*
I have a simple struct in C and I used typedef to create an alias (db) for it:
typedef struct {
char* name;
} db;
I then allocated memory on the heap for a pointer:
db *a = malloc(sizeof(db));
a-&...
Best practices
0
votes
15
replies
161
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" ...
5
votes
1
answer
168
views
How to handle strict FIFO waiters with futex when a waiter crashes?
I’m building a shared-memory, event-driven allocator on Linux with:
Multiple producers, single consumer (MPSC)
Variable-size allocations from a shared memory pool
When memory is unavailable, ...
Best practices
0
votes
9
replies
136
views
What's the best way to get a live update of a laptops battery percentage and status via a C program?
I'm creating a program that involves showing the battery percentage and charge status. I want to be able to have the program update as close to instantly as I can get.
Every example I could find ...
24
votes
4
answers
2k
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?...
Advice
0
votes
0
replies
89
views
SuperKarel - Finding a target sum path in a binary tree
Karel is placed in a world that contains a binary tree represented by beepers. Each node of the tree is located on a single field and may contain one or more beepers. The root of the tree is placed ...
5
votes
1
answer
168
views
MISRA C:2012 violations with enums in C99
I'm working on a C99 project that is required to follow the MISRA C:2012 standard.
Whenever I use an enumeration type, I get a violation of rule 10.4 or 10.5.
As a static analysis tool, I use the code ...
1
vote
0
answers
107
views
DroneCAN: GNSS-node CRC errors
I am trying to make a Dronecan GNSS-node from a stm32g4, using fdcan but with a classic can implementation(8byte payloads), I use the canard lib for managing the node. but when trying to receive the ...
4
votes
1
answer
247
views
How to FIX printf resulted SIGSEGV when compiled with openssl?
I am trying to compile my code with openssl library. My code is printf then the program received SIGSEGV signal.
Code of test.c:
#include <winsock2.h>
#include <openssl/ssl.h>
#include <...
1
vote
0
answers
123
views
Small error in CUDA stream mandelbrot kernel
I'm new to CUDA, and I can't see where's the mistake in my kernel. Upon comparing it with the result from my professor, the difference was extremely small, with the pixel average being off by 0,0039. ...
1
vote
0
answers
93
views
understanding debugger output stm32nucleof401re
I am getting the following error (debugger output) when i try to do "ST_LINK: attach to application" in vscode. I have no idea what is going on. Any help is greatly appreciated.
I use WSL ...
14
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 ...
1
vote
2
answers
204
views
Pure Type Punning In C
I want to do pure type punning without memcpy in C using pre-allocated page which I made with mmap() on Linux, because I understand the architecture and how physical ram works, alignment & ...
2
votes
1
answer
168
views
How do you replace multiple elements of an array in C with elements of a different array
If I wanted to replace large chunks of an array in C with items from another list, are there any solutions that don't involve for loops or writing code like the following?
int64_t foo[size];
foo[value]...
2
votes
1
answer
124
views
Python GC for ctypes memory / reference "holding"
I have written a CFD code which I POC'd in Python. In order to accelerate the computationally expensive parts, I have rewritten these methods in C. I am calling them using ctypes which seems to work ...
3
votes
1
answer
229
views
Include a C header with complex float types in C++
I can't figure out an easy method to include a C header with C complex types in C++. Assume you have the following files:
c_source.h
#include <complex.h>
#ifdef __cplusplus
extern "C"
...
-2
votes
0
answers
147
views
Printf not waiting for a delay [duplicate]
I'm trying to make a short puzzle game that plays in the terminal, but want to use delays to make dialogue work better. The delays are happening, however they're all happening at once, and the print ...
-1
votes
0
answers
136
views
Cannot call external C function in Rust [closed]
When I run cargo run, I receive the following error. I am not able to figure why rust is not detecting the badmath function.
➜ unssafe git:(master) ✗ cargo run --verbose
Fresh find-msvc-tools ...
5
votes
1
answer
181
views
How to make interruption handler in C
I have this C code:
void print_hello(void);
void far int_handler(void);
void kernel(void)
{
// Set interruption handler in IVT
*((unsigned short*)(0x30 * 4)) = (unsigned short)int_handler;
...
0
votes
0
answers
130
views
incorrectly read floats with fscanf [closed]
I am writing a code using Virtual C IDE that is to read in a text file with hundreds of lines, each containing a date, time, and eight floats; all these are assigned to arrays of according variables. ...
-1
votes
1
answer
142
views
How to use strtok to separate into tokens and append to an array [closed]
I am working on a function where I will be recieving data from an SPI communication and then get the date/month/year from them
I have created a function to do the latter
void set_file_header(const ...
2
votes
5
answers
302
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 ...
4
votes
2
answers
155
views
Why is the number different and the string the same?
So I have this code here
#include <stdio.h>
typedef union Converter {
char *c;
int i;
} Converter;
int main(int argc, char **argv) {
Converter f;
f.c = argv[argc - 1];
int ...
2
votes
1
answer
102
views
Network namespace MTU setting in C vs the 'ip' utility with big Ethernet frames
I have this Linux C project that (among others) sends (bigger than 1500 bytes) raw Ethernet frames between network interfaces of different processes. I have a bunch of system tests where this ...
-2
votes
1
answer
160
views
C collision code between a rectangle and circle behaving incorrectly at high speeds of circle [closed]
I have a function which handles collision between a rectangle and a circle, the rectangle is stationary, and the circle is moving towards the rectangle at some speed. When I input lower speeds then ...
Advice
0
votes
3
replies
95
views
Design and development of a specification language
I developed a specifications language. Depending on the comments on this, I’ll call it a “code-aware specifications language” or a “verification-aware specifications language”. The idea I’m playing ...
1
vote
0
answers
186
views
How can I bypass the ASLR c code that was compiled as 64-bit? (school assignment : “within 10 minutes.”)
How could I get past it if the code is compiled as 64-bit? There’s no need to obtain a root shell; it’s sufficient to just verify that the address was guessed correctly. In my assignment, to bypass ...
5
votes
1
answer
136
views
SAMD11 I2C (SERCOM0) never leaves IDLE state
I am trying to get I²C to work on the SAMD11C14A (ATSAMD11D14A-SSUT).
Just for context the MCU is soldered on a custom PCB with nothing but 4.7kΩ pull up resistors soldered to the paths and no other ...
Best practices
1
vote
4
replies
129
views
What are the strengths and weaknesses of using VTable/COM-centric APIs?
I'm currently designing a library,
I want this library to maintain a stable interface for old applications,
However, it should be expandable.
It acts as a hardware abstraction layer.
The graphics ...
Advice
1
vote
11
replies
192
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 ...
Best practices
1
vote
1
replies
62
views
Thread safe initialisation of function-scoped static variables in C11
I wish to initialize a function-scoped static structure in a multi-threaded environment without using a mutex, so that the fast path (i.e. already initialized) involves little overhead.
Initialization ...
4
votes
2
answers
235
views
Dynamic memory allocation
I have the following C code that uses a static global pointer (pStruct) to implement a one-time memory allocation (a singleton pattern for an array of structs).
How should I safely implement a ...
1
vote
0
answers
59
views
Creating IpSet with libipset in c,Create command keeps failing Error: Invalid create command: missing settype
I'm trying to create ipset through libipset from application layer. below i pasted the code for an function which tries to create an ipset with the help of libipset. but for some reason it's not ...
6
votes
1
answer
247
views
Shared assembly between Rust and C using preprocessor
I am using Rust to target a baremetal system.
My startup code is written in Assembly and is in crt0.S, it is shared between Rust, C and Assembly projects.
crt0.S contains:
#include "constants.h&...
6
votes
0
answers
137
views
Why hasn't ASLR randomized the address of my program? [duplicate]
I have recently learned about content related to ASLR.
I referred to this article:
Why aren't glibc's function addresses randomized when ASLR is enabled?
However, I use the Windows 11 system ...