Skip to main content

All Questions

-1votes
5answers
2kviews

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 ...
10votes
6answers
4kviews

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 ...
3votes
4answers
3kviews

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

Recently I was going through some of the source-codes of the best competitive programmers in the world. I found out that those people use a template while writing programs, preferably in C++. I have ...
-4votes
2answers
150views

How to exploit efficiency of C? [closed]

I want to write a program that performs heavy computations and I want it to be as fast as possible, so I choose C to be the language. Nevertheless, I was told that in spite of its simplicity and high ...
1vote
1answer
223views

Alternatives to service locator with opaque pointer in C

I have a project in which one module keeps the state of the target device (things like current command level, but mostly status registers caches). I'm aware that having a global public variable (...
0votes
2answers
1kviews

Programmatically create header file

What's the best way to programmatically create a header file for another project? Here's the specific use case: one program fingerprints the device for discrete information like version number, id ...
56votes
11answers
24kviews

What are the best practices regarding unsigned ints?

I use unsigned ints everywhere, and I'm not sure if I should. This can be from database primary key id columns to counters, etc. If a number should never be negative, then I will always used an ...
56votes
6answers
287kviews

Is initializing a char[] with a string literal bad practice?

I was reading a thread titled "strlen vs sizeof" on CodeGuru, and one of the replies states that "it's anyways [sic] bad practice to initialie [sic] a char array with a string literal." Is this true,...
2votes
1answer
791views

Some questions about implementing a preemptive scheduler in C: Context switching and execution time of the dispatcher

I am trying to implement a preemptive scheduler in C, but I have some understanding problems: When the scheduler is called by an interrupt, a context switch may occur. The context switch can only be ...
14votes
8answers
34kviews

Are C strings always null terminated, or does it depend on the platform?

Right now I am working with embedded systems and figuring out ways to implement strings on a microprocessor with no operating system. So far what I am doing is just using the idea of having NULL ...
27votes
1answer
20kviews

Where did "exit(-1)" come from?

I see in a lot of legacy software and bad tutorials on the Internet that recommend using exit(-1), return -1 or similar to represent "abnormal termination". The problem is, in POSIX at least, -1 has ...
12votes
3answers
3kviews

Is it bad to refer to access array elements via pointer arithmetic instead of the [] operator?

I've just started learning to program in C, and to improve my understanding of pointers and arrays, I tried to refer to the elements of an array without creating any pointer at all: for(k1 = 0; k1 &...
5votes
6answers
5kviews

Why is C++ backward compatibility important / necessary? [closed]

As far as I understand it is a wide-spread opinion within the C++ community that certain features of C++ (including some features inherited directly from C), while still usable in themselves, do not ...
3votes
3answers
1kviews

How to maintain modularity in C?

I am writing C code with multiple modules like LCD display, flash memory, and GSM module etc. The project consists of thousands of lines of code, in different files. The behavior of the system can be ...
22votes
2answers
4kviews

Has variable width types been replaced by fixed types in modern C?

I came across an interesting point today in a review over on Code Review. @Veedrac recommened in this answer that variable size types (e.g. int and long) be replaced with fixed size types like ...

153050per page
close