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
1
vote
3answers
173 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
41 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
122 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
204 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
87 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
124 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
141 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
466 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
78 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
106 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
472 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
24 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
291 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
34 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
123 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
116 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
303 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
70 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
154 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
46 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
299 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
107 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 ...
3
votes
3answers
490 views

How does a compiler work when it's not directly compiling to machine code

I know the compilation process goes with this flow: source -> parse -> AST -> intermediate code -> assembly -> machine code and in the case of Java you will have bytecode which is ...
0
votes
1answer
118 views

Is it possible to swap out programs displayed in stdout?

I would like to write a program in C that forks then execs three different processes. Afterwards, two of the programs will be suspended and only one of the three will be outputting to stdout. Is is ...
0
votes
2answers
55 views

Architecture/design for reading incoming data and distributing it across program

This may be a broad question but I have not been able to find an answer. In my program I am communicating to a device over serial communication. The data coming through is binary, and formatted with ...
-1
votes
1answer
135 views

static, dynamic analysis - what mistakes were made in the code?

A software company develops software packages for commercial animal farming. A special function in C calculates the daily amount of feed for different kind of animals dependent on their bodyweight....
1
vote
3answers
187 views

Does each process have it's own section of data, text , stack and heap in the memory?

I have created the above picture to illustrate my question. Is there a section within memory (lets say from address 0x1 to 0x15) that all processes use to place their text segment in (left figure), ...
5
votes
2answers
291 views

Why use multi-purpose getters that take an enum value, rather than separate getters?

I'm designing a C API which will have about a dozen getter functions for various values. Something like: bool getSomeBool(); bool getSomeOtherBool(); ... int getSomeNumber(); int getSomeOtherNumber();...
46
votes
3answers
10k views

Why do we still grow the stack backwards?

When compiling C code and looking at assembly, it all has the stack grow backwards like this: _main: pushq %rbp movl $5, -4(%rbp) popq %rbp ret -4(%rbp) - does this mean the ...
0
votes
1answer
89 views

Chain function calls which return error codes or 0 on success in C [closed]

I used to call functions which returns int error code or 0 on success like this: int tmp = function_a() ?: function_b() ?: function_c(); if (tmp) handle_error(); Now I'm ...
0
votes
2answers
192 views

How can I figure out where the code starts for a complex software project? [duplicate]

Say I want to (try to) read through and understand a fairly complex piece of code (for example the free software Coreboot firmware code, which can be found here). How can I figure out where the code ...
0
votes
1answer
222 views

Getting file format by checking file header [closed]

I'm writing a program, part of which consists of determining if a given file is a PNG. Knowing that a file doesn't have to be named with its respective filename extension to be of a certain type, I ...
22
votes
6answers
9k views

Why is C/C++ main argv declared as “char* argv[]” rather than just “char* argv”?

Why is argv declared as "a pointer to pointer to the first index of the array", rather than just being "a pointer to the first index of array" (char* argv)? Why is the notion of "pointer to pointer" ...
2
votes
3answers
232 views

What's faster.. Multiple variables or single array? [duplicate]

I have a C-coded function that realizes a very long calculation on a microcontroller. I try to optimize it for speed at the moment. The function content is created automatically using Mathematica. It ...
2
votes
3answers
438 views

GCC or Clang to output bytecode for a VM?

Long story short, I wanted to use C as a scripting language and in a portable manner so I created a register-based, JITless VM to accomplish this. I've formalized the VM's ISA, script file format, ...
1
vote
3answers
139 views

Is it ok to duplicate a program memory to avoid inline reset?

I have a time-critical function that needs to be executed multiple times sequentially. void task(state_t *state); It uses a relatively large amount of memory, so I figured that allocating it once ...
3
votes
3answers
270 views

C Thread Architecture

I'm trying my hand at multi-thread C programming and decided to start with a simple ncurses program to move a character across the terminal screen and change direction based on user input (think ...
4
votes
1answer
117 views

Should I specify my header include path in the source code, or as a project option?

Basically, I am asking, whether my code should say #include “../libs/src/my_lib.h” or #include “my_lib.h” with a complier option of -I ../libs/src/ I feel (reasonably strongly) that ...
4
votes
7answers
246 views

Is there a best practice for allocationg/deallocating multiple, dynamic arrays in C?

I was wondering what would be the best approch to allocate/deallocate multiple one-dimensional, dynamic arrays in C. This seems easy at first , however, for me it turned otu to be problematic Considre ...
3
votes
2answers
452 views

Include own header file first or last? Any technical reasons?

I was revisiting my question How can I prevent header hell?, when I noticed a comment which said A good technique for ensuring a header is independent is having a rule that the source file always ...
-2
votes
2answers
98 views

How to put interface created with JavaFX on C code? [closed]

I am creating server/client application. The server and client parts should be written in C language. However, for user interface I would like to use javafx. So, the question is it possible to combine ...
-4
votes
2answers
166 views

How the value of the integer variable is stored in a memory by bytes/bits?

In C program I'm doing below stuff int x = 4; Let us assume integer has 2 bytes in this case. So my question here is the variable x will hold two bytes that mean 16 bits. So here how the value 4 ...
-2
votes
1answer
159 views

Possible motivation of having a minimal main function as in CPython source code?

I was recently looking through the CPython source code and noticed something rather intriguing: /* Minimal main program -- everything is loaded from the library */ #include "Python.h" #include "...
0
votes
2answers
146 views

Why does malloc and calloc have different signatures?

malloc and calloc has these signatures: void* malloc( size_t size ); void* calloc( size_t num, size_t size ); They do basically the same thing. Both allocates a chunk of memory of the specified size ...
79
votes
8answers
13k views

Why use trailing newlines instead of leading with printf?

I heard that you should avoid leading newlines when using printf. So that instead of printf("\nHello World!") you should use printf("Hello World!\n") In this particular example above it does not make ...
3
votes
2answers
146 views

malloc on different platforms

I am testing a red-black tree implementation (repository) and I find that with Windows 10 and gcc, malloc starts returning NULL after inserting approx. 50 million nodes but on Linux it works at least ...
1
vote
1answer
340 views

Best practice of c/C++ dependency management on build servers?

I develop largely in java which has maven or ant/ivy. Is there a recommended way to manage build dependencies for c++ projects? I'm setting up the build on our build server right now but the thing ...
0
votes
2answers
306 views

How to unit test device driver initialization?

I am writing an IMU device driver in C using test-driven development methodology. As part of the driver, I pass a struct with user selected configuration settings to an initialization function which ...
-3
votes
2answers
208 views

C/C++ code optimization without compilation

Given a simple loop in C or C++ such as the following: for (int i=0, j=0; i<5; i=i*2){ j=j+i*1; } I would like to use a tool (gcc, clang or any other) to optimize the code without actually ...
-1
votes
1answer
84 views

Makefile: Dev and Release

In Git, I have a master branch (release) and a dev branch (development) for a C project. In the master branch, I want gcc to only be passed the compiler flag -Os (optimize for speed) and not -ftest-...