Boost logo

Boost Users :

From: Olaf Peter (ope-devel_at_[hidden])
Date: 2008-06-26 14:14:55


the following code crashs:

#include <boost/cstdint.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/algorithm.hpp>

#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?


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at