Unhandled exception? I don't realize what the problem is in this code-piece...

Started by
7 comments, last by CPPhoenix 22 years, 9 months ago
First of all I have three variable declarations: BITMAP * data[512]; char filename[512][32]; int count; So, the code piece:

BITMAP* bmplst::operator +=(char* f)
{	
	if (f == NULL) 
		return NULL;
	int n = 0;
	bool cont = true;

	do
	{	
		if (data[n] == NULL || filename[n][0] == '\0' || n == count)
			cont = false;
		else if (stricmp(f,filename[n]) == 0) 
			return data[n];
		n++;
	} while(cont);

	data[count] = load_bitmap(f,pal);
	strcpy(filename[count],f);
	count++;

	return data[count-1];
}
   
When I run this I get an unhandled exception in 0xC0000005, according to MSVC this line is casuing it: if (data[n] == NULL || filename[n][0] == '\0' || n == count) I really don't see what's wrong, can you help me? Edited by - CPPhoenix on July 2, 2001 10:23:05 AM
Advertisement
You should make n==count be your first condition. If count is the data''s size then on the last pass through, it would crash when it compared data[n]==NULL because it would be out of the data''s bounds. If this doesn''t do the trick, let us know what count is supposed to represent.

Good luck,
Eck

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

I tried with this first:
if (n == count || data[n] == NULL || filename[n][0] == ''\0'')
Same error...
if (n == count || filename[n][0] == ''\0'' || data[n] == NULL)

Then I got a different run-time error... It''s some acces violation(it was that before too):
49:       do50:       {51:           if (n == count || filename[n][0] == ''\0'' || data[n] == NULL)00401B25     mov         eax,dword ptr [ebp-4]00401B28     mov         ecx,dword ptr [ebp-8]->00401B2B   cmp         ecx,dword ptr [eax+4C00h] 


If the error happens when the counter advances from 511 to 512, then it isn't an overflow error, it's an "out of bounds" error..
try doing the sentence in this order:

if (n != count)
{
if (data[n] == NULL || filename[n][0] == '\0') cont = false;
}
else
cont = false;

That's should solve your problem. Be sure that count = 511, not 512.
(Sorry for my English..)



Edited by - IaM on July 2, 2001 11:47:39 AM
Sorry, it wasn''t an out-of-bound error...
Anyways, if I do what you just did I get the error at:
if (n != count)

AHHHH... I''m confused
Ok, I am confusing...
Right now the function looks like this:

class bmplst
{ BITMAP * data[512];
char filename[512][32];

PALETTE pal;
public:
int count;

bmplst();
~bmplst();
BITMAP* operator+=(char * f);
BITMAP* operator[](int x);
BITMAP* operator[](char * f);
void setpal();
};

BITMAP* bmplst::operator +=(char* f)
{
if (f == NULL)
return NULL;
int n = 0;
bool cont = true;

do
{
if (n != count)
{
if (data[n] == NULL || filename[n][0] == ''\0'')
cont = false;
}
else if (stricmp(f,filename[n]) == 0)
return data[n];

n++;
} while(cont && n != count);

data[count] = load_bitmap(f,pal);
strcpy(filename[count],f);
count++;

return data[count-1];
}
<br><br>And I get a run-time error at:<br>if (n != count)<br><br> </pre>
quote:Original post by CPPhoenix

BITMAP* bmplst::operator +=(char* f)
{
if (f == NULL)
return NULL;
int n = 0;
bool cont = true;

do
{
if (n != count)
{
if (data[n] == NULL || filename[n][0] == '\0')
cont = false;
}
else if (stricmp(f,filename[n]) == 0)
return data[n];

n++;
} while(cont && n != count);

data[count] = load_bitmap(f,pal);
strcpy(filename[count],f);
count++;

return data[count-1];
}
<br> </pre> <hr height=1 noshade></SPAN></BLOCKQUOTE> <br><br>Well.. in the line..<br>if (n != count)<br>{<br> //bla, bla..<br>}<br>else<br>if <br> if (stricmp(f,filename[n]) == 0) return data[n];<br><br>The substring filename[n], when n == count, it's an invalid memory position.. the check should be inside the if.. something like this:<br><br><br>do<br>{ <br> if (n != count)<br> {<br> if (data[n] == NULL || filename[n][0] == '\0') <br> cont = false;<br> else<br> if (stricmp(f,filename[n]) == 0) <br> return data[n];<br> }<br> n++;<br>} while(cont && n != count);<br><br>If it doesn't help, send me an e-mail (if you want/can) with the source code and the necesary files and i will try it..<br><br>Hope it helps.. bye<br><br><br>Edited by - IaM on July 2, 2001 1:23:36 PM
Krissa at #gamedev at EFNet helped me with the fact that data[512] didn''t have any allocated memory... That solved some of the problems...
But I still get an access violation at:
if(n != count)

Can non-members be compared to public members?
quote:Original post by CPPhoenix
Krissa at #gamedev at EFNet helped me with the fact that data[512] didn''t have any allocated memory... That solved some of the problems...
But I still get an access violation at:
if(n != count)

Can non-members be compared to public members?


Of course they can.. when you''re in a function that''s a member of a class, the member variables of that class are like global variables.
And.. yes.. BITMAP* data[512] was an array of pointers.. of course that you have to allocate memory =]

I still don''t see how that line could cause an access violation..
Try to see the assembler and give us the exact location of the error..

This topic is closed to new replies.

Advertisement