2017-09-29 30 views
1

我在做一个密码验证程序C++,它基本上把一个字符串作为输入,然后再验证一个字符串来确认它是正确的,并最终检查输入的字符串格式是否正确。假设字符串的长度大于6且小于20个字符,则至少包含1个大写字母和1个小写字母的字符串被称为“格式正确”。我终于写出了这段代码:*** ./pw中的错误:free():无效指针:0x0000000000602200 ***

#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) 

我不知道发生了什么确切发生的事情!任何帮助将大大appreaciated ..

回答

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);