PDA

View Full Version : c++ new() and for loops


-Vormav-
03-25-2007, 12:08 AM
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:

Ian Jones
03-25-2007, 02:16 AM
Looks like your doing everything right, I don't usually do ++i in a for loop usually i++ but I doubt tht has any bearing on the problem. You are using new() correctly from what I understand aswell. I'll do a test later and see if I can get the same problems, otherwise this is indeed a very strange one.

yakul
03-25-2007, 11:45 AM
Have you tried to make a minimal main that run this code alone?

UrbanFuturistic
03-25-2007, 09:12 PM
This reminds me of an old Visual C++ 4.2 bug; I suspect it's a flaw in the compiler optimisation code.

Try separating the cout into a seperate function, not inline, and see how it runs eg.int allocBufTex(void)
{
cout << "Allocating space for buffer" << endl;
return 0;
} float* buffer[3];
allocBufTex();
for( int i = 0; i < 3; ++i )Oddly enough, I ended up using Borland Turbo C++ for my A Level project... funny that.

CapBBeard
03-26-2007, 08:54 AM
Yeah I've often found odd problems like that when I crank the optimisations right up on my compiler. (This is using VC++ 2003).

Have you tried turning them all off and doing a standard build without them? I seem to remember /GL + /G7 or something caused a couple of issues at one point. That's VC specific though. I have heard of similar things cropping up with GCC too though, with things like frame pointer omission optimisations.

-Vormav-
03-28-2007, 11:37 AM
Ugh. I guess it was the optimizer. I was compiling with that option the entire time. First time I've had issues with that, though. :shrug:

CGTalk Moderation
03-28-2007, 11:37 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.