Here is my code:

int LoadVerts(char *s_fName, OURCUSTOMVERTEX *p_Verts, short *p_Indices)
{
	long i_CountVerts;
	long i_CountInds;

	unsigned char buffer[sizeof(OURCUSTOMVERTEX)];

	basic_ifstream<unsigned char> f_DataFile;

	f_DataFile.open(s_fName, std::ios_base::in | std::ios_base::binary);

		f_DataFile.read(buffer, sizeof(long));
		memcpy(&i_CountVerts, buffer, sizeof(long));
		f_DataFile.read(buffer, sizeof(long));
		memcpy(&i_CountInds, buffer, sizeof(long));

		if ((p_Verts = (OURCUSTOMVERTEX *)realloc(p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts)) == NULL)
		  exit( 1 );
		if ((p_Indices = (short *)realloc(p_Indices, sizeof(short) * i_CountInds)) == NULL)
		  exit( 1 );

		for (int temp = 0; temp < i_CountVerts; temp++)
		{
			f_DataFile.read(buffer, sizeof(OURCUSTOMVERTEX));
			memcpy(&p_Verts[temp], buffer, sizeof(OURCUSTOMVERTEX));
		}

		for (int temp2 = 0; temp2 < i_CountInds; temp2++)
		{
			f_DataFile.read(buffer, sizeof(short));
			memcpy(&p_Indices[temp2], buffer, sizeof(short));
		}
	f_DataFile.close();

	return 0;
}

My issue is coming in at the following lines:

if ((p_Verts = (OURCUSTOMVERTEX *)realloc(p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts)) == NULL)
		  exit( 1 );
		if ((p_Indices = (short *)realloc(p_Indices, sizeof(short) * i_CountInds)) == NULL)
		  exit( 1 );

This is the error I am getting after a successful build and the program is running:

Windows has triggered a breakpoint in saveverts.exe.

This may be due to a corruption of the heap, and indicates a bug in saveverts.exe or any of the DLLs it has loaded.

The output window may have more diagnostic information

After a bit of lengthy chasing.... i have turned up empty handed with my focus on a function in istream called "_Read_s", which I am confident is not the problem. SOOOO.... I am looking for Help, advice, or Insight.

SHORT DESC: This function should receive 2 arrays of different types. During the meat of the code it should load a file, read from the file the first two long values to get the lengths of each array. it should reset the arrays to the new lengths to allow for input of the values from the files. After the values are filled in the newly sized arrays, these new arrays and new values should be accessible from the calling procedure, in this case WinMain().

Thanks in advance.

The first question is why are you using something as clunky as realloc in a C++ program?

The second problem is you're using realloc in an unsafe manner.
You need to do this.

void *temp = realloc ( p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts );
if ( temp != NULL ) {
  // success, update the pointer
  p_Verts = temp;
} else {
  // p_Verts is still pointing at the OLD memory.  This needs to be saved
  // or free'd as appropriate
}

The third problem is you're calling the function by value, not by reference.

void func ( OURCUSTOMVERTEX *&p_Verts ) {
   // your code

  void *temp = realloc ( p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts );
  if ( temp != NULL ) {
    // success, update the pointer
    p_Verts = temp;
  } else {
    // p_Verts is still pointing at the OLD memory.  This needs to be saved
    // or free'd as appropriate
  }

  // your code
}

Thus, if the allocated memory really does move when realloc is called, the actual pointer to that memory (from the caller's point of view) will also change.

In your code, this change was NOT reflected in the caller, and it probably ended up using an invalid pointer as a result.

The first question is why are you using something as clunky as realloc in a C++ program?

You are obviously more versed than I, and i definitely thank you for your help. That being said what sort of alternative do you propose?

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.