Need help with a programming bug that I can't work out

Started by
5 comments, last by Gestalthalcyon 20 years, 9 months ago
I''m making a program in MS VC++ 6.0 but for some reason it keeps having a runtime access violation error. I tracked it down, sort of. A call to new was always returning NULL the second time the code executed. I''ve included some psuedo code to show the situation. Does anyone know why this is doing this? And more importantly, how to fix it?

byte* firstptr=new byte[aValue];
byte* secondptr=NULL;

while(Something_that_will_loop_a_few_times)
{
	
	secondptr=new byte[diffValue]; /* <- This always returns NULL the second time it loops*/
	
	//various opperations

	
	delete[] firstptr;
	firstptr=secondptr;
	secondptr=NULL;
	diffValue=someComputedValue;
	
	//other opperations

	
	Something_that_will_loop_a_few_times--;
}
§LateR§
Advertisement
I suspect that the error is on the diffValue=someComputedValue line. Check what someComputedValue is equal to.
Unfortunatly that isn''t it. When diffValue gets fed back into new byte[diffValue] , it is equal to 16,900. That shouldn''t be a problem.

Anyone else have any ideas?

How about a reason that new would return NULL in anycase?
Out of memory - Not an issue with 512megs
Feed 0, or some invalid number into it - Just checked it, not that
Other than that, I can''t think of any reasons that it should return NULL.

§LateR§
I have seen this before. The new returns NULL because you may have mangled the memory manager. Have you tried removing the delete[] firstptr or changing it to just delete firstptr?
Unless you got some specific reason NOT to use STL i''d suggest you do:

http://www.sgi.com/tech/stl/List.html <--- doubly linked list.
http://www.sgi.com/tech/stl/Slist.html <--- singly linked list.
http://www.sgi.com/tech/stl/Vector.html <--- Vector ( a smart Array replacemnent... +several other nifty things)

All the above links are to sgi''s STL implementation - but they *should* match those that comes with you compiler ( since STL is now part of the C++ standart )


And *AFIK* the ONLY reason that new would return NULL is due too memory exhaust. So my guess is this :

your program have memory leaks, and these have managed to fill up your 512mb of ram. Try to reboot and see If that doesn''t work ( untill ofcourse your leaks start building up again )
As for tracking down the leak your could try to overload the new & delete operators ( there are tons of articles about this on the net )



/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''''t use it...
/Please excuse my bad spelling - My native language is binary not english|Visit meTake my advice - I don''t use it...
quote:Original post by Gestalthalcyon
//various opperations

[...]
//other opperations

At a guess, one of those `various'' operations is trashing memory that doesn''t belong to your program.
I finally figured it out. You were right, it was messing with memory that wasn''t my program''s. Thanks for the help.

I just got started with Dev Studios and I have a few questions to ask (I was using borland). First off, how do I indent a whole block of code (in Borland you just hilight it and press ctrl+shift+i)? Second, why can''t I do this?:
for(int i=0; i<x; i++){}//...for(int i=0; i<x; i++){}


In borland this was not a problem, but with Dev Studios its giving a multiple declaration error.

I figure I''ll just ask these things here instead of starting a new thread because it shouldn''t need a whole thread.

Anyways, thanks for your help.

This topic is closed to new replies.

Advertisement