Favourite Mistakes

Here is one that I've made a number of times - I'll let you figure out what is wrong with it.

1
2
3
4
5
6
7
std::vector<ClassA> a;
std::vector<ClassA*> pa;
for(unsigned int i = 0; i < sum_num; i++){
   a.push_back(ClassA());
   pa.push_back(&a[i]);
}
//... 


I've got a few more good ones - I'll add them when I get a chance.

Please add any that you've been unlucky enough to come across.

Nick.
Just remembered another one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct StructA{
   unsigned char a;
   unsigned char b;
   StructA(unsigned char _a, unsigned char _b) : a(_a), b(_b) {}
};
bool operator<(const StructA& lhs, const StructA& rhs){
   if(lhs.a < rhs.a) { return true; }
   return false;
}

//...

std::map<StructA,int> some_map;
some_map[StructA(3,5)] = 3;
some_map[StructA(3,8)] = 8;

//...
what is the error you are getting here in the first one and in the second one .
The first one is flawed because vector can rearrange contents in memory when you append items.

So all the pointers being put in 'pa' might quickly become invalid pointers.

I didn't look much at the 2nd one.
I'm guessing the second one is an incomplete comparison operator, so the two objects are inserted at the same position, the second erasing the first one?
@bartoli: Yep - that one took some serious debugging haha
Last edited on
This one got me good when I was getting started:

1
2
3
4
5
6
7
//...
unsigned int aSize = 10;
std::vector<StructA> a(aSize);
for(unsigned int i = aSize-1; i >= 0; i--){
   a[i].some_unsigned_int_member = i;
}
//... 
Last edited on
Didn't the compiler warn you?
Nope. May I ask now, why the heck anyone would use postfix and not prefix ++ and -- operators? Don't give me any 'compiler optimization' stuff ;p

REAL men write code like this:
1
2
3
4
for(unsigned long i = 0; i < number; ++i) //preincrement ftw
{
    //
}



Anywho, I see a lot of people make this mistake with color codes:
color = (0, 255, 128);
"Why does it only have a halfway red color?"
Usually from people new to programming and trying to write HLSL shaders.
Last edited on
L B wrote:
May I ask now, why the heck anyone would use postfix and not prefix ++ and -- operators?

A few years ago I thought prefix ++ was ugly. Now I don't know what I think so I use whatever I feel like.
They are different and can have different uses. Of course, if you just wnat to increment something, pre is faster. But the temporary used in the post can be handy.
Last edited on
I understand there are uses, I just wonder why people would write i++ by itself instead of ++i
I think it's a holdover from C back in the day. postfix makes more sense in that context, as manually iterating with a pointer is more common.

I'd see this code a lot: *foo++;, which to this day I have to stop and think about exactly what it's doing (*stabs people who don't use parenthesis*).

Anyway postfix makes sense in that [relatively common] situation, and prefix didn't make sense in as many situations. And if you're using postfix in one place, why not use it in every place to be consistent?



Though I agree that prefix should be preferred unless you have reason to use postfix.
Topic archived. No new replies allowed.