Program crashes when calling new operator (C++) -
i'm working way through tutorials found on creating ascii game engine in c , writing program in c++ practice. i'm working on stuff allocating image data on heap in form of image struct (containing int width, int height, , 2 char pointers locations on heap holding arrays of chars [width * height] in size)... however, i'm having problems calling new operator. function i'm allocating memory struct itself, character , colour data, looks this:
image *allocateimage(int width, int height) { image *image; image = new image; if (image == null) return null; image->width = width; image->height = height; image->chars = new char[width * height]; image->colours = new col[width * height]; //image->colours = (char*) ptradd(image->chars, sizeof(char) + width * height); (int = 0; < width * height; ++i) { //initializes transparent image *(&image->chars + i) = 0; *(&image->colours + i) = 0; } return image; }
the main function (where function called twice) looks this:
int main() { int x, y, offsetx, offsety; dword i; srand(time(0)); bool write = false; input_record *eventbuffer; colorref palette[16] = { 0x00000000, 0x00800000, 0x00008000, 0x00808000, 0x00000080, 0x00800080, 0x00008080, 0x00c0c0c0, 0x00808080, 0x00ff0000, 0x0000ff00, 0x00ffff00, 0x000000ff, 0x00ff00ff, 0x0000ffff, 0x00ffffff }; coord buffersize = {width, height}; dword num_events_read = 0; small_rect windowsize = {0, 0, width - 1, height - 1}; coord characterbuffersize = {width, height}; coord characterposition = {0, 0}; small_rect consolewritearea = {0, 0, width - 1, height - 1}; whnd = getstdhandle(std_output_handle); rhnd = getstdhandle(std_input_handle); setconsoletitle("title!"); setconsolepalette(palette, 8, 8, l"sunkure font"); setconsolescreenbuffersize(whnd, buffersize); setconsolewindowinfo(whnd, true, &windowsize); (y = 0; y < height; ++y) { (x = 0; x < width; ++x) { consolebuffer[x + width * y].char.asciichar = (unsigned char)219; consolebuffer[x + width * y].attributes = foreground_blue; } } write = true; image *sun_image = allocateimage(sunw, sunh); image *cloud_image = allocateimage(cloudw, cloudh); setimage(sun_image, sun.chars, sun.colors); setimage(cloud_image, cloud.chars, cloud.colours);
i can post more code if feels it's necessary, program reaches point - in fact, little before, crashes on second call allocateimage, @ point in function new operator called. program has been working fine until point - recent additions have been functions allocation of image data on heap (for creation of images variable sizes) deallocation (which isn't reached program). since program i'm learning written in c 1 place looking @ source code won't me, , google's been not either. can point me what's going wrong?
these lines
*(&image->chars + i) = 0; *(&image->colours + i) = 0;
are dubious because image
pointer. pointer pointer doesn't make sense here. remove &
.
since actual code writes joe random address can happen. not unusual thwart memory subsystem , hence next new
call.
Comments
Post a Comment