Archived

This topic is now archived and is closed to further replies.

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

This topic is 6009 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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: do
50: {
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]


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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];
}
[code]

And I get a run-time error at:
if (n != count)

Share this post


Link to post
Share on other sites
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];
}
[code]



Well.. in the line..
if (n != count)
{
//bla, bla..
}
else
if
if (stricmp(f,filename[n]) == 0) return data[n];

The substring filename[n], when n == count, it's an invalid memory position.. the check should be inside the if.. something like this:


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);

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..

Hope it helps.. bye


Edited by - IaM on July 2, 2001 1:23:36 PM

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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..

Share this post


Link to post
Share on other sites