Tag Info

Hot answers tagged

42

You should always include all headers defining any objects used in a .cpp file in that file regardless of what you know about what's in those files. You should have include guards in all header files to make sure that including headers multiple times does not matter. The reasons: This makes it clear to developers who read the source exactly what the ...


14

The general rule of thumb is: include what you use. If you use an object directly, then include its header file directly. If you use an object A that uses B but do not use B yourself, only include A.h. Also while we are on the topic, you should only include other header files in your header file if you actually need it in the header. If you only need it in ...


7

I keep wondering whether or not I should explicitly include all headers used directly in a particular file Yes. You never know when those other headers might change. It makes all the sense in the world to include, in each translation unit, the headers you know that translation unit needs. We have header guards to ensure that double-inclusion is not ...


7

Monomorphization. For every generic (polymorphic) type/function, generate a non-generic (monomorphic) version for every set of type parameters. Given these declarations: struct G<T> { a: T, b: T } fn get_a<T>(g: G<T>) -> T { return g.a; } and this code: x = G<int>(1, 2); y = G<float>(1.0, 2.0); get_a(x); get_a(y); ...


6

"C program source text is free-format, using the semicolon as a statement terminator and curly braces for grouping blocks of statements." (Wikipedia) Blocks don't look like control flow, but they are; without the curly braces only the next line would be controlled by a loop keyword, but with the braces the entire block is looped over.


5

The answer is yes. The Linux kernel is written almost entirely in C. A very teeny-tiny portion is written in assembler, and those portions consist of only portions related to low-level functions that can't be done in C, such as setup of the target CPU at boot, setting up hardware interrupts, power information through ACPI/EFI, etc. Otherwise, pretty ...


5

Take a look at this neat implementation of sorting six integers. According to sorting networks, 16 comparisons are required to sort 7 integers. Here is the code for seven integers: static int sort7(int *d){ #define SWAP(x,y) if (d[y] < d[x]) { int tmp = d[x]; d[x] = d[y]; d[y] = tmp; } SWAP(1, 2); SWAP(3, 4); SWAP(5, 6); SWAP(0, 2); ...


4

Arrays aren't the only option for implementing autorelease pools. Anything suitable for a set implementation should be suitable for an autorelease pool, including (single- or double-linked) lists, trees, and hash tables. When picking which data structure to use, consider the timing of the various operations and match that to usage. For an autorelease pool, ...


4

sin() and tan() typically return the same sign zero result for a given sign zero input It could be generally true since: Speed/accuracy. For small enough doubles, the best answer for sin(x) is x. That is, for numbers smaller than about 1.49e-8, the closest double to the sine of x is actually x itself (see the glibc source code for sin()). Handling of ...


4

currently there are 2 ways; the type erasure method of java; essentially everything becomes a void* (or a custom type like a struct base{void** functptrs;} and gets casted to and fro as needed, this requires the type to follow a certain interface to let the template figure out if casts are valid at runtime. instantiation creates a new definition of the ...


3

Importing in Java, C# and the like is fundamentally different from including in C and C++: The former will add the named modules to those considered for symbol resolution, the latter inserts the named headers content literally into the translation unit. Which means a header can contain anything the source-file can, including translation-unit-local objects ...


3

This is possible but has limitations. An implementation would utilize setjmp() / longjmp(). You can see an example implementation here: http://aceunit.sourceforge.net/doxygen/ExceptionHandling_8h.html Beware: return within try-blocks would be a bug!


2

You're going to struggle to make this work as well as you're hoping. The precise figures vary depending on operating system, but if you try opening more than a few hundred sockets at a time on an ongoing basis you're going to start running out of system resources pretty quickly. On windows desktop machines the limit is lower still (windows desktop prevents ...


2

You can use a XOR linked list. Traversing the list will then be: Node* trailing = 0, current = head; while(current){ //visit current->value Node* next = trailing ^ current->next; trailing = current; current = next; } You need to have 2 sequential elements to be able to traverse the list. And inserting and removing is more complicated. ...


2

The usual solution is to log to a file, and to use log rotation. You can implement the rotation yourself, or use a daemon like logrotate.


1

If you are not using precompiled headers then each include means that the header is included fully into each compilation unit. That means reading the file and putting it through the same pipeline of the rest of the code and filling the symbol table. So yeah unused includes will slow down compilation. Some compilers will have optimizations in place that ...


1

I am taking a similar slightly different approach from proposed answers. In headers, always include just a bare minimum, just what is needed to make the compilation pass. Use forward declaration wherever possible. In the source files, it is not that important how much you include. My preferences are still to include minimum to make it pass. For small ...


1

Opinions differ on this, but I am of the view that every file (whether c/cpp source file, or h/hpp header file) should be able to be compiled or analysed on its own. As such, all files should #include any and all header files that they need - you should not assume that one header file has already been included previously. It is a real pain if you need to ...


1

After further thought (and your comment), I realise there's a solid behavioural basis for choosing. If I: open an existing file in your editor begin altering it realise I want a backup and move (not copy) the original save my modifications should it alter the backup (the original inode with a new name)? Or write the altered file to the original name, ...


1

Well, you can: Use log rotation. After given time, close the log, move it away and open new one. Log to a socket, either UDP or unix domain. In this case the message will be forgotten if you don't have any listener connected. Put some lightweight syslog daemon there and use syslog(3). There you can configure file with rotation, socket and some other ...


1

As the typical intention of code like this uint16_t ffff16 = 0xFFFF; int64_t who_knows = ffff16 * ffff16; is to perform the multiplication in 64 bits (the size of the variable the result gets stored in), the usual way to get the (platform independent) correct result is to cast one of the operands to force a 64-bit multiplication: uint16_t ffff16 = ...


1

The usual way is to retain for the calling code, To make it consistent you should do it for all objects that you return. //popFromQueue will remove from the queue and pass the buck to the calling code Foo* pop(FooQueue* q){ Node* foo = q.head; q->head = foo->next; foo->next=null;//make sure new head isn't released; could have also ...


1

If the documentation for a particular compiler says what it will do when code does something which is considered "Undefined Behavior" by the standard, then code which relies upon that behavior will work correctly when compiled with that compiler, but may behave in arbitrary fashion when compiled using some other compiler whose documentation does not specify ...


1

How would your proposed extension handle the situation where a multi-dimensional array isn't allocated statically? Having the extension be limited to cases where the allocation is static would make its behavior inconsistent with other array handling, but if the extension isn't limited to static array allocations then passing a double[5000][5000] to function ...



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