I summarized my problem to the following short program.
It causes SEGFAULT in -O3 mode only (-O2 works fine).
According to gdb
it happens at *f = 0
line.
#include <iostream>
void func1(int s, int t)
{
char* buffer = new char[s + t*sizeof(float)];
if (!buffer)
{
std::cout << "new failed
";
return;
}
float* f = (float*)(buffer + s);
for (int i = 0; i < t; ++i)
{
*f = 0;
//std::cout << i << std::endl; // if uncomment this line everything will work fine
++f;
}
delete [] buffer;
std::cout << "done
";
}
int main()
{
int s = 31, t = 12423138;
std::cout << s << " " << t << std::endl;
func1(s, t);
return 0;
}
Please let me know, what am I doing wrong?
See Question&Answers more detail:os