Jump to content
  • Advertisement
Sign in to follow this  

Programming traps

This topic is 4093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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


Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Add a space between the brackets and it'll work.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!