164 questions from the last 30 days
23
votes
1
answer
3k
views
How does the compiler interpret '____'?
I have found a piece of code that defines register access permissions for a chip. Using arm-none-eabi-gcc it compiles correctly.
Here is the code:
static const uint8_t defaultRegisterAccess[...
Advice
1
vote
21
replies
261
views
How to read a specific text file format in C
I just wanted to ask if it's possible to read to a specific file format, say if I wanted to read from a file that has a format of something "info.uc" or "main.uc", how would I go ...
Advice
1
vote
22
replies
304
views
Is there a reason why signed integer is used for the ssize_t?
From the man pages, I found that size_t has the range of 0 to SIZE_MAX, and ssize_t has the range of -1 to SSIZE_MAX. So, after printing those values on a 64bit system, I have the following results:
...
11
votes
7
answers
1k
views
Is it possible to expose C++ template struct (fully specialized) to C?
I want to use circular queue with different types and lengths in a project which mainly consists of C code. I'm thinking about implementing the circular queue as a C++ template struct. How to expose ...
6
votes
3
answers
384
views
Find any single 2D point within radius, but fast
I've got a list P of 2-dimensional points, 100 ≤ |P| ≤ 100000, and a list C of circle centers, 100 ≤ |C| ≤ 100000.
All coordinates (x,y) ∈ P or (x,y) ∈ C are integral, 0 ≤ x,y ≤ 4095.
P and C may ...
7
votes
2
answers
366
views
Error “type containing an unknown-size array is not allowed” using official SDK header
I’m using an official SDK that provides an api.h header file. When I try to compile, I get the following error, with wriggles on cmd:
type containing an unknown-size array is not allowed
Here are the ...
3
votes
3
answers
197
views
strlen() returns 1 larger on linux than windows
I am building a c extension for a python module and it needs to work the same on a windows machine and on a raspberry pi.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#...
10
votes
1
answer
493
views
Is this a well-defined way to access bitfield bits by index in C11
The order of bits in C11 bitfields is implementation-defined, and so is any padding in between them. This makes it difficult to access the bitfields by index at runtime, even though most ...
Best practices
1
vote
7
replies
161
views
Memory allocation when using a linked list
So I'm making a linked list for one of my school assignments in C. I found this website that has some helpful code snippets, and I noticed that they use malloc() to allocate more memory every time ...
Best practices
1
vote
7
replies
124
views
Is there way to remove coredump generation for SIGQUIT for C program
I have automated tests for fairly complex software, which send SIGQUIT signals at program startup. If the signal arrives before the handler is installed, a coredump is generated, causing the test to ...
3
votes
3
answers
155
views
Wrong result when adding up to result in my number conversion program [duplicate]
I am currently writing a program in C to convert numbers between bases as an exercise. It asks the user for an input and for the base, that the number given is. It then gives the input into the ...
6
votes
2
answers
275
views
How can I reverse the endianness of an integer that is not a byte-multiple size?
All the existing threads I can find for this type of problem only refer to reversing endianness on byte-multiple bit sizes (i.e, 8, 16, 32, etc), but not non-byte-multiple (i.e, 5, 13, 20, etc), so ...
2
votes
2
answers
151
views
I am trying to translate my C code into assembly but my code is not working. What is wrong with it?
I am trying to use an MSP-EXP3430G2ET with a Buzzer and a potentiometer on CCS. The goal of the program is to have 2 modes, a high frequency mode and a low frequency mode. If you press the button, it ...
3
votes
2
answers
132
views
Accessing a union defined as a type [closed]
typedef union
{
unsigned long anyvariable;
float output;
} map;
return (*(map*)&fraction32).output;
I copied this method of accessing the members of a union which ...
3
votes
2
answers
145
views
GCC pragma: error: expected expression before ‘#pragma’
Please consider the following minimal example:
#include <stdio.h>
// Compile with:
// gcc -g -o myprogram main.c
//#define SPECIAL
typedef struct {
int id;
char name[50];
float value;
} ...
-3
votes
1
answer
181
views
How does fgets work when reading a file or stdin? [closed]
I am following the Bro Code course of C and there is something I am not fully understanding. This might be a basic question, but I cannot figure out what is going on behind the function fgets.
When ...
1
vote
2
answers
210
views
VSCode IntelliSense reports include errors for mixed C/C++ Code
I am mostly programming in C, yet some of my code is in C++. The code compiles fine using gcc/g++ under Linux, yet VisualStudio Code's IntelliSense is reporting include errors with the following setup:...
0
votes
1
answer
158
views
Input memory leak? [duplicate]
In my homework assignments I've got a task to get an input using scanf() of an integer number and check if its a positive negative or zero.
I've coded this:
#include <stdio.h>
int main()
{
...
2
votes
2
answers
112
views
Unexpected behaviour from minimax algorithm
I recently started learning C and as a learning project I tried creating a tic tac toe bot using the Minimax algorithm. Here is the code that I have written so far:
#include <stdbool.h>
#include ...
3
votes
1
answer
179
views
Memory Layout of C Programs: How is size of stack-heap space determinded?
I was reading an article on the Memory Layout of C Programs; https://www.geeksforgeeks.org/c/memory-layout-of-c-program/.
It mentioned, under "4. Stack Segment":
When the stack and heap ...
1
vote
1
answer
127
views
Why does my multithreading/pointers code print random values?
My code should print the numbers given as command line arguments. Instead it prints random-looking values.
What is going on and how can I fix it?
#include <stdio.h>
#include <pthread.h>
#...
1
vote
2
answers
148
views
How to set int array pointer within for loop in c
The following code is trying to set the test array in the first for loop, but it can't be used in the second for loop.
int main()
{
int *test[3];
/** Try to set the array value one by ...
3
votes
2
answers
97
views
Why do I get a Run-Time Check Failure #2
Im new to C Programming and the Windows API im getting "Run-Time Check Failure #2 - Stack around the variable 'pMyStringAdress' was corrupted." after compilation in Visual Studio
Thats my ...
8
votes
1
answer
174
views
If I start two strictly CPU bound threads bound to the same CPU, and one is SCHED_IDLE, why does it ever schedule?
If I start two threads bound to CPU 0, where one is SCHED_IDLE, then why is it ever scheduled when the other thread is CPU bound at either normal priority, or at SCHED_FIFO?
For SCHED_FIFO I get it, ...
Advice
2
votes
7
replies
117
views
Do all definitions of an inline function in C share the same address?
I know that it's possible to take the address of an inline function, just like with ordinary functions. But if each translation unit has its own inline definition, and I take the address of the ...