Visual Studio上的C ++错误:“Windows触发了断点” [英] C++ error on Ms Visual Studio: “Windows has triggered a breakpoint"
问题描述
大家好,
我想在目录中提取所有3克(每个3克包含3个字节,每次移位1个字节)的文件文件中每3克的频率。我编写了一个简单的C ++程序,以递归方式提取3克二进制文件,并将它们作为密钥保存在哈希表中。
我添加每个键之前我找到了键。如果它在堆中我没有添加这个键,只是增加成员值(存在频率3gram)。
程序运行但是它停止并显示错误消息windows已经在我的程序中触发了一个断点。这可能是由于堆的损坏导致程序中的错误或它加载的dll
如果有人能帮助我,我将不胜感激..
谢谢,
<前lang = c ++> #include hash_table.h
#include < 字符串 >
#include < windows.h >
#include < fstream >
#include < stdio.h >
#include < iostream >
#define MAX_BUFFER_SIZE 256
typedef CHashTable< int> CLongHashT;
使用 命名空间标准;
void makeVocabHash(string dir,CLongHashT HashTperAll, int N){
/ * N - > N元! * /
HANDLE hFindFile;
WIN32_FIND_DATAA Win32FindData;
CHAR目录[MAX_PATH];
int counter;
int countNgram;
int i;
string tmp;
fstream fileOpen;
// 复制目录路径
sprintf(目录, %s \\ *。*,& dir [ 0 跨度>]);
if ((hFindFile = FindFirstFileA(Directory,& Win32FindData))== INVALID_HANDLE_VALUE){ // 如果找不到目录(查找目录的第一个文件)
return ; // 错误,找不到目录
}
do {
if (strcmp(Win32FindData.cFileName, 。)!= 0 && strcmp(Win32FindData.cFileName,< span class =code-string> ..)!= 0 ) {
sprintf(目录, %s \\%s,& dir [ 0 ],Win32FindData.cFileName);
// 如果找到文件
if (!(Win32FindData.dwFileAttributes& FILE_ATTRIBUTE_DIRECTORY)){
// 是一个文件
fileOpen.open(目录,ios :: 在 | ios :: binary | ios中: :吃);
// 文件大小
int end = fileOpen.tellg();
fileOpen.seekg( 0 ,ios :: beg);
int begin = fileOpen.tellg();
int size = end - begin;
char 数据[MAX_BUFFER_SIZE];
fileOpen.read(& data [ 0 ],size);
fileOpen.close();
counter = 0 ;
// 读取N个字节的数据,构造3克并插入hashT
while ((counter!=((size-N)+ 1 ))){
for (i = 0 ; i!= N; ++ i)
tmp + = data [i + counter];
// insert to hashT
if (HashTperAll.GetMember(tmp))
countNgram = *(HashTperAll.GetMember(tmp))+ 1 ;
else
countNgram = 1 ;
HashTperAll.AddKey(tmp,& countNgram);
tmp = ;
counter ++;
}
}
其他 {
// 是一个目录
makeVocabHash(Directory,HashTperAll,N);
}
}
} while (FindNextFileA(hFindFile,& Win32FindData)); // 在目录中查找下一个文件
// 关闭句柄
FindClose(hFindFile);
}
// --------------- -------------------------------------------------- ----
void main()
{
CLongHashT HashTDocs;
cout<< 输入路径;
string dir;
cin>> DIR;
makeVocabHash(dir,HashTDocs, 3 );
}
问题可能在这里:
char data [MAX_BUFFER_SIZE];
fileOpen.read(& data [ 0 ],尺寸);
当文件大小大于MAX_BUFFER_SIZE< br />时,这会导致缓冲区溢出。
为避免这种情况,请在堆使用new
或malloc
:
char data = new char [size];
fileOpen.read(& data [ 0 ],size);
// 在此处执行数据
删除数据;
Hi all,
I want to extract all 3 grams(each 3 gram contain 3 byte with 1 byte shift each time) of files in a directory and count frequency of each 3 gram in files. I have written a simple C++ program to extract 3 grams of binary files recursively and saved them in a hash table as a key.
before I add each key I find key. if it was in heap I did not add this key and just increase member value(frequency of presence 3gram).
The program runs but it stops with an error message saying "windows has triggered a break point in my program.This may be due to the corruption of the heap which indicated a bug in the program or the dlls that it loads"
I would appreciate it if somebody could help me ..
Thanks,
#include "hash_table.h"
#include <string>
#include <windows.h>
#include <fstream>
#include <stdio.h>
#include <iostream>
#define MAX_BUFFER_SIZE 256
typedef CHashTable<int> CLongHashT;
using namespace std;
void makeVocabHash(string dir, CLongHashT HashTperAll, int N) {
/* N -> N-gram! */
HANDLE hFindFile;
WIN32_FIND_DATAA Win32FindData;
CHAR Directory[MAX_PATH];
int counter;
int countNgram;
int i;
string tmp;
fstream fileOpen;
// copying path to directory
sprintf(Directory,"%s\\*.*", &dir[0]);
if((hFindFile = FindFirstFileA(Directory, &Win32FindData)) == INVALID_HANDLE_VALUE){ // if directory not found (finding first file of directory)
return ; // error, directory not found
}
do{
if(strcmp(Win32FindData.cFileName, ".") != 0 && strcmp(Win32FindData.cFileName, "..") != 0){
sprintf(Directory, "%s\\%s", &dir[0], Win32FindData.cFileName);
// if found a file
if(! (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) {
// is a file
fileOpen.open(Directory, ios::in | ios::binary | ios::ate);
//size of file
int end = fileOpen.tellg();
fileOpen.seekg (0, ios::beg);
int begin = fileOpen.tellg();
int size = end - begin;
char data[MAX_BUFFER_SIZE];
fileOpen.read(&data[0], size);
fileOpen.close();
counter = 0;
// reading data with N bytes, construct 3 grams and insert to hashT
while( (counter != ((size - N) + 1) ) ) {
for( i=0; i!=N; ++i)
tmp += data[i+counter];
// insert to hashT
if (HashTperAll.GetMember(tmp))
countNgram = *(HashTperAll.GetMember(tmp)) + 1;
else
countNgram = 1;
HashTperAll.AddKey(tmp, &countNgram );
tmp = "";
counter++;
}
}
else {
// is a directory
makeVocabHash(Directory, HashTperAll, N);
}
}
} while(FindNextFileA(hFindFile,&Win32FindData));//finding next file in directory
// closing handles
FindClose(hFindFile);
}
//---------------------------------------------------------------------
void main()
{
CLongHashT HashTDocs;
cout<< "enter a path";
string dir;
cin>> dir;
makeVocabHash(dir,HashTDocs, 3);
}
The problem may be located here:
char data[MAX_BUFFER_SIZE]; fileOpen.read(&data[0], size);
This results in a buffer overflow when your file size is greater thanMAX_BUFFER_SIZE<br />.
To avoid this, allocate the buffer on the heap usingnew
ormalloc
:
char data = new char[size]; fileOpen.read(&data[0], size); // Do something with data here delete data;
这篇关于Visual Studio上的C ++错误:“Windows触发了断点”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!