Just another site

Archive for the tag “c++”

Vectors and constructors with rand()

So here is a silly mistake I just made; say Foo is a class whose constructor includes setting a variable randomly and you want a population of Foos, then

vector foos(n,foo);

isn’t what you want, this only calls the Foo constructor once and then makes n copies of the result, so the random value is the same for each instance! You need

vector foos;
for(int i=0;i<n;++)
Foo foo;


Declaring two pointers.


Foo * bar1, * bar2;

note the extra star. Makes sense when you think about it.

Dev-C++ is back!

I am about to start teaching C++ again so I need an IDE to recommend to students using windows, I was pleased to see Dev-C++ has been updated again at last!

gcc optimizations.

Today I spent trying to compare how fast different algorithms for calculating the van Rossum metric run; I ran sample code compiled using the -O3 flag in gcc. I have never known how these optimization settings work, but today I was profiling the code using gprof and was amazed to see how in-line the code is when optimized, the length of the gprof output is determined in part by how many function calls there are, the output is 163 lines with -O3 and 1042 without.

Post Navigation