I am writing to a file, using a memory buffer. I copy each record to the memory buffer and then flush it to the disk.
CODE:
char * OutBuffer;
char *pt;
OutBuffer = new char(BufferSize);
pt = OutBuffer;
for (int i=0; i<(FileSize / RECORD_SIZE); i++){
if (((i % recordsPerBlock)==0) && (i>0)){
FileSortHandle->write(OutBuffer, BufferSize);
pt = OutBuffer;
}
else{
memcpy(pt, minRecord, RECORD_SIZE);
pt = pt + RECORD_SIZE;
}
minRecord = nullptr;
}
When i call “FileSortHandle->write(OutBuffer, BufferSize);” the VS shows:
“Windows has triggered a breakpoint in STL_Test2.exe.
This may be due to a corruption of the heap, which indicates a bug in STL_Test2.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while STL_Test2.exe has focus.
The output window may have more diagnostic information.”
Does anyone can help me?
This allocates memory for just ONE char, and this one char is initialized with
BufferSizewhich may cause overflow ifBufferSizeis too big forchar.What you probably meant is this:
It allocates memory for
BufferSizechars. The buffer is uninitialized.It is not related to your problem, but if possible, prefer allocating the memory when you declare the pointer:
That is, prefer initialization over assignment.