*** `./pw' 中的错误:free():无效指针:0x0000000000602200 ***
Posted
技术标签:
【中文标题】*** `./pw\' 中的错误:free():无效指针:0x0000000000602200 ***【英文标题】:*** Error in `./pw': free(): invalid pointer: 0x0000000000602200 ****** `./pw' 中的错误:free():无效指针:0x0000000000602200 *** 【发布时间】:2017-09-29 18:46:17 【问题描述】:我在C++
中执行密码验证程序,它基本上将字符串传递作为输入,然后验证另一个字符串以确认它正确,最后检查输入的字符串是否格式正确。考虑到字符串的长度大于 6 且小于 20 个字符,具有至少 1 个大写字母和 1 个小写字母的字符串称为“正确格式”。我终于想出了写这个sn-p的代码:
#include <iostream>
using namespace std;
string pass, verify;
int cntUpper = 0, cntLower = 0;
int cntTry = 3;
int notValid();
int input();
string verify_format(string ab, string bc);
int main()
for (int i = 0; i <= 50; i++)
cout << "*";
cout << endl;
cout << "Welcome to password Validator\n";
for (int g = 0; g <= 50; g++)
cout << "*";
cout << endl;
input();
cin.ignore();
return 0;
int notValid(int a, int b)
cout << "\nYour password is not valid\n";
if (cntTry > 0)
cout << "Please try again. You have " << cntTry << " chances left\n";
cntTry--;
input();
else
cout << "You reached your maximum limits. Aborting...\n";
return 0;
int input()
cout << "Enter your password : ";
getline(cin, pass);
cout << "Verify : ";
getline(cin, verify);
verify_format(pass, verify);
string verify_format(string ab, string bc)
if (ab != bc)
cout << "Password do not matches. Try again!\n";
input();
else cout <<"\nPass Matched\n";
for (int i = 0; i < pass.size(); i++)
if (isupper(pass.at(i)))
cntUpper++;
for (int v = 0; v < pass.size(); v++)
if (islower(pass.at(v)))
cntLower++;
if (pass.size() > 20 || pass.size() < 6 || cntUpper == 0 || cntLower == 0)
notValid(cntLower, cntUpper);
else cout << "Password Accepted!\n";
我在终端中使用通常的 g++ pwcheck.cpp -o pw 命令编译!
我运行程序进行测试并输入 ILove***
作为输入,然后输入相同的字符串以验证 2 个字符串是否匹配。
输出有点出乎意料:
Pass Matched
Password Accepted!
*** Error in `./pw': free(): invalid pointer: 0x0000000000602200 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fba2b7627e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7fba2b76b37a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fba2b76f53c]
./pw[0x401149]
./pw[0x401053]
./pw[0x40130f]
./pw[0x40113d]
./pw[0x400fd4]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fba2b70b830]
./pw[0x400e89]
======= Memory map: ========
00400000-00402000 r-xp 00000000 00:16 21169 /home/lubuntu/pw
00601000-00602000 r--p 00001000 00:16 21169 /home/lubuntu/pw
00602000-00603000 rw-p 00002000 00:16 21169 /home/lubuntu/pw
022c4000-022f6000 rw-p 00000000 00:00 0 [heap]
7fba24000000-7fba24021000 rw-p 00000000 00:00 0
7fba24021000-7fba28000000 ---p 00000000 00:00 0
7fba2b3e2000-7fba2b4ea000 r-xp 00000000 00:16 9384 /lib/x86_64-linux-gnu/libm-2.23.so
7fba2b4ea000-7fba2b6e9000 ---p 00108000 00:16 9384 /lib/x86_64-linux-gnu/libm-2.23.so
7fba2b6e9000-7fba2b6ea000 r--p 00107000 00:16 9384 /lib/x86_64-linux-gnu/libm-2.23.so
7fba2b6ea000-7fba2b6eb000 rw-p 00108000 00:16 9384 /lib/x86_64-linux-gnu/libm-2.23.so
7fba2b6eb000-7fba2b8ab000 r-xp 00000000 00:16 9390 /lib/x86_64-linux-gnu/libc-2.23.so
7fba2b8ab000-7fba2baab000 ---p 001c0000 00:16 9390 /lib/x86_64-linux-gnu/libc-2.23.so
7fba2baab000-7fba2baaf000 r--p 001c0000 00:16 9390 /lib/x86_64-linux-gnu/libc-2.23.so
7fba2baaf000-7fba2bab1000 rw-p 001c4000 00:16 9390 /lib/x86_64-linux-gnu/libc-2.23.so
7fba2bab1000-7fba2bab5000 rw-p 00000000 00:00 0
7fba2bab5000-7fba2bacb000 r-xp 00000000 00:16 2192 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fba2bacb000-7fba2bcca000 ---p 00016000 00:16 2192 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fba2bcca000-7fba2bccb000 rw-p 00015000 00:16 2192 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fba2bccb000-7fba2be3d000 r-xp 00000000 00:16 2191 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fba2be3d000-7fba2c03d000 ---p 00172000 00:16 2191 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fba2c03d000-7fba2c047000 r--p 00172000 00:16 2191 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fba2c047000-7fba2c049000 rw-p 0017c000 00:16 2191 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fba2c049000-7fba2c04d000 rw-p 00000000 00:00 0
7fba2c04d000-7fba2c073000 r-xp 00000000 00:16 9367 /lib/x86_64-linux-gnu/ld-2.23.so
7fba2c259000-7fba2c25e000 rw-p 00000000 00:00 0
7fba2c26f000-7fba2c272000 rw-p 00000000 00:00 0
7fba2c272000-7fba2c273000 r--p 00025000 00:16 9367 /lib/x86_64-linux-gnu/ld-2.23.so
7fba2c273000-7fba2c274000 rw-p 00026000 00:16 9367 /lib/x86_64-linux-gnu/ld-2.23.so
7fba2c274000-7fba2c275000 rw-p 00000000 00:00 0
7fffde86a000-7fffde88b000 rw-p 00000000 00:00 0 [stack]
7fffde939000-7fffde93b000 r--p 00000000 00:00 0 [vvar]
7fffde93b000-7fffde93d000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)
我不确定那里到底发生了什么!任何帮助将不胜感激..
【问题讨论】:
【参考方案1】:int notValid(int a, int b);
int input();
string verify_format(string ab, string bc);
这些函数声称返回int
s 和string
s,但它们的实现在return
语句中明显缺乏。
从函数的末尾流出相当于没有值的返回;这会导致值返回函数中的未定义行为。 (link)
将它们更改为void
。
void notValid(int a, int b);
void input();
void verify_format(string ab, string bc);
【讨论】:
以上是关于*** `./pw' 中的错误:free():无效指针:0x0000000000602200 ***的主要内容,如果未能解决你的问题,请参考以下文章
随机错误核心转储:`./a.out' 中的错误:free():下一个大小无效(快速):0x00000000010e8d70 *** 中止(核心转储)
free pascal中的exitcode后面跟的数字,各代表啥?