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
0 votes
1 answer
80 views

Using a preprocessor macro to wrap functions?

I have multiple use cases where I need to call an "entry" function, do a bunch of stuff, and then call an "exit" function. (In my case, "entry" is enabling a chip select ...
user avatar
6 votes
4 answers
518 views

What was the original purpose of C strncpy() function?

C standard library has strncpy function, declared as: char *strncpy(char *dest, const char *src, size_t n); It's a strange beast, as it fills n bytes of memory pointed to by dest. It does this by ...
user avatar
  • 3,694
0 votes
2 answers
72 views

How to tell client if predicate function fails?

Just say I have list APIs, and provide find() to search node. With this design if something went wrong during pre-conditions, client would have no idea about it because no status code provided. bool ...
user avatar
  • 185
-2 votes
1 answer
63 views

C (Arduino): treat a #defined hex value as ASCII chars [closed]

This is Arduino code, but since it uses C syntax, and it's not Arduino-specific, I thought I'd ask here. I have a few values defined so: #define PID_RPM 0x0C Typically it's used as a hex number. But ...
user avatar
-1 votes
1 answer
102 views

Writing a API for a hardware device for mutliple platforms

We want to create an API to communicate witha device we currently sell. The API should be available for several platforms like C / C++ / .NET / Python and available for Windows and Linux. The idea is ...
user avatar
  • 99
0 votes
1 answer
143 views

In C and C++, can a compiler store an unamed object generated from a expression, if that same expression is reused, but the result is unchanged? [closed]

I'm mostly interested in C and C++, but I think this question can also apply to other languages. My question is, if an unamed object or value is generated from the evaluation of an expression or ...
user avatar
16 votes
5 answers
6k views

Why are variables declared without a value in C?

The way I see used most frequently int main() { int i; for (i=0; i<10; i++) { printf("hello\n"); } return 0; } The way I’m used to int main() { ...
user avatar
  • 190
0 votes
1 answer
211 views

How can I unit test state machines in C

In an 8bit mcu, I wrote a piece of code of a state machine using a function pointer static void (*state)(void);. Inside the same compilation (let's say sm.c) unit I implement all the available states. ...
user avatar
  • 203
2 votes
2 answers
126 views

Design pattern to create a shared lib architecture

I am making a renderer as a hobby, one thing I thought to try is making the low levelAPI be dynamically swappable, i.e. you could have an opengl or vulkan backend and switch between the two without ...
user avatar
  • 241
2 votes
1 answer
129 views

Wrapping a legacy C++ codebase with a C API

I have inherited a large C++ codebase implementing various Windows desktop applications, services and libraries using Windows MFC. There are no automated tests. We need to decouple the UI and retain a ...
user avatar
  • 91
2 votes
2 answers
160 views

Strategy for offering a library both a single-header and as a header + compiled implementation

I am maintaining a FOSS library which, for the sake of discussion, consists of a small .h file and a larger .c file (plus build-related files, see below) I've been requested to offer this library as a ...
user avatar
  • 2,390
-4 votes
1 answer
59 views

Is there a way to use a third party Python or C API through C#? [closed]

I will receive a hardware which will have native Python API to communicate through USB. The device will continuously send image data. The vendor only supports Python and C APIs to communicate with the ...
user avatar
  • 129
-2 votes
2 answers
166 views

Is there any other reason to Null-terminate a string besides to find out the length of the string? [closed]

I have looked at the source code of some standard C functions and wondered what the reason is for null-terminating a string other than to find out its length. e.g. The strlcat() function which is ...
user avatar
  • 113
24 votes
8 answers
6k views

Should I assume data passed to my function is accurate?

Assuming I have the following struct (just an example) struct string{ int len; char*str; } And I have the function int init_str(struct string*s, int len); which will perform s->len=len; s-...
user avatar
  • 368
0 votes
1 answer
117 views

Implement Dependency Inversion in C with UML diagram

As per Robert C. Martin in Clean Architecture, he gives a simple UML diagram to illustrate Dependency Inversion. To put it simply, HL1 initially referred to ML1 without interface to invoke F() ...
user avatar
  • 185
0 votes
5 answers
80 views

best practice for error handling between PAM_SUCCESS and PAM_AUTH_ERR macros

We have two exit code code PAM_SUCCESS(0), PAM_AUTH_ERR(7). If we wanna return 0 or 7 when we compared password with our password in following code which code is better that this code ? if (strcmp (...
user avatar
2 votes
1 answer
128 views

How could Vulkan pNext design be implemented in a "safer" way?

Vulkan introduces a member .pNext of type void* in all its core structs allowing to create handles. This member purpose is to allow to extend the structure by passing a pointer another one. This is ...
user avatar
  • 129
2 votes
3 answers
180 views

What methods are available to implement feature flags or "loadouts" for C++ projects?

I'm looking for a system to be able to configure my C++ project to compile some sections of codes and not others depending on what target hardware I'm compiling for. The area of development is ...
user avatar
  • 31
2 votes
1 answer
250 views

How to properly delete nodes in a linked list?

I'm doing this homework exercise with simple linked lists in C with nodes having the following structure: typedef struct lista { int num; struct lista * next; } nodo; So the exercise asks us ...
user avatar
  • 33
-1 votes
1 answer
432 views

How can I initialize an empty binary search tree in C?

I am trying to implement a Binary Search Tree in C as a learning exercise. I have the following struct that represents a node. typedef struct BSTNode { int data; struct BSTNode *parent; ...
user avatar
  • 125
0 votes
0 answers
82 views

Designing a logic to receive ACK for every message sent in a non-blocking way

This is more of a design/architecture question: I have got a timer based scheduler which informs the client of what and when to run next on its end. For which, I created a priority queue based timer ...
user avatar
  • 9
1 vote
3 answers
278 views

Debug statements in production quality code?

Would anyone here recommend using debug statements such as the following in production quality code? I think these are personally one of the easiest to include or exclude, but they make the code hard ...
user avatar
0 votes
1 answer
108 views

Help architect a timer-based scheduler

I am writing a timer-based scheduler to accomplish the task of informing a certain process of what to do and when. The Idea: Inform an X process of certain requests to perform at timed-intervals. So ...
user avatar
  • 9
0 votes
1 answer
221 views

Opaque objects on the stack / in structs

I have a collection of opaque objects and related functions API. Such objects are typically created on the heap, but that involves a non-trivial amount of overhead. And when aggregate together, that ...
user avatar
  • 623
-6 votes
3 answers
688 views

Could H correctly decide that P never halts?

Because a halt decider must compute the mapping from its inputs to an accept or reject state on the basis of the actual behavior of its actual inputs, when H is a simulating halt decider H(P,P) would ...
user avatar
  • 81
5 votes
2 answers
621 views

Is there a standard error handling design pattern for C?

I'm working on a large C application that I've divided into sub-libraries (e.g. networking, message handling, message building, etc.) I've converged onto a pattern where functions are only allowed to ...
user avatar
  • 155
0 votes
2 answers
143 views

Low level C driver API

Recently I had my first experience writing a wrapper around FTD2XX driver for a desktop tool that communicates with an embedded device. The library provided some common device specific functionality ...
user avatar
0 votes
2 answers
127 views

Data structure for graphs and vectors in C

I'd like to design an implementation for graphs in C. I'm wondering what the most efficient approach in terms of both computational power and memory consumption would be. I've looked at various open-...
user avatar
2 votes
2 answers
450 views

C99: Alternative to "static" variable declaration?

By coincidence I stumbled over "The New C Standard" from Derek M. Jones, where he comments this sentence from the C99 standard regarding "storage duration of objects": There are ...
user avatar
  • 137
0 votes
2 answers
524 views

How can Python, a language that supports Object Oriented Programming, be implemented in C, which is not an Object Oriented Programming language?

To my understanding, C does not have the concept of objects, then how can Python be implemented in C to support something that C can not? How is the concept of "Object" modeled in C? What is ...
user avatar
  • 39
2 votes
1 answer
128 views

How should a bytecode VM call external C functions?

I am trying to implement a basic bytecode VM, which I plan to target with a compiler. How can I implement the ability to call external C functions using the bytecode, i.e., call arbitrary functions in ...
user avatar
1 vote
2 answers
799 views

C Const Safety with Struct Data Pointers

In our codebase, having precise management of memory, data was being passed around like so: typedef struct { size_t const sz_size; size_t sz_len; int32_t * const restrict pData; } S_ARR_T; ...
user avatar
  • 179
0 votes
3 answers
81 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 ...
user avatar
  • 95
0 votes
3 answers
99 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 ...
user avatar
  • 115
0 votes
1 answer
113 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; ...
user avatar
  • 185
2 votes
4 answers
254 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 ...
user avatar
-1 votes
1 answer
173 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 ...
user avatar
1 vote
3 answers
213 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 ...
user avatar
  • 49
2 votes
1 answer
125 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 ...
user avatar
  • 121
3 votes
4 answers
280 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 ...
user avatar
0 votes
1 answer
79 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 ...
user avatar
  • 11
-1 votes
3 answers
272 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 ...
user avatar
-2 votes
1 answer
126 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){ ...
user avatar
1 vote
1 answer
331 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 ...
user avatar
-4 votes
1 answer
149 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 ...
user avatar
  • 113
-1 votes
2 answers
143 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 ...
user avatar
  • 203
0 votes
0 answers
55 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 ...
user avatar
  • 95
-2 votes
1 answer
99 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 ...
user avatar
  • 441
-5 votes
1 answer
431 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(); } } ...
user avatar
3 votes
4 answers
299 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 ...
user avatar
  • 173

1
2 3 4 5
26