Favourite Mistakes

Jan 23, 2012 at 3:39pm
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.
Jan 23, 2012 at 3:51pm
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;

//...
Jan 23, 2012 at 6:09pm
what is the error you are getting here in the first one and in the second one .
Jan 23, 2012 at 6:30pm
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.
Jan 23, 2012 at 7:04pm
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?
Jan 24, 2012 at 12:11am
@bartoli: Yep - that one took some serious debugging haha
Last edited on Jan 24, 2012 at 12:18am
Jan 25, 2012 at 12:25am
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 Jan 25, 2012 at 12:41am
Jan 25, 2012 at 12:29am
Didn't the compiler warn you?
Jan 25, 2012 at 2:13am
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 Jan 25, 2012 at 2:14am
Jan 25, 2012 at 2:19am
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.
Jan 25, 2012 at 3:23pm
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 Jan 25, 2012 at 3:23pm
Jan 25, 2012 at 10:16pm
I understand there are uses, I just wonder why people would write i++ by itself instead of ++i
Jan 25, 2012 at 11:09pm
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.