Note that there are some explanatory texts on larger screens.

plurals
  1. POProgram crashes when calling new operator (C++)
    primarykey
    data
    text
    <p>I'm working my way through some tutorials I found on creating an ASCII game engine in C and writing my program in C++ to practice. I'm currently working on some stuff with allocating image data on the heap in the form of an Image struct (containing an int width, int height, and two char pointers to locations on the heap holding arrays of chars [width * height] in size)... however, I'm having some problems calling the new operator. The function where I'm allocating the memory for the struct itself, as well as its character and colour data, looks like this:</p> <pre><code>Image *allocateImage(int width, int height) { Image *image; image = new Image; if (image == NULL) return NULL; image-&gt;width = width; image-&gt;height = height; image-&gt;chars = new CHAR[width * height]; image-&gt;colours = new COL[width * height]; //image-&gt;colours = (CHAR*) PtrAdd(image-&gt;chars, sizeof(CHAR) + width * height); for (int i = 0; i &lt; width * height; ++i) { //initializes transparent image *(&amp;image-&gt;chars + i) = 0; *(&amp;image-&gt;colours + i) = 0; } return image; } </code></pre> <p>The main function itself (where this function is called twice) looks like this:</p> <pre><code>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, &amp;windowSize); for (y = 0; y &lt; HEIGHT; ++y) { for (x = 0; x &lt; 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); </code></pre> <p>I can post more code if anyone feels it's necessary, but the program only reaches this point - in fact, a little before, as it crashes on the second call to allocateImage, at the point in the function where the new operator is called. The program has been working just fine until this point - the only recent additions have been the functions for allocation of image data on the heap (for creation of images with variable sizes) as well as deallocation (which isn't reached by this program). Since the program I'm learning from is written in C this is one place where looking at the source code won't help me, and Google's been not much help either. Can anyone point me to what's going wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload