Questions tagged [c]

C is a general-purpose computer programming language used for operating systems, games and other high performance work.

Filter by
Sorted by
Tagged with
-3
votes
3answers
176 views

efficiency difference of & vs == in if [closed]

I was reading some code and stumpbled over a weird if assignment: #define compVal 1 uint someVal; ... someVal = 1; //or 0, large 1 not allowed! ... if (someVal & compVal) ... so the code should ...
0
votes
3answers
63 views

Designing this module to support exchangeable drivers

We have a battery manager implementation which uses a fuel gauge driver and a charger driver. Currently both of these drivers are hard-references to the fuel gauge and charger driver we currently ...
0
votes
2answers
50 views

Is it better (or preferred) to initialize allocate and free structures used by a thread, “inside the thread”

Let's say we are building a threaded program in C, for example a message queue system where a thread is spawned to manage one end of the queue, and (re)connect sockets. This can roughly be set up in ...
0
votes
1answer
94 views

What design pattern (if so) did I apply? How can I further improve it?

Suppose I have a program.c that needs element_123 to do some operations, and element_123 can be accessed by including agent.h /*program.c*/ #include "agent.h" uint32_t element_123 = 0; ...
2
votes
4answers
140 views

Naming convention for functions that mutate arguments vs creating a new object

Take the following signature BigInt* addBigInt(BigInt* arg1, BigInt* arg2); Traditionally, the safest way to implement this function would be for it not mutate the arguments, and would necessarily ...
-1
votes
1answer
147 views

Should all structures be ADTs?

Is there any specific category an ADT must be in? I'm struggling to find out how best to represent objects in the program for example: As a record: struct Person { int age }; Or as an ADT: struct ...
1
vote
3answers
196 views

Unifying and modularizing the while / do feature set in c

disclaimer: I'm a university student who's one-year-new to programming. Please don't slaughter me in your responses as I am still a human being I have an idea that I want some feedback on. I am ...
1
vote
1answer
47 views

Methods for remote display of and embedded FreeRTOS devices display

I have a device that has a NXP MK66FN1M arm core processor with a black and white display. This device is running FreeRTOS with serial, usb, keypad, display, external FeRAM, wifi, and ethernet. What ...
3
votes
4answers
209 views

Should functions depend on other functions?

Suppose I have a function to log error messages printError(). Suppose I have another function which might throw an error, doSomething(). Should doSomething() implement its own error logging or depend ...
0
votes
1answer
75 views

Custom datatype and their constructors - should you clone, or just copy?

Suppose we have the following C snippet, implementing a tree data structure: typedef struct node { char* data; struct node** children; unsigned int num_children; } node; Then we could ...
0
votes
3answers
119 views

Is it possible to solve this synchronization problem using mutex and conditional variables?

I am trying to solve this synchronization problem in C to practice for my lectures of Operating Systems where we use POSIX and Linux. I've been trying for days to find an approach to this problem with ...
0
votes
0answers
35 views

Sharing values among enums

I'm designing a state machine framework. (Yes, I know they already exist, but bear with me...) My "standard state machine" always defines four states: typedef enum { STD_SM_STATE_INIT, ...
-2
votes
1answer
75 views

How to manually delete a std::vector in c++? [closed]

I have following code snippet void func(int row,int n,std::vector<int>& buffer){ if(row>n){ return; } std::vector<int> new_buffer; for(int elm : buffer){ ...
1
vote
1answer
79 views

How to separate public and private headers when internal data structures are private?

When developing a shared library in C, it is common to separate the "public" headers from the "private" headers. The public headers contain all the functions and types that are ...
-3
votes
1answer
130 views

Understanding C after getting used to scripting languages

I use python and javascript in my daily workflow. I am really excited about understanding c as I am drawn towards contributing to projects that are written in the language but even after weeks of ...
0
votes
2answers
128 views

How do I break down my components and files in C

I've been designing an event processor for state machines in C and I'm trying to figure out what's the best way to break down my components in simpler and smaller *.h and *.c files. Specifically I'm ...
0
votes
0answers
48 views

How to best design this communication module/library?

Introduction A customer of ours has embedded products with sensors and actuators. Now they would like to connect this device to the cloud so they can remotely monitor and configure it. It should ...
-2
votes
1answer
88 views

What is the time complexity of this algorithm

This code is from https://www.geeksforgeeks.org/segregate-even-and-odd-numbers/ It takes an array of numbers & segregates them in place without bothering about the order. void segregateEvenOdd(int ...
-4
votes
1answer
145 views

How do I manage multiple nested for-loops without using multiple variables?

If I have code that looks like this: int i; void functionA (){ for (i=0; i<10; i++){ functionB(); } } void functionB (){ for (i=0; i<20; i++){ doSomething(); } } ...
3
votes
4answers
286 views

Elegant way to check return of getXY() for multiple values in conditional check

When I want to execute some code under the condition that a variable has one of two (or more) values I can use the OR operator: if (var == x || var == y) { DoSomething(); } But I'm not sure ...
-2
votes
1answer
60 views

What should I use to graphically represent an object's position on a fixed path using python(preferred)?

Consider 2 roads merging into one(This map is supposed to remain fixed). The program will not generate this map. The program will read this map and plot positions on it on its own. The python program ...
-1
votes
2answers
112 views

What design/component principle did I apply? How can I further improve it?

I recently refactored a program for code feasibility and maintainability; however, I am totally unaware of what software principle I did implement. I just followed my feeling. The purpose for this ...
2
votes
5answers
785 views

Do all threads share the same instance of a heap variable, or have different instances of a heap variable? [closed]

Computer Systems: a Programmer's Perspective says: 12.4.2 Mapping Variables to Memory Variables in threaded C programs are mapped to virtual memory according to their storage classes: Global ...
3
votes
3answers
134 views

Use of globals in stack-based virtual machine implementation

I'm implementing a stack-based virtual machine in C. The following variables are used by pretty much every function: memory array various pointers to memory offsets program counter stack stack ...
0
votes
2answers
154 views

Cases of memory management where it is reasonable to separate program into several execs

I have 5000+ strings pet-project for CLI and it can do some optional calculations and can output results to CLI or/and to file. Some new modules don't work. Now I catch with GDB some strange segfaults ...
-2
votes
1answer
66 views

How should functions that keep state across multiple invocations be made safe?

In Computer Systems: a Programmer's Perspective, 12.7.1 Thread Safety When we program with threads, we must be careful to write functions that have a property called thread safety. A function is said ...
-4
votes
4answers
459 views

Since `strcpy`, `strcat`, and `sprintf` are dangerous, what shall we use in stead of them?

In Computer Systems: a Programmer's Perspective, Unfortunately, a number of commonly used library functions, including strcpy, strcat, and sprintf, have the property that they can generate a byte ...
0
votes
2answers
149 views

What is a “function returning type” in C?

C11 standard says 6.3.2.1 Lvalues, arrays, and function designators A function designator is an expression that has function type. Except when it is the operand of the sizeof operator, 65)...
2
votes
1answer
102 views

Ask for suggestion: data type for parsing stringified fractional numbers

I am the author of a C library for parsing INI files. So far I have delegated the task of parsing values as numbers to the standard atoi() family of functions. However I think time has come that I ...
2
votes
2answers
142 views

Are bitwise operators in C used both for bit operations and for operations for integer or some C types?

In C, Bitwise logical operators &, |, ^ is used for selecting bits in a word. Bitwise shifting operators >> and << can be used for implementing multiplication and division between ...
-2
votes
1answer
50 views

Does this alignment of structure objects apply only to structure objects being used as elements of an array?

In Computer Systems: a Programmer's Perspective, In addition, the compiler may need to add padding to the end of the structure so that each element in an array of structures will satisfy its ...
-4
votes
2answers
99 views

IO stream interface in C

When writing a library (eg. for dealing with JPEG or PNG files), the first thing library author needs to provide is an abstraction over FILE* (to allow also reading from plain memory, pipe, socket, ...
0
votes
2answers
492 views

Why are some languages called platform dependent if I can always share the source code?

I was reading about erlang when I read that it is platform-independent, using BEAM as the VM, now I understand that a VM compiles the byte code to machine code and this makes that language machine-...
-3
votes
1answer
153 views

Data structures or coding styles in C++ for avoiding long elseif chain when parsing?

Lately I have created some small parsers of data. My initial code structure // more cases here ... else if(!strcmp(X,"somekey")){ // Parse according to "somekey" behavior. } // ...
-4
votes
1answer
164 views

Do these two design philosophy conflict with each other?

With few years of C programming, I noticed there are two major types of design choices for data organization. I offer an analogy below. First: height folder: Andy.txt, Bruce.txt, Chris.txt and Tina....
1
vote
1answer
116 views

What is the name of this kind of (intra-process, inter-thread, queue-based) event system?

Context Espressif's embedded development libraries for the ESP32 microcontroller contain an event system based on FreeRTOS. The event system handles connectivity, interrupts and other IO activity, but ...
6
votes
6answers
552 views

How is design using C different from C++?

A employer is looking for C programmers, and I'm told they say that ... Good C design isn't the same as good C++ design ... and so they're looking for candidates experienced with C and not only C++. ...
-2
votes
1answer
56 views

Adding samples from wav files without causing integer overflow

Recently, I've been writing a wav writer in C. For this question of mine, Let's consider two wav files that we'll be combining the samples; both are s16le mono wav files to be simple. The first idea ...
0
votes
3answers
154 views

What goes into a computer deciding how many memory locations to assign for specific data types in C?

I have learned file memory management and some very simple CPU assembly for manual memory manipulation. Still, I feel like there is a gap in my knowledge when it comes to modern, complex computers, ...
2
votes
1answer
95 views

What is the design of libraries like cairo and OpenGL called?

I'm not a software engineer, so sorry if the question is elementary. Usually, functions provided by libraries written in C receive a handle from the calling program. This handle directly (if it's a ...
1
vote
1answer
148 views

How to check data has been received correctly with CRC - much confusion here

I am trying to process some serial data (sent over UART). Its coming from a radio controlled receiver, is 27 bytes long. The last 2 bytes of this are a 16bit CRC check according to the spec. The ...
7
votes
5answers
975 views

Why are people coding “C-style C++”?

In discussions about whether to cast the result of malloc or not when coding C, one common argument is that if you cast the result then you can compile your C code with a C++ compiler. Why would one ...
-5
votes
2answers
102 views

Current industry standard with regards to C macros [closed]

I have read from several authors that macros in C should be avoided whenever possible, and use inline functions instead. It's true that inline only 'requests' the compiler to replace the function call ...
-4
votes
2answers
354 views

Malloc vs variable length array?

In general, should I use variable length arrays or malloc? In my specific case I am processing files of very short to very large sizes. Also if I compile on version MacOS 10.15, and then run on 10.13, ...
-4
votes
1answer
73 views

Using only the Cache not the DRAM in a Program (USB to Ethernet) [closed]

I am reading some data from USB, do some packet processing and then send the result to the Ethernet port. The USB ports are connected to an onboard USB Hub where it communicates with the ...
-4
votes
4answers
543 views

Why are magic numbers bad practice?

Why are magic NUMBERS considered bad practice? For example: memcpy(ResourcesDir+GameDirLen, "/Resources", 11); What is wrong with the 11? Why are magical numbers so bad? Please, my question ...
11
votes
4answers
957 views

Why would you ever use `malloc(0)`?

While reading an answer here, I saw this code: char ** v = malloc(0); while ((r = strtok(s, " ")) != NULL) { char ** vv = realloc(v, (n+1)*sizeof(*vv)); The thing that bugged me was the ...
2
votes
5answers
2k views

Is using 64 bit integers (long long) faster than less bits ones?

I read in the book "Game Engine Architecture" by Jason Gregory that: "It’s possible to access data items that are narrower than the width of a machine’s data bus, but it’s typically more costly than ...
-2
votes
1answer
80 views

Are jagged arrays lvalue or not?

When I was study lvalue i see that C expression can be lvalue if a subscript ([]) expression that does not evaluate to an array. (from https://docs.microsoft.com/en-us/cpp/c-language/l-value-and-...
2
votes
1answer
499 views

How do I implement Dependency Inversion in C?

I'm a firmware developer and I'm interested in applying the SOLID practice in low level programming especially in Hardware Abstraction Layers in ARM microcontrollers. Every example I come across on ...

1
2 3 4 5
25