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 CLOSED
 
Thread Tools Search this Thread Display Modes
  03 March 2007
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:


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:
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?
 
  03 March 2007
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.
 
  03 March 2007
Have you tried to make a minimal main that run this code alone?
 
  03 March 2007
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.
__________________
SEVENTYMILLION.org: a project to find, map and connect Irish people worldwide.

Last edited by UrbanFuturistic : 03 March 2007 at 10:38 PM.
 
  03 March 2007
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.
 
  03 March 2007
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.
 
  03 March 2007
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
CGSociety
Society of Digital Artists
www.cgsociety.org

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

All times are GMT. The time now is 12:44 AM.


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