堆内存分配异常
Exception in Heap Memory Allocation
我正在使用Windows Sockets上传一些文件到FTP服务器。我正在搜索的文件有一个特定的关键字,并发送到服务器,如果文件的名称包含该关键字。代码如下:
void FTPUpload(const char* path, const char* name)
{
SOCKET s1 = ConnectFTP("111.111.111.111", 21);
Receive(s1);
Send(s1, "USER user", true);
Receive(s1);
Send(s1, "PASS password", true);
Receive(s1);
Send(s1,"PWD", true);
Receive(s1);
Send(s1,"TYPE I", true);
Receive(s1);
Send(s1, "PASV", true);
char szString[1000];
Receive(s1, szString);
char szIP[40];
char* start = strchr(szString, '(' );
char* end = strchr(szString, ')' );
int num = end - start;
char str[30] = {' '};
strncpy( str, start + 1, num - 1 );
char* token = strtok( str , "," );
strcpy(szIP, "");
strcat(szIP, token);
strcat(szIP, ".");
token = strtok( NULL, "," );
strcat(szIP, token);
strcat(szIP, ".");
token = strtok( NULL, "," );
strcat(szIP, token);
strcat(szIP, ".");
token = strtok( NULL, "," );
strcat(szIP, token);
token = strtok( NULL, "," );
int intA = atoi(token);
token = strtok( NULL, "," );
int intB = atoi(token);
int port = (intA * 256) + intB;
sprintf(buf, "IP %s, Port %d", szIP, port);
//connect to FTP Server Data Connection
SOCKET s2 = ConnectFTP(szIP, port);
string command = "STOR " + string(name);
Send(s1, &command[0], true);
Receive(s1);
//Send the found file
long Begin;
long End;
char * block;
ifstream myfile;
myfile.open(path, ios::in | ios::binary);
Begin = myfile.tellg();
myfile.seekg(0,ios::end);
End = myfile.tellg();
unsigned long size = End - Begin;
int Div = (int)size / 1024;
int Mod = (int)size % 1024;
for (int i=0; i<Div; i++)
{
block = new char[1024];
myfile.seekg(i*1024);
myfile.get(block,1024+1);
Send(s2,block, false);
}
if (Mod != 0)
{
block = new char[Div];
myfile.seekg(Div*1024);
myfile.get(block,Mod+1);
Send(s2,block, false);
}
closesocket(s2);
myfile.close();
Receive(s1);
Send(s1, "QUIT", true);
Receive(s1);
closesocket(s1);
}
void FindFile()
{
string keyword = "key";
string path = "C:\";
string ipath = path+"*";
WIN32_FIND_DATA file;
HANDLE search_handle = FindFirstFile(ipath.c_str(),&file);
if (search_handle != INVALID_HANDLE_VALUE)
{
do
{
string cpath = path + file.cFileName;
DWORD attr = GetFileAttributes(cpath.c_str());
if ((attr & (0x10)) != (0x10))
{
if ((cpath.find(keyword)) != (string::npos))
FTPUpload(cpath.c_str(), file.cFileName);
}
}
while(FindNextFile(search_handle,&file));
FindClose(search_handle);
}
}
当目录中有2个文件匹配关键字时,程序工作正常。在第三个文件上,我在dbgheap.c中得到了bad_alloc的异常,即我在从堆中分配和释放内存时遇到了麻烦。请帮助。
这是错误的
block = new char[1024];
myfile.seekg(i*1024);
myfile.get(block,1024+1);
块是1024字节,但是get读取1024个字符,然后附加一个' '字符。也许你想用read
而不是get
。
这是错误的
block = new char[Div];
myfile.seekg(Div*1024);
myfile.get(block,Mod+1);
块是Div字节,但你读Mod字节。
可能对这个问题没有帮助,但请尝试摆脱分配数组的习惯。使用std::string
和std::vector
是更安全、更容易的选择。例如
myfile.seekg(i*1024);
vector<char> block(1024);
myfile.read(&block[0],block.size());
相关文章:
- std::元组分配和复制/移动异常保证
- 从 C# 调用 C++ DLib 会导致错误的分配异常
- 我们应该在抛出异常之前取消分配内存吗
- C++中的内存分配(引发异常:读取访问冲突)
- 如何捕获源自静态分配对象的构造函数的异常?
- 如何实现,错误分配中止而不是抛出异常
- 当有大量内存分配时,如何处理C++编程中的异常
- 从 boost::存档:binary_iarchive 反序列化 std::string 时出现错误的分配异常
- 分配数组时有异常,将其分配为 2D 数组时没有异常
- Qt:如何在QByteArray中调试错误的分配异常
- 如果在构造函数引发异常后释放了分配的内存
- 分配内存后,构造函数处出现未捕获的异常
- 由将值分配给双精度类型的变量而导致的浮点异常
- 在分配scoped_ptr之前处理异常
- C++坏内存分配异常
- 将值分配给字符串向量时的 Cygwin 异常
- 具有空异常规范的运算符new在分配返回0时调用构造函数
- 无效解除分配时的 xmemory0 异常
- 抛出异常后,对象内分配的指针是否自动解除分配
- 通过分配c内存错误捕获c++异常