Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [c]

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

2
votes
3answers
149 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 ...
-4
votes
1answer
59 views

Why is wchar_t in C++ implemented as a new data type? [on hold]

I am learning C/C++ and have come across the type wchar_t, which appears to be 2 byte long char. So my question is why did C++ add a new type, when it could have just added the ability to apply the ...
0
votes
1answer
108 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
51 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
123 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
113 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
270 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
79 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
188 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
77 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
7k 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
210 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
286 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
136 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
251 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
113 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
237 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 ...
2
votes
2answers
198 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
90 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
120 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
151 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
120 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 ...
2
votes
2answers
129 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
156 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
140 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
179 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
63 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-...
0
votes
1answer
66 views

How to safely walk a list to remove an element as well as associated elements safely

I have a single linked list of elements. Each element has a unique ID (positive integer) and a type. Each element may have a "sister" element, associated by its ID, that may also be in the list. The ...
2
votes
0answers
72 views

Dynamically load user code from a common library

My project aims to provide a common interface between multiple different HDL simulators, so users can write test benches that will work no matter the simulator used. In this situation, it is the ...
0
votes
2answers
133 views

Understanding Context Free Grammar using a simple C code

I'm trying to understand the difference between terminal and non-terminal values in a real language. I wasn't able to find enough examples on real language CFGs on the internet, most examples are ...
0
votes
1answer
52 views

Subdomain matching

I am working on a small plugin for a DNS server. I have a static list of domain (sometimes subdomains too) names: gaming.xyz.com facebook.com mail.example.com blog.example.com I want to check if a ...
-1
votes
1answer
807 views

Folder structure for a C project [closed]

I am wondering what is the recommended folder structure for a C project. I read several posts about using src, include, test, build folders. But what if I want to structure the project in modules? ...
3
votes
2answers
139 views

Best way to model UTC time and deltas in embedded C

I'm getting ready to do some work on an embedded firmware prototype that has a GPS chip attached to it (straight C on an arm0, no OS). I'm torn on how to model UTC instants and time deltas. ...
-6
votes
1answer
180 views

Why does the C compiler use memory for simple arithmetic operations

Assume the following C code: #include <stdio.h> int main() { int a = 5; int b = 15; return a + b; } Compiling it using gcc creates an assembly code which includes the following: ...
13
votes
3answers
685 views

Is using C/C++ macros as a shortcut for conditional compilation a good practice?

Let's say I want to have several types of output messages in my code. One of them is DEBUG, which is printed only, when the code is compiled in Debug mode. Usually I'd have to write something like #...
-2
votes
1answer
96 views

How can I find the address of my first memory location

If i'm writing my own kernel I will have access the the physical memory (instead of a virtual memory). Now assume I want to make a loop starting from the first memory location until the last one and ...
-3
votes
0answers
66 views

How does an Operating System gets allocated its first memory space [duplicate]

Going through osDev.org, I managed to write a small kernel that outputs a memory address, similar to this: #include <stdio.h> int main() { size_t a = 2; size_t *p = &a; printf("...
-3
votes
2answers
194 views

How are memory addresses created?

#include <stdio.h> int main() { size_t a = 2; size_t *p = &a; printf("%p\n", p); } The above code will print the memory address of a. I understand that within an OS, this ...
2
votes
5answers
411 views

Why is there a limit on data types

I know that an int for example has a max value of 2,147,483,647 and is stored in 4 memory slots (each 8 bits and a total of 32 bits or simply a word). Int can be loaded into a 32 bit Register just ...
2
votes
4answers
546 views

Is an Array Index content stored in one memory address?

Assume in C we have the following array of one item: int a[] = {2000}; 2000 in binary would be: 11111010000 If each memory address can hold 1 byte (8 bits) of data, then howcome in some tutorials, ...
15
votes
2answers
3k views

Is converting a C++ method to a C function with a pointer argument an acceptable pattern?

I use C++ on ESP-32. When registering a timer I have to do this: timer_args.callback = reinterpret_cast<esp_timer_cb_t>(&SoundMixer::soundCallback); timer_args.arg = this; Here the timer ...
2
votes
1answer
179 views

Handling hardware dependency while unit testing

I am writing a driver for an IMU sensor using test driven development approach. Communication protocol of choice is SPI. Using CubeMX and STM32F415 processor, SPI interface is implemented in ...
-1
votes
1answer
195 views

Few unusual C/C++ declarations

I came across this Enum and Struct declarations in a project supposedly done by an expert. The declarations / definitions are little different than what im used to so far. enum EnumKeys { KEY_MENU ,...
1
vote
1answer
128 views

Find a string in list of strings

Background: I am writing an application for a small embedded device. There is a static list of strings: currently about 500 strings and string length is 12 characters on average. The list might ...
2
votes
1answer
185 views

How to abstract HAL in embedded test driven development?

Recently I've been reading into topic of test driven development and decided to give it a go with a sensor driver I need. Sensor uses SPI interface and I will implement this driver on STM32F415 ...
5
votes
3answers
183 views

How to handle repetitive mallocs/frees in a DRY way in C

So I have a function which is like a constructor for my structure: MyStructure* CreateMyStructure(...) { MyStructure *my_structure; double *data; int *colind, *rowptrs; data = malloc(...
3
votes
1answer
658 views

Why do we need to specify the type of data a pointer will hold, if all pointers are the same [duplicate]

Why do we need to specify the type of the data whose address, a pointer will hold, if all pointers are the same. Since all pointers store addresses. Also, the amount of space a pointer will require in ...
1
vote
2answers
189 views

Using C coding conventions in C++ code [duplicate]

I'm working on a large codebase whose core modules are in C and extension modules are in C++. We have a coding convention for C code but I'm being told that we need to enforce the same coding ...