A scenario, as per C++ specification supported and also supported by the GNU C++ compiler but not right to do so.
Example:
The following piece of code works both in the GNU C++ compiler as well as Microsoft C++ compiler. Code execution gives the right result (I mean expected output).
#include < vector >
#include < iostream >
class Simple
{
public:
Simple(unsigned int aInt) : memberInteger(aInt)
{
}
void Amaze()
{
myVector = { Simple(1), Simple(2) };
}
unsigned int memberInteger;
std::vector < Simple > myVector;
};
int main()
{
Simple s(0);
s.Amaze();
size_t sz = s.myVector.size();
return 0;
}
The size of my vector returns 2.
Now, I've changed the a code little bit and it gets compiled well in Gnu C++ compiler(4.9.2 and used "g++ -std=c++11 -Wall Simple_Defeat.cpp -o Simple_Defeat" to compile) but not in Microsoft C++ compiler (Checked with 2013):
#include < vector >
#include < iostream >
class Simple
{
public:
Simple (unsigned int aInt): memberInteger (aInt)
{
}
unsigned int memberInteger;
std::vector < Simple > myVector = { Simple(1), Simple(2) };
};
int main()
{
Simple s(0);
std::cout << s.myVector.size() << std::endl;
return 0;
}
However, on execution, this one gives a core dump due to results in SIGSEGV. I think this is an edge case scenario in C++ (11) where specification allows initializing vector through initialization list but which results in code like below in Gnu C++:
Simple(unsigned int aInt) : memberInteger(aInt)
{
myVector = { Simple(1), Simple(2) };
}
As a result, it's an indefinite recursion causes a stack overflow. I came to this conclusion after examining the call stack which shows ~26,000 calls invocation to the ctor of Simple.
Happy programming!
Example:
The following piece of code works both in the GNU C++ compiler as well as Microsoft C++ compiler. Code execution gives the right result (I mean expected output).
#include
#include < iostream >
class Simple
{
public:
Simple(unsigned int aInt) : memberInteger(aInt)
{
}
void Amaze()
{
myVector = { Simple(1), Simple(2) };
}
unsigned int memberInteger;
std::vector
};
int main()
{
Simple s(0);
s.Amaze();
size_t sz = s.myVector.size();
return 0;
}
The size of my vector returns 2.
Now, I've changed the a code little bit and it gets compiled well in Gnu C++ compiler(4.9.2 and used "g++ -std=c++11 -Wall Simple_Defeat.cpp -o Simple_Defeat" to compile) but not in Microsoft C++ compiler (Checked with 2013):
#include < vector >
#include < iostream >
class Simple
{
public:
Simple (unsigned int aInt): memberInteger (aInt)
{
}
unsigned int memberInteger;
std::vector < Simple >
};
int main()
{
Simple s(0);
std::cout << s.myVector.size() << std::endl;
return 0;
}
However, on execution, this one gives a core dump due to results in SIGSEGV. I think this is an edge case scenario in C++ (11) where specification allows initializing vector through initialization list but which results in code like below in Gnu C++:
Simple(unsigned int aInt) : memberInteger(aInt)
{
myVector = { Simple(1), Simple(2) };
}
As a result, it's an indefinite recursion causes a stack overflow. I came to this conclusion after examining the call stack which shows ~26,000 calls invocation to the ctor of Simple.
Happy programming!
Comments