Programming traps

Started by
43 comments, last by ddyer 17 years, 1 month ago
Just wanna know the common programming mistakes which can't be detected by compilers. Here is an example on which I spent the whole yesterday to find it out.

vector<CNPC*> NPCs;

for(i = 0; i < 100; ++i) {
	CNPC npc(Vector(i, 0));	// create a line of NPCs
	NPCs.push_back(&npc);
}
//	Now the vector NPCs stores nothing as the objects are deleted just after the scope


Advertisement
There are thousands. And they are not really traps, they are just people misunderstanding how things really work.

theTroll
*(int*)0=0; ...always gets me
Construct (Free open-source game creator)
ifstream config_file("config.tx"); //oops that should be "config.txt"

somehow it always takes me days to find string errors. The symptoms always seem to indicate something else.
Quote:Original post by Glak
ifstream config_file("config.tx"); //oops that should be "config.txt"

somehow it always takes me days to find string errors. The symptoms always seem to indicate something else.


This is one good reason to keep string literals out of your code as much as possible, and read your text data in from a file instead. (i18n concerns would be the other main reason.) You could also accept the "main config file name" from the command line ;)
std::vector<std::vector<int>> on some compilers won't parse correctly :(
Mostly due to the >> operator
I've got stuck for a long time on that problem.
Add a space between the brackets and it'll work.
I hit a nasty one the other day:

void func1( const std::string &szFoo )
{
func2( szFoo.c_str() );
}

void main()
{
func1( "this is a string" );
}

This causes memory corruption, at least using the STL implementation in VC2005. Something about c_str() being marked const but actually doing something to the memory, which doesn't behave well with static strings.

Geoff
Quote:Original post by gdunbar
I hit a nasty one the other day:

void func1( const std::string &szFoo )
{
func2( szFoo.c_str() );
}

void main()
{
func1( "this is a string" );
}

This causes memory corruption, at least using the STL implementation in VC2005. Something about c_str() being marked const but actually doing something to the memory, which doesn't behave well with static strings.

Geoff

Is func2 a third-party function? It seems to use const_cast or do low-level manipulations (possibly in assembly) and I would stay away from code which did that.

As others have said there are just too many and you'll encounter them all the time. The most important thing is to learn from your mistakes. If I find myself often getting errors in my hard-coded strings then I try to avoid hard-coded strings and remember to pay extra attention every time I have to hardcode a string. If I had problems with template classes inside template classes then I would try to use typedefs and when I don't then pay extra attention.
The Most Vexing Parse of C++ bit me yesterday, albeit in a slightly modified form.

Inside a member function, I had the following line:

scissor(bounds());

where scissor is a type and bounds is another member function of the same class. It seems like it should create a temporary scissor object, passing the result of bounds() as a parameter, but instead it is equivalent to this:

scissor bounds();

That is, a declaration of a function called bounds taking no parameters and returning a scissor object!

Quote:
std::vector<std::vector<int>> on some compilers won't parse correctly :(

It shouldn't, according to the Standard. Fortunately, the next revision will fix this. Meanwhile, as has been said, just put a space between the brackets.

This topic is closed to new replies.

Advertisement