New answers tagged

2

There is indeed a way to do that, but also likely a better way to approach the problem. I'll show both. Add instrumentation It's not always to tell when constructors and destructors are being called, so it's useful to add instrumentation. Here's some code I use sometimes that uses the Curiously Recurring Template Pattern, or CRTP for short. It isolates ...


2

The easiest way is to use the PIMPL idiom, also colloquially called “compilation firewall”. This technique does not need conditional compilation to define the structure differently: in your public header, you just define opaque pointers to a struct which you do not define. In this case C will handle the struct as an incomplete type, meaning that you can only ...


6

One of the biggest strengths of C is also one of its biggest weaknesses: it has a very small runtime and standard library. This is a strength because it makes C very portable and highly reusable as a base for other things, even in compilers of other languages. It's a weakness because you have to look to third party libraries for things that feel like they're ...


0

I’d find that rule very unhelpful, and it leads to worse code and more bugs. In newer languages it is very bad. In Swift for example, and probably in other languages, it is a very good idea to have variables that only are assigned a value once - and that means they must be declared in a nested scope in practice.


1

Since strcpy, strcat, and sprintf are dangerous, ... strcpy, strcat, and perhaps sprintf are better used as building blocks for safer functions than used as safe functions unto themselves. If due to the local usage of strcpy, strcat, overrun is not possible, then there is is no safety concern (aside from possible buffer overlap). How to form a safer ...


2

It's basically just one loop that scans from left and right to a midpoint. Here's a simpler version that does the same thing (though omits optimizations): void segregateEvenOdd(int arr[], int size) { int left = 0; int right = size - 1; while (left < right) { if (arr[left] % 2 == 0) { left++; } ...


3

It seems like using the same variable 'i' to iterate through the two for-loops would overlap and cause errors. Indeed, it would. Well, not "errors", so much as unexpected behavior. The code you have is syntactically fine; no C or C++ compiler that I know of would raise any warnings, much less errors. However, if you run it, you'll see that it ...


Top 50 recent answers are included