免费异常c
我在函数的最后一行遇到异常,该异常与 free 有关。这里是: Windows 在 HW1.exe 中触发了断点。
这可能是由于堆损坏造成的,这表明 HW1.exe 或其加载的任何 DLL 中存在错误。
这也可能是由于用户在 HW1.exe 获得焦点时按了 F12。
输出窗口可能有更多诊断信息。
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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在修改
text
(请参阅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 ...
text
指向您使用malloc()
分配的内存,但随后您执行了text++
,因此它不再分配。当您将新值传递给free()
时,这是一个错误,因为文本不再指向内存。text
points to memory that you allocate withmalloc()
but then you dotext++
so that it no longer does. When you pass the new value tofree()
, that's an error because text no longer points to the memory.尝试,然后在中间更改
text
并不介意。在开头
和
结尾
try
at the beginning
and
at the end, then changing
text
in the middle does not mind.