Questions tagged [c]
C is a general-purpose computer programming language used for operating systems, games and other high performance work.
1,330 questions
2
votes
4
answers
160
views
Multithreaded Game Server: Single send() or Many send()s for Game List?
I'm developing a multithreaded game server (C/TCP). I need to send a list of 50-100 available games to a console client.
Option A: Send 50-100 separate messages, using send() for every single ...
1
vote
2
answers
169
views
Where to initialize clients in C server?
I’m writing a multiplayer server in C using sockets and pthreads.
The project is separated by responsibility:
server.c/.h → networking (socket, bind, listen, accept, thread creation)
player.c/.h → ...
1
vote
2
answers
147
views
Should I introduce a controller layer to separate networking from game/player logic in a C server project?
I’m writing a multiplayer server in C using sockets and pthreads.
The project is separated by responsibility:
server.c/.h → networking (socket, bind, listen, accept, thread creation)
player.c/.h → ...
4
votes
6
answers
857
views
Can a language be sound if it doesn't promise safety?
It is said that C's type system is unsound, which means that it has "false negatives", it tells the programmer everything is fine, but then the code fails at runtime. for example, "the ...
-1
votes
1
answer
142
views
How to organize struct-based "methods" in C similar to object-oriented style? [closed]
So I was coding in C for a while now, getting used to language syntax and different styles.
Implemented a couple of simple data structures, algorithms and tried my skills in making Minesweeper clone.
...
1
vote
3
answers
717
views
Should I include the header of the source file always in C/C++?
I have a source file that is used by other sources so it has a header file. If in the header, there are types used that needs inclusion of other headers, I'm better to include this header in its ...
1
vote
3
answers
578
views
Why does C allow escaping apostrophes / single quotation marks in string literals
As far as I can tell, the strings "It's me" and "It\'s me" are always identical. There seems to be no reason when a programmer needs to escape '. Yet, I could not find and ...
1
vote
3
answers
373
views
Is Feeding a Watchdog Timer from an Interrupt Service Routine a Bad Practice?
In an embedded system, I require a watchdog to be able to pass ESD qualifications. Having no experience with watchdogs, I went through this Memfault article. I liked the events "registration"...
2
votes
1
answer
174
views
What architectural connector is a file descriptor?
The software architecture literature has lots of information on software connectors as in component and connector views of architecture.
What kind of connector is a file descriptor? I noticed in the ...
6
votes
4
answers
735
views
Why is there (practically) no 6-byte integer in common usage?
In Postgres, it used to be quite common to use a 4-byte integer auto field for primary keys, until it started becomming somewhat common to run into the 2147483647 limit of 4-byte integers. Now, it's ...
1
vote
3
answers
436
views
Get call graph / path of C with function pointer
I have a C codebase with function calls made through function pointers. I tried using LLVM AST and IR with Python to generate a function call graph, but handling the function pointer calls requires ...
0
votes
2
answers
303
views
What should I consider when determining what `ALLOC_MAX` should be in this type of I/O loop?
For, e.g. determining an amount of memory that is safe to allocate for processing file or device with this type of I/O loop:
HANDLE hFile /* = file open with GENERIC_READ */;
LARGE_INTEGER liSize;
...
1
vote
5
answers
390
views
Anti-pattern? Double header and exposed implementation detail
Consider that I've implemented SHA-256 hashing in C as incrementally updated IUF (init-update-finalize) working context and 3 subroutines. To use it in free-standing environment, or to efficiently use ...
7
votes
2
answers
730
views
When the stack frames become computationally expensive
I've been experimenting with different data structures and algorithms in Python, Java and C to see in what circumstances function/method inlining could bring meaningful gains in terms of the execution ...
3
votes
1
answer
212
views
C++: Good approach to handle libxml2 resource management in a wrapper
I try to write a C++ wrapper to a well-known C library, libxml2. In libxml2, an xmlDocPtr represent an XML document and xmlNodePtr represents a node. An xmlDocPtr contains a root xmlNodePtr and every ...
2
votes
3
answers
270
views
How to decouple spagheti code for unit tests [duplicate]
A little background on the project: we as a company receive a spaghetti source code, and into that we add even more spaghetti code. So with that I want to say that
complete restructuring and ...
0
votes
4
answers
3k
views
Is it a bad practice to return an enum without an enum return type?
I'm specifically asking about C.
Example:
enum numbers {
EVEN,
ODD
};
int isFiveEvenOrOdd(void) {
if (5 % 2 == ODD) return ODD;
else return EVEN;
}
int main(void) {
printf("%...
4
votes
4
answers
1k
views
Is updating a macro value in the Xcode preprocessor's macros violating the open–closed principle?
For example, for some Xcode projects, if I have some places that defines a number at some .cpp files:
const int PAGE_MAX=5;
and a new requirement comes that needs to change PAGE_MAX, I need to modify ...
2
votes
4
answers
509
views
How would I go about writing my own implementation of Win32 functions?
So I am currently coding a C program for Windows and come across a little bit of a problem. I've been compiling using the mingw-w64 toolchain. In my program, I am attempting to remove as many ...
0
votes
1
answer
244
views
Elegant way in C to store many parameters with default value and current value in embedded flash
I'm programming an embedded system that has a number of user configurable parameters, which are stored in flash memory. I have to store a default value for each parameter as well as the user settings. ...
3
votes
1
answer
305
views
C++ vs C console output idioms
I'm struggling to understand pros and cons of the C++ and C approaches to console output. C++ uses the stream approach with concatenation of operator<<, while for "C approach" I mean a ...
0
votes
2
answers
209
views
How might encapsulated source be broken into into multiple files?
I have a project where there is a primary, high-level, opaque struct with many functions that operate on it. Maybe I am simulating a CPU.
How might the corresponding source code be organized?
One way ...
1
vote
2
answers
467
views
How to handle errors of pthreads fundamental lock und unlock functions?
I am writing a little C library for the Raspberry Pi for controlling 433MHz transmitters/receivers. While receiving data the whole application would block, so I decided to put this code into a ...
25
votes
2
answers
7k
views
Why is the term "string" so often abbreviated as "sz"?
A pattern I have noticed in many big C and C++ programs - including Microsoft Windows (REG_SZ type in Registry) and Valve's Source SDK (names of practically every string variable) - is that "sz&...
3
votes
2
answers
1k
views
Maintaining global states in a recursive function
I am writing a recursive function. Some of the parameter data is different for each recursive call but some of the data only need to have one copy existing at any one time during the recursive call. ...
0
votes
0
answers
59
views
Are type, constant/static/global, and pointer prefixes still necessary in C coding standards, considering the capabilities of modern IDEs? [duplicate]
Our coding standards for C include various prefixes for data types, constants, static/global variables, and pointers. These prefixes were originally introduced for code review purposes, but with the ...
21
votes
4
answers
10k
views
In C, if I am passing a file descriptor to a function, should I close it in the function or in the place where I called the function?
I have a situation like the following:
int fds[2];
pipe(fds);
// Write lots of stuff into fds[1]
close(fds[1]);
do_stuff(fds[0]);
// Should I close fds[0] here or at the end of do_stuff?
Which one ...
10
votes
5
answers
13k
views
Why is int in C in practice at least a 32 bit type today, despite it being developed on/for the PDP-11, a 16 bit machine?
For background, the question is to prepare some training material, which should also should explain a bit why the things are the way they are.
I tried to get some idea of how C began based on this ...
1
vote
1
answer
1k
views
Linux poll with a thread pool and multiple events
I am working on a simple client server program in C in which multiple clients will be connected to a single server.
Clients will submit operations/actions to the server and the server will process ...
1
vote
3
answers
250
views
How robust should an interface/implementation be?
In a spare-time project of mine, I implemented RSA public-key cryptosystem.
Because the official PKCS#1 standard specify key formats in terms of ASN.1 syntax and DER/BER coding, which is a coding with ...
0
votes
1
answer
272
views
Is interleaving local variable declarations with assertions and function calls a bad practice?
In my experience, it is customary to place local variable declarations at the beginning of their scope. Several questions in this forum ask whether this needs to be so, and their answers tend to agree ...
7
votes
2
answers
2k
views
How can I make code that is both DRY and fast where intermediate values in a calculation may or may not be needed?
How can I make DRY (lacks repetitive patterns) code that also avoids inefficiencies from using intermediate values in a calculation that might not need to be used?
Here is an example:
In this code, I ...
27
votes
6
answers
28k
views
How can Rust be "safer" and "faster" than C++ at the same time?
I have been told that Rust is both safer and faster than C++. If that is true, how can that be even possible? I mean, a safer language means that more code is written inside the compiler, right? More ...
0
votes
1
answer
196
views
Is it anti-pattern to obtaining public static data from a function?
In C programming, I have a set of information, and I have to ways of providing it to user:
construct a data structure and provide it as an object.
write a function to read them out and return them.
...
1
vote
1
answer
356
views
How to return a result from an active object state machine
I frequently use the concept of Active Objects (https://www.state-machine.com/active-object) combined with state machines when designing code. The key idea behind these is that only "events"...
0
votes
3
answers
2k
views
How to combine epoll + multithreading in an HTTP server
I'm building an HTTP server in C using epoll and pthread. The idea is to have an event loop in the main thread monitoring file descriptor and a thread pool to handle computationally-expensive ...
4
votes
1
answer
341
views
Dealing with global variables required by badly-written library
I am working with a library that is somewhat poorly written. In order to function, it requires several global variables to be declared and sometimes even maintained by my own code. I really don't ...
-1
votes
1
answer
156
views
Why use the same identifier for a macro AND a function (GNOME source code)?
I just happened to stumble across the following question on stack overflow:
In gatomic.c of glib there are several function declarations that look like this:
gboolean
(...
1
vote
4
answers
853
views
Passing arrays as global variables instead of pointers in C
I'm required to write the Low-Level Requirements of a Software Component which shall perform signal processing over arrays of 200k elements of integers/floats which lives in the main memory of the ...
1
vote
3
answers
250
views
TDD - What to do when adding a new function on a dependency causes many previous tests to fail?
I was programming today and encountered something that just feels like I'm doing something wrong (maybe?). I've encountered this situation before, but I wanted to reach out and ask if there's a better ...
1
vote
0
answers
100
views
Mimic public/private data members in C with hidden static arrays
Context
I'm designing the software architecture of a safety critical software written in C, under DO-178C (DAL A) and with a Code Standard based on MISRA-C 2012, but no fully compliant.
It is the ...
4
votes
1
answer
361
views
What is the problem with whitespace in C that Ruby allegedly repeated?
I'm reading the book The Secret Life of Programs by Jonathan E. Steinhart. In it, he mentions in passing:
many consider the handling of whitespace in Ruby to be a replay of of a mistake in the ...
0
votes
2
answers
264
views
Why are arguments of fopen restrict-qualified?
It has caught my attention that both arguments of fopen are both restrict-qualified. It's been that way since C99.
What does this achieve?
If I have:
const char *path = "foobar";
const char *...
0
votes
3
answers
973
views
How to implement state machine side effect that needs data the state machine doesn't have
Given a state machine that implements a certain protocol. I need multiple instances of this protocol running, so I create multiple instances of this state machine. Each instance is associated with a ...
3
votes
2
answers
3k
views
Standard error codes vs custom error codes in C
I am working on improving the code quality and portability of my C library, specifically a ring buffer implementation, that will be used in larger applications. I have encountered a dilemma regarding ...
3
votes
1
answer
444
views
Is C usually a last resort?
I've been learning C recently.
I've completed a number of coding challenges on websites like codewars in C, and I always find myself wishing I had something like Python's flexible data structures. In ...
7
votes
2
answers
4k
views
Why does C not support direct array assignment?
In C you cannot assign arrays directly.
int array[4] = {1, 2, 3, 4};
int array_prime[4] = array; // Error
At first I thought this might because the C facilities were supposed to be implementable with ...
0
votes
2
answers
180
views
Where to put the code that searches objects in a complex hierarchy?
Given the following hierarchy of objects: a keyed collection of ClassA objects, where each ClassA object contains a keyed collection of ClassB objects and each ClassB object contains a keyed ...
0
votes
1
answer
86
views
Avoiding repeating code when assigning pointer addresses based on branch
I have the following code in C that reads data off a char array and makes decisions on what variables should some pointers point to.
char preset1[20];
char preset1Name[20];
int preset1Temp;
int ...
2
votes
1
answer
2k
views
Saving and reading files in user specific application data in C or C++
Most useful or reasonably complex applications need to save data such as user settings or saved games or browser history. I have been working on applications and games in C or C++ but I am not sure ...