I am getting an exception on the last line of the function, an exception that has to do with the free. Here it is:
Windows has triggered a breakpoint in HW1.exe.
This may be due to a corruption of the heap, which indicates a bug in HW1.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while HW1.exe has focus.
The output window may have more diagnostic information.
void unicode(HANDLE file, DWORD sizeOfFile, int N) {
if(sizeOfFile - 2 == 0)
return;
_TCHAR* text = (_TCHAR*)malloc(sizeOfFile);
DWORD numRead = 0;
BOOL read = ReadFile(file, text, sizeOfFile, &numRead, NULL);
assert(read && (sizeOfFile == numRead));
int i = 0;
int lineNum = 0;
int lineStart = 0;
text++;
sizeOfFile--;
while(i <= sizeOfFile / 2) {
if(i == sizeOfFile / 2 && lineNum < N)
printLineUnicode(text + lineStart, i - lineStart, lineNum++);
else if(text[i] == '\r') {
if(lineNum < N) {
printLineUnicode(text + lineStart, i - lineStart, lineNum++);
}
i ++;
lineStart = i + 1;
}
i ++;
}
i -= 2;
int lineEnd = i;
while(i >= 0) {
if(i == 0 && lineNum < N)
printLineUnicode(text, lineEnd - i + 1, lineNum++);
if(text[i] == '\n') {
if(lineNum < 2*N) {
printLineUnicode(text + i + 1, lineEnd - i, lineNum++);
lineEnd = i - 2;
i --;
}
}
i --;
}
free(text);
}
You are modifying
text(see the linetext++;), so at the end of the function the pointer will be different from the one returned by the allocation function. Bad.Save the pointer and use the saved one to free the memory …