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.

Proper way to pass dynamic arrays to other functions

Actually, the two first ideas pass the array by address and the third passes the array by reference. You can devise a little test to check this: The output of this test is If a parameter is passed by value, it cannot be modified inside a function because the modifications will stay in the scope of the function. In … Read more

C++ performance vs. Java/C#

JIT vs. Static Compiler As already said in the previous posts, JIT can compile IL/bytecode into native code at runtime. The cost of that was mentionned, but not to its conclusion: JIT has one massive problem is that it can’t compile everything: JIT compiling takes time, so the JIT will compile only some parts of … Read more

“Incomplete type not allowed ” when creating std::ofstream objects

As @Mgetz says, you probably forgot to #include <fstream>. The reason you didn’t get a not declared error and instead this incomplete type not allowed error has to do with what happens when there is a type that has been “forward declared”, but not yet fully defined. Look at this example: Notice we could not actually instantiate a Foo object or … Read more

heap corruption detected | C++

“Heap corruption” generally means you wrote into unallocated memory, damaging the data structures used to make the memory allocator work. There may be more problems, but the first one I see is on this line: This will write strlen(n) + 1 bytes to buffer, but buffer is only strlen(n) bytes long (the extra byte is the terminating \0.) Writing that extra byte results … Read more

c++ vector bubble sort

You need to pass by reference; by making a copy, you sort a temporary copy, and then that’s it; it disappears and the original is still unsorted, because, once again, you sorted only a temporary copy of the entire vector. So change vector<int> a to vector<int>& a. Here’s the code, fixed: http://coliru.stacked-crooked.com/a/2f118555f585ccd5

List iterator not dereferencable?

Could you have a race-condition? If the list were empty, then I’d expect a problem when trying to dereference begin(), but you check for empty. Do you have another thread adding or removing items from list in parallel? Your code snippets works for me on VS 2008 (assuming I typedef Counted_message_reader to int).

Converting bool to text in C++

How about using the C++ language itself? UPDATE: If you want more than 4 lines of code without any console output, please go to cppreference.com’s page talking about std::boolalpha and std::noboolalpha which shows you the console output and explains more about the API. Additionally using std::boolalpha will modify the global state of std::cout, you may want to restore the original behavior go here for … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)