Hot answers tagged

10

In practice, it's not going to matter most of the time. It won't matter in most programs. For some programs where it might potentially matter, it might still not matter because there's no significant, measurable performance difference. For those programs where there is a measurable performance difference, it might still not matter because the program ...


6

In my opinion, that is a horrible paradigm. I see absolutely no pros and at least three substantial cons. Needless code complexity Since malloc(0) can return NULL, the code has to be written to handle that anyway. And since malloc(0) can also produce a non-NULL result, the code also has to be written in a way to handle a non-NULL pointer. Pointer ...


6

Robert Harvey's answer is fully correct, but since you mentioned Game Engine Architecture, I think it is worth to add a few words to the case where speed actually could matter. The described effect of "more CPU work" when "reading a 16-bit value on a 64-bit machine" is something which on some CPUs may occur, and on other's not. But when ...


4

You see an absolute rule, and things don't work that way. You also seem to be focussed on macros vs. inline functions which is very rarely the question. Macros can be extremely useful if you know how to use them. They can also be anything from misleading to dangerous if you don't know what you're doing. I haven't used the two projects that you mention, but I ...


4

So, if I just use long long (64 bit integers), will my code be faster than if I use less bits integers Not automatically no. Robert has already covered the most of it. But there was one thing I thougtht was worth mentioning. If you save four 16 bit integers in one 64 bit integer, you will of course have a bit overhead separating them. But the code can ...


4

In short It's a bad practice because "magic numbers" make localization and maintenance more difficult, since someone else (or a future version of you) will not know (anymore) why this magic number was as it was. So the good practice is to avoid magic numbers. Or at least, give them a name with const or #define, and some comments to remind how they are ...


4

Let's start by defining a magic number. 3.14 What is that? Is it pi? If it is, it should be codified into your program as: const double pi = 3.14; Why? The name gives it meaning beyond it being just some arbitrary number. It's more readable. double area = pi * radius * radius; You can change it in one place (for example, adding more digits of ...


2

Note that since C17/18 a subtle addition occurred: If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned to indicate an error, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object. § 7.22.3 1 Now when malloc(0) ...


2

A plain number like 11 can have different meanings - it could mean “eleven things”, or it could be an error code like “thing not found”, or it could be a command code to “do this thing”, or it could be a bitmask to check several flags at once, and it can mean all of those things in the same code. This makes maintenance a nightmare, because if you need to ...


2

There are two bad things with magic numbers, one of which is intrinsic to the concept and one that just is a consequence of practicalities. The intrinsic drawback is that it's so much harder to understand. It does not convey any information except for the raw value. A named constant is much more readable. The practical drawback is that whenever a magical ...


1

If you found a case where VLAs don’t work, I’d say that is a pretty good reason not to use them. The real problem is likely that stack space is limited (megabytes) while malloc is much less limited (gigabytes). Never use VLAs if you don’t have a low maximum size for the array. So not for “files of very large sizes”.


1

The only way to guarantee that no DRAM gets used by your program is to make sure that the computer running your program uses some other kind of RAM modules. Unless you have a severely underpowered processor in your system, such that 100% CPU time is required to copy bytes between modules on the PCIe bus, your program will spend part of the time waiting for ...


1

Addressing a specific part of your quote: For example, when reading a 16-bit value on a 64-bit machine, a full 64 bits worth of data must still be read from memory. The desired 16-bit field then has to be masked off and possibly shifted into place within the destination register. This is true for some architectures, but not all of them. Particularly, the ...


1

The following does not apply to jagged arrays: Any of the following C expressions can be l-value expressions: A subscript ([ ]) expression that does not evaluate to an array A jagged array is an approach to a two-dimensional data structure that allows for varying the 2nd dimension between positions of the 1st dimension.  ...


Only top voted, non community-wiki answers of a minimum length are eligible