C++ ecliipse free():函数末尾的指针无效
c++ ecliipse free(): invalid pointer at end of function
我正在用Eclipse和OpenSSL制作一个程序。 我已经测试了我的代码并收到免费():无效的指针错误。 但是我没有在我的代码中使用new和delete,函数结束时发生了免费错误,aes_decrypt()(见吹) 它工作正常 8 次(调用 9 次),但接下来,正如我所说发生了错误。 我错过了什么吗?如果是这样,我该如何解决此错误? 非常感谢。
主()
{
in.open("/tmp/log.txt");
int size=11;
string trans[11];
string tmp;
for(int i=0; i<size;){
getline(in,tmp);
if(tmp[0]=='2'&&tmp[1]=='0'){
tmp=aes_MakeDecryptable(tmp);//Dont care about this function.
trans[i]=tmp;
i++;
}
}
Block myblock(0,trans,4);
myblock.BlockGen();
mychain.AddBlock(myblock);
cout<<"gethash() "<<mychain._GetLastBlock().GetHash()<<endl;
cout<<"getmerkle() "<<mychain._GetLastBlock().GetMerkle()<<endl;
cout<<myblock.CMerkle()<<endl;
cout<<Loadandcheck("2019-03-04 13:46:32")<<endl;//open and check stability 2019-03-04 13:46:32.txt
sleep(5);
cout<<"gethash() "<<myblock.GetHash()<<endl;//sha512 hash return
cout<<"getmerkle() "<<myblock.GetMerkle()<<endl;//merkle hash return
cout<<myblock.CMerkle()<<endl;//merkle hash return
return 0;}
函数 Loadandcheck()
string Loadandcheck(string node){
string result;
ifstream nodefd;
ifstream lengfd;
string tmp;
int length=0;
unsigned char tmpChar[256];
int tmpTransnum=0;
nodefd.open(node+".txt");
lengfd.open(node+"_leng.txt");
cout<<"loading "<<node+".txt"<<endl;
if(!nodefd.is_open() || !lengfd.is_open()){
cout<<"couldn't find node"<<endl;
return "-1";
}
else{
cout<<"open node ";
getline(nodefd, tmp);
cout<<node<<endl;
for(; !nodefd.eof();){
getline(nodefd,tmp);
if(tmp == " ")
length++;// get node number
}
length -=2;
cout<<"length "<<length<<endl;
string stringArray[length];
length =0;
//nodefd.close();
//nodefd.open(node+".txt");
nodefd.seekg(0, ios::beg);
getline(nodefd,tmp);
lengfd>>length;//length for aes decrypt
while(!nodefd.eof()){
if(tmp !=" "){
string substr;
while(true){
getline(nodefd,substr);
if(substr == " ")
break;
tmp +=' ';
tmp +=substr;
}
}
cout<<"tmp2 "<<tmp<<endl;
aes_StringToCharArr(tmp,tmpChar);
cout<<"tmpChar : "<<tmpChar<<" length : "<<length<<endl;
stringArray[tmpTransnum]=aes_decrypt(tmpChar,length,iv,key);//at here, free() error occurred after 9 times called, Before? fine
cout<<"result "<<stringArray[tmpTransnum]<<endl;
tmpTransnum++;
if(nodefd.eof() || lengfd.eof())
break;
getline(nodefd,tmp);
lengfd>>length;
}
cout<<"done"<<endl;
tmpTransnum=0;
nodefd.close();
lengfd.close();
cout<<endl<<"currently working at ";
for(int i=0; i<length;i++)
cout<<stringArray[i]<<endl;
cout<<endl;
result=LoadnMerkle(0,tmpTransnum,stringArray);
return result;
}}
函数 aes_decrypt()
string aes_decrypt(unsigned char* ciphertext,int ciphertext_len,unsigned char *iv, unsigned char* key){
unsigned char decrypt_result[256];
int decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,decrypt_result);
/* Add a NULL terminator. We are expecting printable text */
decrypt_result[decryptedtext_len] = ' ';
return (const char*)decrypt_result;}//free(): invalid pointer error occurred program terminate.
在终端
some hash...
一些哈希...
gethash() some hash...
getmerkle() some hash...
一些哈希...
加载时间:2019-03-04 13:46:32.txt 开放节点 2019-03-04 13:46:32 长度 9
tmp2 ǂ sS=e R tmpChar : ǂ sS=e R 结果 2019_02_26T08_00_06_35_2_Connect_debian_sys_maint@localhost_onusing_Socket 。 tmp2 j^ I C- 32 n v� 1/4 free():无效指针
除非length
constexpr
,否则您无法以这种方式定义string stringArray[length]
两个选项
- 将固定长度数组定义为
string stringArray[max]
其中 max 声明为 const 或 constexpr - 使用 std::vector
相关文章:
- 在 C++ 中传递字符串时指针无效
- free():仅在一种情况下指针无效
- free():实现矢量擦除()时的指针无效
- free():添加字符串时指针无效
- C++ ecliipse free():函数末尾的指针无效
- C++ 指针无效函数问题
- 将 boost::object_pool 与矢量一起使用时指针无效
- free():矢量 C++ 的指针无效<vec4i>
- omnet++free():指针无效,错误134/139
- 在OpenCV中调用stereoRectify会导致异常:帧指针无效
- 销毁和重新创建一个对象会使指向该对象的所有指针无效吗
- 用户定义类中的Null指针无效
- 删除指向类的指针无效
- 运行GTest时指针无效
- C++错误消息:指针无效
- 可以std::string reconfiguration使指向堆的指针无效
- 为什么删除指向指针向量的指针会导致指针无效
- MPI_scatter:缓冲区指针无效
- mmap 错误:分段错误/指针无效错误
- 返回 std::string 时指针无效(libc 如此说)