- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 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);
这些函数声称返回 int
和 string
,但在 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/
在指向指针的指针上使用指针算术是否定义明确? 例如 int a=some_value; int* p=&a; int**p2=&p; 现在对 p2 执行算术是否是定义明确的行为?(例如 p2+1、p2
我正在尝试使用一个函数来替代 C 中的 scanf()。该函数是由第三方编写的,并进行了相应的定义: ScanDecimal16uNumber - Scans a decimal 16bit unsi
我正在尝试为 Sundials CVODE 编写 CFFI 包装器图书馆。 SWIG 被 Sundial header 阻塞,因为它们相互关联,并且 SWIG 找不到合适的 header ,所以我手工
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: pass by reference not working 我正在阅读一些教程 linklistproblem在互联
我有一个代码片段很难理解。 char *c; // c is uni dimensional table ( single row ) char **p ; // p is a two dimen
我正在将一些代码移植到 Windows 并且被难住了。有一些代码在启动时自动运行以将指针复制到指针,并在退出时再次运行以删除指向指针的指针(如果它不为空)。 我已经创建了一个示例程序来重现该行为 in
将非 const 指针转换为 const 指针是合法的。 那为什么将指向非const的指针转换为指向const的指针是不合法的呢? 例如,为什么下面的代码是非法的: char *s1 = 0; con
将非 const 指针转换为 const 指针是合法的。 那为什么将指向非const的指针转换为指向const的指针是不合法的呢? 例如,为什么下面的代码是非法的: char *s1 = 0; con
将指向非常量的指针转换为指向常数的指针是合法的。 那么为什么将指向非const的指针转换为指向const的指针是不合法的呢? 例如,为什么下面的代码是非法的: char *s1 = 0; const
之间有什么区别 procedure(some_routine), pointer :: ptr ptr => null() 和 procedure(some_routine), pointer ::
只是为了消除一些困惑。我最近遇到了这段代码(使用指针到指针): int encode(unsigned char type, uint64_t input_length, unsigned char*
我已经阅读了我能找到的有关 C/C++ 指针的内容,但其中大部分是介绍性的,虽然它可以帮助您理解它们的使用,但在许多情况下,现有代码会抛出难以破译的示例。 我确实看到了一些例子,他们将一行代码分解成它
我一直在关注的学习数据结构的书使用“单指针”作为函数中的参数,这些函数在链表的不同位置添加新节点,例如在开始,在结束。同样在删除的情况下使用“pointer-to-pointer”。在所有这些情况下,
考虑这段代码: #define MAX 4 ............ ............ int** ptr = (int**)malloc(sizeof(int*)*MAX); *ptr =
如何将指向 void 对象的指针转换为类对象? 最佳答案 使用 static_cast。请注意,只有当指针确实指向指定类型的对象时,您才必须这样做;也就是说,指向 void 的指针的值取自指向此类对象
我假设一种语言的实现允许您将指针视为整数,包括对它们进行标准算术。如果由于硬件限制这是不现实的,请告诉我。如果编程语言通常没有这么强大的指针运算,但是在实践中是可行的,那么我仍然想知道这种实现BigI
我是一名 nodejs 开发人员,我通常为我的应用程序使用一个结构,该结构包含一个配置包/对象,该对象包含对我常用的库和配置选项的引用。通常,此配置对象也包含我的数据库连接,并且可以通过我的应用程序访
我已经在几个上下文中阅读过“胖指针”这个术语,但我不确定它的确切含义以及它何时在 Rust 中使用。指针似乎是普通指针的两倍,但我不明白为什么。它似乎也与特征对象有关。 最佳答案 术语“胖指针”用于指
这是让我困惑的代码。 static char *s[] = {"black", "white", "pink", "violet"}; char **ptr[] = {s+3, s+2, s+1, s
通用指针允许您创建指向指针的指针: void foo(Object **o) {} int main() { Object * o = new Object(); foo(&o); } s
我是一名优秀的程序员,十分优秀!