Hey there! First off let me say that this is for an assignment and i would of rather used a singular array. Also i'm pretty sure that the TA wrote this assignment cause some stuff is ass backwards to create the illusion of the arrays starting in the bottom left instead of the top left. Anyways I have two functions. The first one reads in a text file and stores some data in a dynamic 2 dimensional array. The other function frees or deletes the arrays. I was under the impression that my code was correct but i get a corrupted heap error at run time.

int ImportMapDataFromFile(char *FileName)
{
   FILE *infile = fopen(FileName,"rt");
   
   if (infile == NULL)
     return 0;

   fscanf(infile, "Width %d\n", &BINARY_MAP_WIDTH);
   fscanf(infile, "Height %d\n", &BINARY_MAP_HEIGHT);

   MapData = new int *[BINARY_MAP_WIDTH];
   BinaryCollisionArray = new int *[BINARY_MAP_WIDTH];
   
   for (int i = 0; i < BINARY_MAP_WIDTH; i++)
   {
	   MapData[i] = new int[BINARY_MAP_HEIGHT];
	   BinaryCollisionArray[i] = new int[BINARY_MAP_HEIGHT];
   }

   int x = 0;
   int y = BINARY_MAP_HEIGHT - 1;

   while (!feof(infile))
   {
     char digit = fgetc(infile);
	 if (digit == ' ')
     {
		digit = fgetc(infile);
	 }
	 if (digit == '\n')
	 {
		 y--;
		 x = 0;
	 }
	 else
     {
	     MapData[x][y] = atoi(&digit);
		 BinaryCollisionArray[x][y] = atoi(&digit);
		 x++;
	 }
   }
   fclose(infile);
   return 1;
}


void FreeMapData(void)
{
	for (int i = 0; i < BINARY_MAP_WIDTH; i++)
	{
		delete [] BinaryCollisionArray[i];
		delete [] MapData[i];
	}
    delete [] BinaryCollisionArray;
	delete [] MapData;
}

Dani AI

Generated

The crash was almost certainly caused by out-of-bounds writes in the parsing loop (heap metadata gets stomped, then delete[] fails). 's suggestion to check x/y ranges exposed that problem; the parsing logic was allowing invalid writes. Two common culprits in this pattern are using atoi(&digit) on a single char (undefined behavior — atoi expects a NUL-terminated string) and looping with while(!feof(...)) / not checking fgetc for EOF. Extra characters such as '\r' from CRLF files or stray whitespace can also move x/y out of sync and produce overrun.

A safer, small C-style approach (illustrative) — read fgetc() into an int, check for EOF, ignore '\r', handle '\n', test isdigit() and convert using c - '0', and always guard writes with bounds checks:

int c;
int x = 0;
int y = height - 1;
while ((c = fgetc(infile)) != EOF) {
    if (c == '\r') continue;
    if (c == '\n') { y--; x = 0; continue; }
    if (!isdigit(c)) continue;
    if (x < 0 || x >= width || y < 0 || y >= height) { /* handle error */ break; }
    int val = c - '0';
    map[x][y] = val;
    collision[x][y] = val;
    x++;
}

Additional practical improvements: validate fscanf return values when reading width/height; prefer std::vector or a single contiguous 1D buffer (less chance of fragmented-heap corruption); initialize allocated memory; after freeing set pointers to nullptr and sizes to 0 to avoid double-delete; and prefer C++ stream parsing (std::getline + std::istringstream) for clearer token handling. These measures both prevent heap corruption and make the code easier to debug later.

Recommended Answers

All 3 Replies

sorry i dont know why the brackets went all over the place

How about inserting code before line 37 to verify that x and y are in range?

haha thank you sir! that was it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.