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
22
votes
8answers
5k views

Detect manual changes to an autogenerated C header [closed]

I have a C header that is generated from a CSV file and a python script. The C header mainly contains a list of #define constants. I want to be able to detect manual changes to this header during ...
-2
votes
1answer
91 views

Why would you make a struct without typedef?

Since I cut my teeth on code with OO, I’m biased toward using structs as classes without methods. However, there’s probably a good reason that typedef isn’t the default behavior of struct. What is it?
2
votes
2answers
220 views

Loop complexity in C Vs Python

Just now I was reading a Python question on stack overflow, about clamping a list/array of results to be within a certain range. Once of the more simple answers suggested something like: ...
-3
votes
1answer
118 views

Is it possible to isolate C function from changes to its parameter type? [closed]

I would like to know if it is possible to code any function f that takes a structure S as a parameter while, in case we want to make drastic change to struct S, we can do it without touching a single ...
4
votes
5answers
660 views

How to deal with a lot of conditions in If statement in an elegant manner

Is there a more syntactically beautiful/simply better way to write the following (without major abstraction)?: if (usart_error.CRCError == true || usart_error.DMATransferError == true || ...
-1
votes
0answers
40 views

What should the parser do with the output of the lexer?

Currently I am working on my own compiled language. I have finished the lexer and its output is a token list, the parser should go through this list and build the trees. Example file for the language:...
0
votes
1answer
75 views

Better stdbool.h

How about instead of #define bool _Bool #define true 1 #define false 0 #define __bool_true_false_are_defined 1 We should have this: #define bool _Bool #define true (bool)1 #define false (bool)0 #...
1
vote
1answer
41 views

What are the limitations of WinSock2 sockets within threads?

So I've run into some strange behavior on an application i'm developing in C using the windows API. Im trying to implement a closed connection server-client interface. However, for whatever reason ...
0
votes
2answers
370 views

Logically, is there a reason why ++i++ can not be a valid expression?

I had to increment my integer twice in a loop, so I thought I would try and be clever: for (int i = 0; !sl.isEmpty(); ++i++) { ... } ++i++ however is not an assignable expression, at least in GCC. ...
0
votes
1answer
109 views

Architectural pattern for testable C code [closed]

I wrote a lot of software in C# and Python. I tried to make the overall architecture testable by using the "Clean Architecture" and Dependency Injection. This works well for C# (and python). Now I ...
2
votes
3answers
111 views

Declaring Multiple Variables of the Same Type within a Function's Parameters

We've started looking at functions, and our homework requires us to include some simple ones in our programming. As we've left it a little late in the semester to explore functions, we're well-used to ...
0
votes
0answers
79 views

Searching/Hashing Wikipedia - at each position for keys 1..31 long, the fastest way?

A C coder here. My wish is to find the best way to search words and phrases within the ever-growing English Wikipedia XML dump. As a first layer/step, hashing is the answer, yes? The problem is that ...
3
votes
4answers
358 views

Alternative to nested-if on single return functions

I'd like to see if there has been any precedent on alternatives to nested-ifs--particularly for error-code returns. My workplace requires one return per function, so I cannot early exit. Here is some ...
4
votes
3answers
255 views

How to adhere to the Open Closed principle in a procedural language like C

In Robert Martin's seminal 1996 article "The Open-Closed Principle" he presents an example in C which does not follow the principle (the DrawAllShapes() method is not closed for modification): enum ...
3
votes
1answer
73 views

Is a generic data structure customary for use in a self contained program, or can I duplicate source files?

I have a project that does some Dijkstra searches on a graph, I implemented a red-black tree and priority queue that I use for the nodes, however I also need the same data structures for other things ...
25
votes
7answers
6k views

Can abstractions and good code practice in embedded C++ eliminate the need for the debugger?

I am a C developer for an embedded system. YouTube has recently started recommending "C++ for embedded systems" talks. Having watched some of them, they pique my interest, but none of them answer the ...
3
votes
1answer
248 views

Avoiding Arrow Anti-Pattern in MISRA-C

For a safety-critical domain, with a code standard including the MISRA C 2004 guidelines, a "simple" piece of code becomes obtuse, chiefly because of the requirements both that all if() statements ...
10
votes
5answers
870 views

why aren't the platform specific integer types in C and C++ (short, int, long) deprecated?

TL;DR: Why isn't everybody screaming, "Don't use short, int, and long unless you really need to, and you very likely don't need to!" I understand that, in theory, by using the types short, int, and ...
1
vote
1answer
124 views

Finding undefined behaviour in C for embedded system

I just started at a small company and there is a hot-needle-built software for micro controllers written in C which we are now starting to clean up to make it maintainable again. Of course this goes ...
3
votes
1answer
109 views

Keep track of intializations done in a function

There is an initialization function in my app that does multiple inits. What is a good way to keep a track of inits done so that if init n fails all inits done before it must be cleaned up/de-init ...
0
votes
0answers
65 views

C++ client-server design

The context I need to design a Client-Server(-Client) network in C++ that would allow an android application to communicate to a Raspberry Pi and back I am running everything on linux environment(s) ...
3
votes
4answers
484 views

How to use Macros in Programming to make code faster, efficient and compact

Recently I was going through some of the solutions of the best competitive programmers in the world. I found out that those people use a template while writing the programs, preferably in C++. I ...
42
votes
3answers
14k views

Why is the logical NOT operator in C-style languages “!” and not “~~”?

For binary operators we have both bitwise and logical operators: & bitwise AND | bitwise OR && logical AND || logical OR NOT (a unary operator) behaves differently though. There is ~ ...
1
vote
1answer
106 views

How to eliminate transparent memory copy in runtime?

Despite various ways to scrub sensitive data in volatile memory (see Survive DSE or Zeroing buffers), programs tend to perform transparent memory copies (such as a Garbage Collection). The newly ...
3
votes
1answer
185 views

Managing error code and error message mapping

As part of a game engine I am writing in cython, one of the challenges I am facing is creating a consistent interface for error handling that works at the C and python levels of my code. I am ...
-4
votes
1answer
126 views

Is it really good idea to disable warning of C or Cpp? [duplicate]

When we are working on a big project of C or CPP, we are getting some warning. Therefore, we try to remove that warning because when we see compiler warnings that they make us feel dirty. When we ...
0
votes
2answers
79 views

Common header file for C++ and JavaScipt, redux

I've got a question closely related to this one. I'm cleaning up a colleague's mess, and I'm afraid that that means trying to work with a language -- JavaScript -- that I'm almost completely ignorant ...
2
votes
2answers
957 views

Can Rust replace the C or C++ programs in the Future? [closed]

I recently read an interesting Article that, due to a memory Overflow error Power system of Boeing will collapse and turns the Flight into a Flying Brick. This is a low level memory Handling bug. ...
1
vote
3answers
395 views

Does the gcc optimize out local arrays the same way it would optimize out a local variable?

If I write this code in a function: void func(void) { int x = 0; int y = 3724832+x; } It would (probably) optimize the variable y out because it isn't being used. Even if I'm wrong about ...
0
votes
1answer
56 views

Thread-safe C library with OpenMP

I am writing a C library with routines I want to call from an external driver program using many OpenMP threads. The library will have a main container called lib_workspace, which will be used to do ...
-1
votes
2answers
182 views

malloc in caller, free in callee

Is it good practice to malloc in the caller function, and then free in the callee function? Example: int main() { int *ptr = malloc(100); foo(ptr); return 0; } void foo(int *ptr) { ...
2
votes
3answers
263 views

Practical way to indicate an error from a function that returns a stuct instance?

Suppose I have a function that returns an instance of a struct with the following declaration: struct data API(void); Suppose also that there is a chance for the function to fail with an error ...
0
votes
1answer
104 views

C Function returning pointer to first element vs. a pointer to an entire array

Suppose I have an API function which returns a pointer to some memory, that is later to be consumed in an array-like manner. Suppose also that its size is known in advance, and that it's declared like ...
1
vote
1answer
136 views

Switching out implementation source files in C project

For simplicity, let's say I have the following C project structure: src/ utils/ logger.c logger.h main.c secondary_component.c main.c starts with: #include "utils/logger.h" I would ...
0
votes
1answer
462 views

Should “NaN” default to SNaN or QNaN

I created a function to read floats (IEEE 754 binary 32-bit) from strings, for developers. I have everything finished and working perfectly, except i'm wondering if a user/or programmer enters a ...
0
votes
2answers
710 views

How to prevent 'global variables' in a big project?

With 'global variables', I mean Variables on namespace level Static data members in classes Static variables in functions In a big C++ project I would like to have a mechanism (like a compiler ...
0
votes
1answer
85 views

Sharing #define's between higher and lower layer. Where should shared types go? [closed]

Let's say I have a stacked 2 layer app (High Layer (HL) and Low Layer (LL)) that is implemented in C. HL defines a few #defines. HL calls a LL function with a parameter that takes values of the #...
1
vote
2answers
116 views

Reading problem of object-oriented concept about the book “Object-oriented software construction 2/e”

I've been learning object-oriented concept from this book, and stuck in a specified concept in the middle of chapter 6. Because with no brief explaining beforehand, no one could understand my ...
2
votes
5answers
838 views

Advantages/Disadvantages of Compiling as Both C and C++?

So I've seen some C libraries written so that they compile without any changes with a C++ compiler, for example Lua. What are some advantages and disadvantages of doing this? A few basic ones I see ...
1
vote
0answers
29 views

MPI Derived Types and Portability

When using derived types in MPI for communication of data which is not contiguous in memory, the sequence of datatype-displacement pairs defining the derived type seems to be assumed to be the same ...
2
votes
2answers
307 views

A way not to forget to check for errors in C

As everybody knows, C allows us to write anything we want. There comes one big problem — we are the only who responsible to handle errors that comes from standard library functions and, therefore, ...
0
votes
0answers
55 views

How to analyse the throughput of multithreaded client server programs?

I am practicing socket programming in C language. I have two codes - server and client (based on TCP) on two different laptops. The server forks a new process for every new request. For simulating ...
1
vote
1answer
141 views

How float data type stores number greater than 2^23? As Mantissa is only 23 bits

Can someone explain how floating point numbers are stored in C, as for 32-bit float, 1 bit is parity, 23 bits for mantissa and 8 bits for exponent. So for numbers greater than 2^23(still in the float ...
2
votes
1answer
133 views

How are primitives stored in memory

In my searching although this question seems to get answered a lot, the answers don't really cover what I want to ask. I'm learning assembly and my question is, in higher level languages e.g C data ...
3
votes
2answers
1k views

What is the difference between a static library and an archive library?

In the comments of a recent answer, I equated a static library with an archive of compiled object files. The response was that they are not the same, so what is the difference? To clarify - gcc ...
0
votes
1answer
80 views

Types of errors that the primitive operators in C might be involved in

I am new to C and wondering how to properly build primitive functions in order to handle errors. Take for example the following function: bool testIfEqual(int a, int b) { return a == b; } ...
1
vote
1answer
165 views

Will Windows handle any memory not freed as well as threads not closed? [closed]

If I had an application that pretty much had a "fail-safe" as in, if I need to terminate the application quickly and not free the malloc'd memory, or close the threads that are running, would Windows ...
2
votes
1answer
50 views

Architecture design for Linux administrative interface

I've developed an IoT device utilising OpenWrt so my design needs run a on a shoe-string budget of resources. I need to be able to push small amounts of data, up to 300bytes to my app on a very ...
2
votes
1answer
735 views

Why did Java and C sharply dip in popularity around 2017 in the TIOBE index? [closed]

The TIOBE Programming Community Index shows Java and C dramatically falling in popularity between late 2016 and mid 2017 before rebounding by mid 2018. What is the cause of the volatility? Source: ...
-2
votes
2answers
110 views

Should I call a callback for an event triggered by a developer?

I'm designing an API for a stream-processing library. The library will process a stream provided by the user and will transition between states based on the data in the stream. The state transitions ...

1 2 3 4 5 24