Skip to main content
gamedev.net
🔒 Locked

Debug Assertation Failed!

Started by MattS423 May 16, 2003 at 8:37 PM 7 replies 250+ views
Original Post
MattS423
MattS423
Ok, I wrote a program to see if its possible to read and write vectors from a file. it looks like it is, but there''s one problem... I''ve narrowed it down to this line of code:
ReadFile(hFile,&ReadVector,sizeof(ReadVector),&BytesRead,NULL);
 
Where ReadVector is declared:
vector ReadVector;
 
if I comment out the preceding line, everything works perfectly, but otherwise, I get this lovely dialog box that says: "Debug assertation failed" "Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse" Then there are 3 buttons at the bottom, Abort, retry, and ignore. I think it has something to do with the way i pass ReadFile() the vector... any ideas? Thanks, Matt
Programmers of the world, UNTIE!
antareus
antareus
You can''t just clobber ReadVector with what you''re reading in...nor should you just dump the raw contents to a file like that. Iterate through each one and write it to a file. Then when you read it in, read until eof and push_back until the end.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
MattS423
MattS423
read until eof?? i don''t quite understand...
Programmers of the world, UNTIE!
antareus
antareus
quote:
Original post by MattS423
read until eof?? i don''t quite understand...

End of file.

What I''m saying is don''t just try to throw the data structure on the disk as it''s laid out in memory, use some sort of formatting. That way it is much easier to read in.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
MattS423
MattS423
ahh...i get it...

so the file would look something like this??


int size;
int VectorData[size];
Programmers of the world, UNTIE!
Zipster
Zipster
I'm going to be blunt, you really shouldn't be using vector if you don't know what it is or how to work with it. It is a class object, and you can't just read memory into it like you're doing.

It is perfectly possible to read and write vector data, however since it really isn't just an array, you have to use the interface it provides to manipulate it.

Reading and writing a vector uses similar algorithms to what you might have learned when using regular arrays:

for(int i = 0; i < MyVector.size(); ++i)
{
fileStream << MyVector[i];
}


Notice that the member function .size() returns the size of the vector, and that you can access items with the [] operator, so it's very similar to an array.

Reading the vector is a different story, because the storage for your data isn't already available:

while(!fileStream.eof())
{
int i;

fileStream >> i;

MyVector.push_back(i);
}


Notice how we needed to create a temporary variable, and then add the value to the vector. Alternatively, you could use the .reserve() function to reserve the memory beforehand, but you'd have to know how much data you were reading in.

[edited by - Zipster on May 17, 2003 2:07:43 AM]
MattS423
MattS423
Yeah, ok, that makes sense...

but what if i have other data in the file besides the vector data?

I believe i could just write the size of the vector before the vector data...psuedocode:

Write MyVector.size() to file hFile

int index;
for(index=0;index{

Write MyVector[index] to file
}

then to read...

int Size;
Read the first sizeof(int) of hFile and store the data in size;

int index;
for(index=0;index{

MyVector.push_back(index);

}

Eh?

Programmers of the world, UNTIE!
Zipster
Zipster
As a matter of fact, if you read in how many vector items there are beforehand, you can use .reserve() to reserve the memory. In that case, you could either use MyVector[i] directly and avoid the temporary variable, or push_back if you keep the variable.

[edited by - Zipster on May 17, 2003 2:44:27 PM]
MattS423
MattS423
thats true...
Programmers of the world, UNTIE!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.