I’ve got this strange issue I’ve started to see recurring over and over again in an app I’ve been working on lately (with VS 2005). Take a segment of code like this:
try
{
float* buffer[3];
cout << "Allocating space for buffer" << endl;
for( int i = 0; i < 3; ++i )
{
buffer[i] = new float[BUFFER_SIZE];
}
}
catch( exception &error )
{
cout << "Exception: " << error.what() << endl;
return 0;
}
Given a small BUFFER_SIZE, this works perfectly. Memory is allocated and can be used without any problems. However, start increasing the buffer size, and things get strange: My cout() calls get thrown into the for loop. For instance, given the above example, after I set the buffer size to 100000, I got output like this:
Allocating space for buffer
Allocating space for buffer
Allocating space for buffer
As if the for loop can’t loop back to the correct point in the code, which is very weird.
Anyone ever encountered any problems like this? Am I just using new() incorrectly?
Like I already said, this works perfectly for smaller buffers. And if I move back to using malloc() instead, allocating the same amount of data, everything works just fine, so it shouldn’t be a memory issue (which shouldn’t matter anyway, since my catch() block should be called for bad_alloc cases). So, yeah… what the hell? :curious: