c++ new() and for loops

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
Old 03 March 2007   #1
c++ new() and for loops

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:

	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:
Quote: 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?
Old 03 March 2007   #2
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.
Practice safe design, always use a concept.
Old 03 March 2007   #3
Have you tried to make a minimal main that run this code alone?
Old 03 March 2007   #4
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];
	for( int i = 0; i < 3; ++i )
Oddly enough, I ended up using Borland Turbo C++ for my A Level project... funny that.
SEVENTYMILLION.org: a project to find, map and connect Irish people worldwide.

Last edited by UrbanFuturistic : 03 March 2007 at 10:38 PM.
Old 03 March 2007   #5
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.
Old 03 March 2007   #6
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.
Old 03 March 2007   #7
Thread automatically closed

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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 02:32 AM.

Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.