clearing a vector of pointers [duplicate]

Yes, the code has a memory leak unless you delete the pointers. If the foo class owns the pointers, it is its responsibility to delete them. You should do this before clearing the vector, otherwise you lose the handle to the memory you need to de-allocate. You could avoid the memory management issue altogether by … Read more

What does _T stands for in a CString

_T stands for “text”. It will turn your literal into a Unicode wide character literal if and only if you are compiling your sources with Unicode support. See http://msdn.microsoft.com/en-us/library/c426s321.aspx.

C++ Signed/unsigned mismatch

The signed/unsigned nature of the two values you are comparing should be the same, otherwise one gets cast as the other for the comparison, which can lead to unexpected results. It would be best to make sure that what you’re comparing are the same type, but: If you know which value is safe to cast, … Read more

Why are there two different getline() functions (if indeed there are)?

Bear in mind that the Standard library is composed from 3 (main) parts: IOStream, String and STL, plus some tossed in goodies and the C-headers. I don’t see anything weird in having those parts loosely coupled (though I wish it was not the case). Other incongruities include: std::string::length vs std::string::size, the latter having been added for interface compatibility … Read more

compare and swap vs test and set

test-and-set modifies the contents of a memory location and returns its old value as a single atomic operation. compare-and-swap atomically compares the contents of a memory location to a given value and, only if they are the same, modifies the contents of that memory location to a given new value. The difference marked in bold.

Update g++ but still old version

Installing a newer (or older) version of GCC than the Ubuntu default version via the package manager does not delete the default version. You get both. You can install as many versions as you like. gcc/g++ will continue to run the default version. If you have installed GCC 7, then you run the new compilers with gcc-7 or g++-7. For … Read more

Weighted random numbers

There is a straightforward algorithm for picking an item at random, where items have individual weights: 1) calculate the sum of all the weights 2) pick a random number that is 0 or greater and is less than the sum of the weights 3) go through the items one at a time, subtracting their weight … Read more

compare and swap vs test and set

test-and-set modifies the contents of a memory location and returns its old value as a single atomic operation. compare-and-swap atomically compares the contents of a memory location to a given value and, only if they are the same, modifies the contents of that memory location to a given new value. The difference marked in bold.