Questions tagged [c]
C is a general-purpose computer programming language used for operating systems, games and other high performance work.
1,324 questions
2
votes
1
answer
133
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
490
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
196
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
272
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
309
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 ...
6
votes
2
answers
594
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 ...
2
votes
1
answer
147
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
211
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
1k
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
368
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
160
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
261
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
181
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
230
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
6k
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
761
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
58
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
9k
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
12k
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
845
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
218
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
237
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 ...
22
votes
5
answers
20k
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
172
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
246
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
311
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
122
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
691
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
221
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
89
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
331
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
246
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
696
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
2k
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
417
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 ...
5
votes
2
answers
3k
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
156
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
79
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
1k
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 ...
-1
votes
2
answers
453
views
what algorithm does the malloc function in the c language standard library use? And why it so fast?
Based on the data structure of the AVL tree, I implemented a memory manager that does the best matching according to the size. I originally thought that the speed would be fast and the length of the ...
0
votes
1
answer
229
views
Is it true that variable type before name makes compiler work easier? [closed]
I have seen information that at least one of reasons why type placed before variable name is that it allows compiler easier evaluate size and type of variable. If so then how (what way) it eases this ...
-3
votes
1
answer
207
views
Will anything that's written in c/c++ be rerwritten in rust? [closed]
Will anything that's written in c/c++ be rerwritten in rust?
for maintainance, even for performance?
0
votes
1
answer
110
views
Is it safe to make training data and labels as global variables in C?
I'm trying to make this function called walk in C. The idea is that the function takes as parameters a point in space (represented by an array), a function (pointer), and a step size / learning rate ...
0
votes
2
answers
160
views
Where to check preconditions in multi functions
In a library, there could exist three types of functions. The first are those which are visible to the user i.e. their declarations are installed in the library's include directory. The third are ...
0
votes
1
answer
691
views
Custom #pragma directives
I'm creating a language parser on a microprocessor in C++. For the tables of keywords and commands, rather than maintaining a single curated file (alphabetically sorted, etc), I'd prefer to declare ...
-1
votes
1
answer
214
views
Better way to represent grammar symbols in C
I'm trying to build a simple compiler for a subset of the C language in C. To achieve this, I needed to figure out a way to represent the grammar symbols. Basically, each symbol can either be a "...
-3
votes
1
answer
244
views
When has C or C++ made itself intentionally backwards incompatible? [closed]
Context ( You can skip )
I will be doing a presentation on Python, and will be touching upon V3's decision to be intentionally backwards incompatible, and how that has affected Python long term. I ...