CGTalk > Technical > Graphics Programming
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 03-24-2007, 11:08 PM   #1
-Vormav-
somewhat frequent poster
 
-Vormav-'s Avatar
portfolio
Stephen Gustafson
AAU Student
San Francisco, USA
 
Join Date: Jul 2004
Posts: 2,287
Send a message via AIM to -Vormav- Send a message via MSN to -Vormav-
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:

Code:
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?
 
Old 03-25-2007, 01:16 AM   #2
Ian Jones
Design & Technology
 
Ian Jones's Avatar
Ian Jones
Multimedia Technologist
Brisbane, Australia
 
Join Date: Aug 2002
Posts: 2,219
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-25-2007, 10:45 AM   #3
yakul
Frequenter
 
Join Date: Sep 2004
Posts: 100
Have you tried to make a minimal main that run this code alone?
 
Old 03-25-2007, 08:12 PM   #4
UrbanFuturistic
Where's my jetpack?
 
UrbanFuturistic's Avatar
portfolio
Paul Duffy
Freelance Psychiatrist
Sheffield, United Kingdom
 
Join Date: Apr 2002
Posts: 1,956
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.
Code:
int allocBufTex(void) { cout << "Allocating space for buffer" << endl; return 0; }
Code:
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-25-2007 at 10:38 PM.
 
Old 03-26-2007, 07:54 AM   #5
CapBBeard
New Member
Brad
Timaru, New Zealand
 
Join Date: Feb 2007
Posts: 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-28-2007, 10:37 AM   #6
-Vormav-
somewhat frequent poster
 
-Vormav-'s Avatar
portfolio
Stephen Gustafson
AAU Student
San Francisco, USA
 
Join Date: Jul 2004
Posts: 2,287
Send a message via AIM to -Vormav- Send a message via MSN to -Vormav-
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-28-2007, 10:37 AM   #7
CGTalk Moderation
Expert
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,478
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 08:12 PM.


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