Difference in behaviour( gcc and MSVC++ )
- by Prasoon Saurav
Consider the following code.
#include <stdio.h>
#include <vector>
#include <iostream>
struct XYZ { int X,Y,Z; };
std::vector<XYZ> A;
int rec(int idx)
{
int i = A.size();
A.push_back(XYZ());
if (idx >= 5)
return i;
A[i].X = rec(idx+1);
return i;
}
int main(){
A.clear();
rec(0);
puts("FINISH!");
}
I couldn't figure out the reason why the code gives segmentation fault on Linux(IDE used: Code::Blocks) whereas on Windows(IDE used : MSVC++) it doesn't.
When I used valgrind just to check what actually the problem was, I got this output.
I got Invalid write of size 4 at four different places. Then why didn't the code crash when I used MSVC++?
Am I missing something?