Advertisement

trouble with memory allocation in VC6

Started by May 24, 2001 04:39 PM
2 comments, last by arsenius 23 years, 8 months ago
I''m trying to allocate a buffer of USHORTs of any size in visual c++ 6. My code seems to work, except when the buffer is >=256*256 (255*256 works). I''m testing to see if the buffer is allocated and whatnot, but when I allocate that size buffer, my program hangs. I haven''t gotten the error with any buffer smaller than 256*256 here is the relevant code

struct RAW
{
	int width;
	int height;
	USHORT *image;
};
int LoadRaw16(RAW &raw, char filename[])
{
	FILE *read;
	UCHAR r, g, b;
	USHORT *buffer = NULL;
	read = fopen(filename,"rb");
	raw.image = new USHORT[raw.height * raw.width*2];
        buffer = raw.image;
	if (buffer == NULL)
		return 0;
	USHORT i = 0;
	while (i < (raw.height*raw.width))
	{
		r = getc(read);
		g = getc(read);
		b = getc(read);
		buffer[i++] = RGB565(r,g,b);
	}
	fclose(read);
	return 1;
}
void KillRaw(RAW &raw)
{
	delete [] raw.image;
	raw.image = NULL;
}

RAW blah;
blah.height = 256;
blah.width = 256;
blah.image = NULL;
if (LoadRaw16(blah,"circles.raw") == 0)
	return 0;
KillRaw16(blah);
return 1;
 
actually, the last portion is contained within my main program, which i won''t post unless someone needs it (it shouldn''t be relevant, but it is long:D) I make the call to LoadRaw16() when i''m setting up everything else like DX, and the call to KillRaw16() when I''m shutting down (though it never gets called because the program errors out before it gets there) LoadRaw16() checks to see whether or not it successfully creates a buffer, and returns 0 if it isn''t, which should exit the program if not successful (if LoadRaw16()==0){return 0} but instead the program hangs, and DX never gets initialized. Any thoughts? Also, does anyone know the equivalent of ios::nocreate for fopen()? -arsenius ''after three days without programming, life becomes meaningless'' -The Tao of Programming
Are you sure it''s hanging on the new?


Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
Your problem would seem to be the loop.

  USHORT i = 0;while (i < (raw.height*raw.width)){    r = getc(read);    g = getc(read);    b = getc(read);    buffer[i++] = RGB565(r,g,b);}[source]''i'' is a 16 bit unsigned. the maximmum value it can store is 65535 (0xFFFF). Since 256*256 == 65536 (0x10000), it wraps around to zero. Try using a DWORD or LONG.  
Your problem would seem to be the loop.
  USHORT i = 0;while (i < (raw.height*raw.width)){    r = getc(read);    g = getc(read);    b = getc(read);    buffer[i++] = RGB565(r,g,b);}  

''i'' is a 16 bit unsigned int. the maximmum value it can store is 65535 (0xFFFF). Since 256*256 == 65536 (0x10000), it wraps around to zero. Try using a DWORD or LONG.

This topic is closed to new replies.

Advertisement