gpt4 book ai didi

c++ - *** 错误 `./pw' : free(): invalid pointer: 0x0000000000602200 ***

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:50 26 4
gpt4 key购买 nike

我在 C++ 中做了一个密码验证器程序,它基本上将一个字符串传递作为输入,然后另一个字符串验证以确认它是正确的,最后检查输入的字符串是否正确格式与否。假设字符串的长度大于 6 且小于 20 个字符,则具有至少 1 个大写字母和 1 个小写字母的字符串被称为“正确格式”。我终于想出了写这段代码:

#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 命令编译了!我运行程序进行测试并输入 ILoveStackoverflow 作为输入,然后输入相同的字符串以验证 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)

我不确定那里到底发生了什么!任何帮助将不胜感激..

最佳答案

int notValid(int a, int b);
int input();
string verify_format(string ab, string bc);

这些函数声称返回 intstring,但在 return 语句中明显缺少它们的实现。

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function. (link)

将它们更改为 void

void notValid(int a, int b);
void input();
void verify_format(string ab, string bc);

关于c++ - *** 错误 `./pw' : free(): invalid pointer: 0x0000000000602200 ***,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46495209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com