Hi,
the following code crashs:
#include
#include
#include
#include <cstring> // memset
#include <iostream>
union block {
char buffer[512];
int integer[128];
};
std::size_t sum( block* blk )
{
using namespace boost::lambda;
std::size_t sum = 0;
std::for_each( &blk->buffer,
&blk->buffer + sizeof( blk->buffer ),
sum += *_1 );
return sum;
}
int main()
{
using namespace boost::lambda;
block blk;
std::memset( &blk.buffer, '1', sizeof( blk.buffer ) );
std::for_each( &blk.buffer, &blk.buffer + sizeof( blk.buffer ),
std::cout << *_1 << '\n' );
std::cout << sum( &blk ) << std::endl;
}
I'm not sure for the iterator initialization - maybe this fails. I did
not wrote C style iterators for a long time and forgot this obviously.
What's up here? The goal is the get the sum of the chat buffer.
Do I have to protect the sum variable in function sum?
Thanks,
Olaf