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

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.