What is an "exception handler state"? - c++

Visual Studio's error C1509 says:
too many exception handler states in function
I'm curious as to exactly what an "exception handler state" is. It claims that the max number of states is: 215. However when I do not get this error when I run the file generated by this code, which has 215 + 1 try-catch blocks:
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ofstream foo{ "main.cpp" };
const auto primer = "\tfoo = value(g);\n";
const auto body = "\ttry{\n"
"\t\tcout << 13.0F / foo << endl;\n"
"\t}catch(...){\n"
"\t\tcout << \"error\\n\";\n"
"\t}\n";
const auto count = (1 << 15) + 1;
if(foo)
{
foo << "#include <iostream>\n"
"#include <random>\n"
"\n"
"using namespace std;\n"
"\nint main(){\n"
"\tmt19937 g{ random_device()() };\n"
"\tuniform_int_distribution<> value{ 0, 13 };\n"
"\tauto foo = value(g);\n"
"\n"
<< body;
for(auto i = 1; i < count; ++i)
{
foo << endl << primer << body;
}
foo << '}' << endl;
}
}
So what is an exception handler state?

Related

Problem compiling a function from Boost Library

I'm trying to use the function norm_2_vector from boost.
But I'm getting the error ‘norm_2_square’ was not declared in this scope.
To compile I used the command below, where testNormSquare is the name of the program:
g++ -o testNorm2Square testNorm2Square.cpp
Question: Is there anything I'm missing? What should I do to make the code compilable?
A toy example that is not working is given below.
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
int main () {
using namespace boost::numeric::ublas;
vector<double> v (3);
for (unsigned i = 0; i < v.size (); ++ i)
v(i) = i;
std::cout << 2.0 * v << std::endl;
std::cout << v * 2.0 << std::endl;
std::cout << norm_2_square(v);
}
The error message is the following:
testNorm2Square.cpp:12:18: error: ‘norm_2_square’ was not declared in this scope
Another problem happens if I specify explicitly that norm_2_square belongs to boost::numeric::ublas:
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
int main () {
using namespace boost::numeric::ublas;
vector<double> v (3);
for (unsigned i = 0; i < v.size (); ++ i)
v(i) = i;
std::cout << 2.0 * v << std::endl;
std::cout << v * 2.0 << std::endl;
std::cout << boost::numeric::ublas::norm_2_square(v);
}
In this case the error message is:
testNorm2SquareV2.cpp:12:41: error: ‘norm_2_square’ is not a member of ‘boost::numeric::ublas’

Count resources in C++ RAII style

I would like to have a C++11 RAII component to count how many resources of a certain type are present in a multithreaded environment. I wrote the following:
#include <atomic>
#include <iostream>
using namespace std;
class AtomicCounter
{
public:
AtomicCounter(std::atomic< int > &atomic) : atomic(atomic) {
int value = ++this->atomic;
cerr << "incremented to " << value << endl;
}
~AtomicCounter() {
int value = --this->atomic;
cerr << "decremented to " << value << endl;
}
private:
std::atomic< int > &atomic;
};
int main() {
atomic< int > var;
var = 0;
AtomicCounter a1(var);
{
AtomicCounter a2(var);
cerr << "Hello!" << endl;
AtomicCounter a3(var);
}
cerr << "Good bye!" << endl;
return 0;
}
The idea is that I create an AtomicCounter object in each resource (for example, the main thread function) and this keeps the associated atomic variable updated.
Is this component correct, even when used in a multithreaded environment? Is there a standard component that already does this?

Thread (Attempt to use a deleted function

I'm following an online tutorial on threading and got the error message "Semantic Issue: Attempt to use a deleted function". Any idea what's wrong?
#include <iostream>
#include <thread>
#include <string>
using namespace std;
class Fctor {
public:
void operator() (string & msg) {
cout << "t1 says: " << msg << endl;
msg = "msg updated";
}
};
int main(int argc, const char * argv[]) {
string s = "testing string " ;
thread t1( (Fctor()), s);
t1.join();
return 0;
}
Well, the code works with VS2015, MS-Compiler, with these change to the code:
This
void operator() (string & msg) {
cout << "t1 says: " << msg << endl;
msg = "msg updated";
}
to
void operator() (std::string& msg) {
std::cout << "t1 says: " << msg.c_str() << std::endl;
msg = "msg updated";
}
and this
string s = "testing string " ;
thread t1( (Fctor()), s);
to
std::string s = "testing string ";
Fctor f;
std::thread t1(f, s);
The two main things I changed was msg.c_str(), because the stream doesn't take string, but const char*.
Second, I turned the RValue Fctor() into the LValue Fctor f and gave f as the parameter, the thread apparently doesn't take RValues.

Putting a value to ostream

I have this code below which parses a for statement, but I am not sure how to put any value into the ostream when calling the method write(...). What can I do? (e.g write("for (........."))
#include <ostream>
#include <iostream>
using namespace std;
//I cut out the declaration bit here
typedef const string type;
private:
type *initializer;
type *condition;
type *increment;
type *body;
public:
void write(ostream& stream) const {
stream
<< "for ("
<< *initializer << "; "
<< *condition << "; "
<< *increment << ")\n{\n"
<< *body
<< "}";
}
I guess you try to learn using ostream as an input in a function. But it seems that you mixing things that how to use classs and methods.
Maybe this is no avail but i can give you a little snippet to give you some opinion.
#include <iostream>
#include <string>
using namespace std;
typedef const string type;
type *init;
type *cond;
type *incr;
type *body;
void write(ostream& stream) {
stream
<< "for ("
<< *init << "; "
<< *cond << "; "
<< *incr << ")\n{\n"
<< *body
<< "\n}";
}
int main(int argc, char* argv[])
{
const string ini = "int i = 0";
const string con = "i < 10";
const string inc = "i++";
const string bod = "cout << i << endl;";
init = &ini;
cond = &con;
incr = &inc;
body = &bod;
write(cout);
return 0;
}
Try this code, examine and read more for more details.

c++ Passing a value the wrong way?

When i am passing an object to a function, I am getting undesired results. It seems to happen when I pass a Character through a Mage's action() function.
Here are some snippits of my code:
character.h
class Character {
public:
Character();
int getMaxLives() const;
int getMaxCraft() const;
protected:
maxLives;
maxCraft;
};
character.cpp
#include "character.h"
Character::Character () {
maxLives = 5;
MaxCraft = 10;
}
int Character::getMaxLives() const {
return maxLives;
}
int Character::getMaxCraft() const {
return maxCraft;
}
mage.h
#include "character.h"
class Mage {
public:
Mage();
void action(Character c1);
};
mage.cpp
#include "mage.h"
Mage::Mage () { ... }
void Mage::action(Character c1) {
cout << "Max Craft: " << c1.getMaxCraft() << endl;
cout << "Max Lives: " << c1.getMaxLives() << endl;
}
driver.cpp
int main () {
Character c1;
Mage m1;
m1.action(c1);
My ouput gives me the following:
Max Craft: 728798402 (The number varies)
Max Lives: 5
However, if in my diver, i do:
cout << "Max Craft: " << c1.getMaxCraft() << endl;
cout << "Max Lives: " << c1.getMaxLives() << endl;
I get:
Max Craft: 10
Max Lives: 5
Any ideas?
Looks like you meant for MaxCraft = 10; (in your default constructor) to actually be maxCraft = 10;. As #chris says in the comments, it appears that you're using some (evil, evil) C++ extension that allows implicitly-typed variables, so the MaxCraft = 10; line is simply defining a new variable named MaxCraft.