Tag Info

Hot answers tagged

31

Because implicit type conversions are usually unsafe, and C++ takes a more safe stance to typing than C does. C will usually allow implicit conversions, even if most chances are that the conversion is an error. That's because C assumes that the programmer knows exactly what they are doing, and if not, it is the programmer's problem, not the compiler's ...


22

Here is what Stroustrup says: In C, you can implicitly convert a void* to a T*. This is unsafe He then goes on to show an example of how void* can be dangerous and says: ... Consequently, in C++, to get a T* from a void* you need an explicit cast. ... Finally, he notes: One of the most common uses of this unsafe conversion in C is to assign ...


21

When used properly, enums are far more readable and robust than the "magic numbers" they replace. I don't normally see them making code more brittle. For instance: setColor() doesn't have to waste time checking if value is a valid color value or not. The compiler has already done that. You can write setColor(Color::Red) instead of setColor(0). I believe ...


15

Any change to the ColorChoice enum affects all IWindowColor subclasses. No it doesn't. There are two cases: implementers will either store, return and forward enum values, never operating on them, in which case they are unaffected by changes in the enum, or operate on individual enum values, in which case any change in the enum must of course, ...


14

Enums do not create brittle interfaces. Misuse of enums does. What are enums for? Enums are designed to be used as sets of meaningfully named constants. They are to be used when: You know that no values will be removed. (And) You know it is highly unlikely that a new value will be needed. (Or) You accept that a new value will be needed, but rarely enough ...


10

A "raw" pointer is unmanaged. That is, the following line: SomeKindOfObject *someKindOfObject = new SomeKindOfObject(); ... will leak memory if an accompanying delete is not executed at the proper time. auto_ptr In order to minimize these cases, std::auto_ptr<> was introduced. Due to the limitations of C++ prior to the 2011 standard, however, it's ...


9

In C, there is no need to cast a void * to any other pointer type, it is always safely promoted. It's always promoted, yes, but hardly safely. C++ disables this behaviour precisely because it attempts to have a safer type system than C, and this behaviour is not safe. Consider in general these 3 approaches to type conversion: force the user to ...


5

In most plugin systems in C and C++, the loading of plugins works like this: The main application specifies a path where the plugins should be located (or allows such a path to be configured). On startup, the application looks for either dynamically loadable libraries (DLLs on Windows, .so on Linux, in case of compiled plugins) or scripts (in case of ...


4

Enums are a great improvement over magic identification numbers for closed sets of values that do not have a lot of functionality associated with them. Usually you don't care about what number is actually associated with the enum; in this case it is easy to extend by adding new entries at the end, no brittleness should result. The problem is when you have ...


3

You could also be interested in the system programming aspect of your task. Then read Advanced Linux Programming, read the intro(2) & syscalls(2) man pages. Be aware of the POSIX standard. For thread programming, read a good Posix thread tutorial or a C++11 thread tutorial. Regarding C++ programming, the Qt and POCO projects should interest you (and ...


3

Java and C and C++ have some superficially similar syntax traits (several keywords and lexical conventions in common; use of blocks with braces, ...), because both Java and C++ got a lot of inspiration from the C programming language. However, Java and C++ are very different languages. Not only their syntax, but also their semantics are very different. ...


3

No. The Google style guide makes no sense for anybody except Google. And even for them it's highly questionable. The best approach to take is to forget the Google Style Guide and everything you've read in it, because it's junk. Rvalue references are a great and wonderful thing that drastically improved C++. Not using them will consign your code to the pits ...


3

What Python does is writing the encoding out in the source code, specifically a specially-formatted comment. See PEP 263. It doesn't need to be a comment, but spelling out the encoding is pretty much the only sensible approach. "Guessing" encoding from raw bytes does not work very well, and is far too magical for the tastes of most programmers. It would be ...


2

Short answer: Absolutely no guarantees can be made about a program that crashes (or in fact one that DOESN'T crash). If an application crashes, the entire program's data (and any other writable memory areas, files, databases, etc that this program MAY have touched) areas must be treated as "probably not correct". Long answer: Since there is absolutely no ...


2

I do not have enough reputation to comment but everything that was said in the other answers is very relevant. One important thing partially mentioned by ddyer is that wrapping malloc/free has benefits. He mentions a few but I would like to add a very important debugging tool to that: you can log every malloc/free into an external file along with a few lines ...


2

1) Let's assume that t.count() is a linear-time operation with respect to the size of the bitset (it probably isn't on real hardware, but ignore that). Then it would be correct to say that the algorithm is O(M*N^2), for the obvious reason that you have an M-iteration-loop (t.count()) inside an N-iteration-loop inside an N-iteration-loop. That might be ...


2

There are the features that the language provides, and there are other features that the OS provides. iostream for example is built in to the language. conio.h is specific to Windows. There are other headers too that are specific to Linux.


2

The question is, how do I do this in C++? You will not be able to map the Perl initialization in C++ without making it explicit. Components for a plugin system in C++: define your plugin interface as a header file (will probably contain the declaration of a plugin initialization function/class, a configuration base class interface and class factory, ...


2

Both of your designs have the problem that they put the burden of keeping track of the validity of the favoriteColor attribute on the user of the class. Your second design (with the getHasFavoriteColor) has the additional problem that it duplicates the information if there is a valid value for favoriteColor in a way that makes it very easy to create ...


1

I believe what you're directly asking for is covered by the combination of these StackOverflow questions: http://stackoverflow.com/questions/1528298/get-path-of-executable http://stackoverflow.com/questions/675039/how-can-i-create-directory-tree-in-c-linux The unfortunate reality is that these tasks cannot currently be done in a portable fashion as there ...


1

It is operating system specific. On Linux with X11, you could simply use the fact that DISPLAY is an unset environment variable outside of graphical desktop interfaces (or test for the success of XOpenDisplay); You might also use isatty(3) on STDIN_FILENO(which is 0) to test if stdin is a terminal (but you could also open /dev/tty and see if it fails, cf ...


1

If you are careful in how you use them, I'd not consider enums harmful. But there are a few things to consider if you want to use them in some library code, as opposed to a single application. Never remove or reorder values. If you at some point have an enum value in your list, that value should be associated with that name for all eternity. If you want ...


1

After experimenting for a while I have a solution. I've split the network of machines into many EnergyNet objects, which represents something like a cluster of wires. Each EnergyNet represents a system of energy suppliers, consumers, and storagers, which are a combination of both. Every update tick, the energy net measures the amount of energy produces and ...


1

(Disclaimer: I only have experience in parallelizing computational workloads. I do not have experience in async I/O. The opinions herein are merely based on reading online articles, blogs and Q&A websites.) Applying concurrency to a C++ project is more or less a rigorous exercise in software engineering risk management. You have to choose strategies ...


1

The syntax is quite similar. You will find the ++ operator in both languages. Many of the keywords are the same and have similar (or the same) meanings. However they are both different languages. C++ was developed from the C language, by adding object-orientation on to it. Java was developed as an object-oriented language from the start, and operates in ...


1

On Linux, if compiling code with GCC, the static data's constructor is run at dlopen(3) time. See also the visibility attribute and the GCC specific __attribute__((constructor)) for functions. However, you should define a plugin convention: which names (for extern "C" C++ functions inside plugins) are used, with which signatures, in what order, etc. For ...


1

The reason you find differences is that the C you are using is not GNU C or ANSI C. It's better if you learn the language well instead of trying to find the differences. I'd suggest referring C: The C Programming Language, 2nd Edition by Brian W. Kernighan and Dennis M. Ritchie C++: The C++ Programming Language (3rd Edition) by Bjarne Stroustrup


1

By definition, A void pointer can point to anything. Any pointer can be converted to a void pointer and thus, you will be able to convert back arriving at the exact same value. However, pointers to other types may have constraints, such as alignment restrictions. For example, imagine an architecture where characters can occupy any memory address but integers ...


1

This question is indeed too broad to be answered by a single answer on any single StackExchange site. You might want to visit the Information Security site to read about the practical techniques that you will want to adopt for your software. Of course, you can probably find similar techniques discussed on this site or at StackOverflow, written from a ...



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