progam在std :: getline AM之后停止响应,代码中是否存在错误
progam stop responding after std::getline am is there a mistake in the code?
添加名称(nome)并按Enter Enter the progam关闭我已经尝试使用的progam代码的一部分在下面
aux1 = (struct no *) malloc(sizeof(struct no)+1);
printf("nnDigite numero a ser inserido: ");
gets(auxitem);
aux1->numero = atoi(auxitem);
printf("nnDigite o nome do contato ser inserido: ");
std::getline(std::cin, aux1->nome);
aux1->esq = aux1->dir = (struct no*) NULL;
if (raiz == (struct no *) NULL)
raiz= aux1;
我假设no
是一个名为 nom
的成员的类,其类型是std::basic_string
的某种实例化(例如std::string
))。在这种情况下,no
不是POD类型,为了使用它,您需要构造它。aux1
不指向构造的no
对象。它只是指向Malloc分配的原始内存。因此,您将使用不是有效的构造字符串对象的std::getline
调用CC_9。调用malloc
后,您需要构造一个对象。
aux1 = (struct no *) malloc(sizeof(struct no)+1);
new (aux1) no(/* constructor arguments here */);
...
// later, when you're done with the object
aux1->~no();
free(aux1);
或用new
表达式分配对象,该表达式将处理分配和构造。
aux1 = new no(/* constructor arguments here */);
...
// later, when you're done with the object
delete aux1;
相关文章:
- 此代码是否违反一个定义规则
- C++ LeetCode #377 的 DP 解决方案中,此代码是否有错误?
- 在cpp中使用boost-python的python代码是否进行动态内存分配
- 使用全局 IP 地址时,C++ winsock 2 应用程序中的代码是否必须更改?
- 以下代码是否存在内存泄漏?
- 如何判断我正在运行的Microsoft C++代码是否使用 /EHa 开关编译?
- 我的选择排序代码是否存在导致它跳过数组中的元素的问题?
- 代码是否有效.如果我想显示第一个元素?如果不是,那么 s.begin() 会返回什么?
- C++模板的对象代码是否在可执行文件和动态库中重复?
- 任何人都可以检查这个回文 c++ 代码是否正确
- Android Studio 项目中的 c++ 代码是否最终出现在 apk 文件中(除了 .so 文件)
- 此代码是否容易受到 SQL 注入的攻击?我该如何使其安全
- 使用 log4cplus 将日志发送到远程系统日志服务器的代码是否正确
- 代码是否交换数组的两个数字
- 如何检查编译的代码是否使用了 SSE 和 AVX 指令?
- 此代码是否有逻辑错误
- 使用 char16_t 类型作为 char[] 数组,并通过 reinterpret_cast<> 重新转换它。我的代码是否有未定义的行为?
- GPU cuda 代码是否适用于多个 GPU 卡而无需任何实现
- 此代码是否对C++线程安全
- QaudioOutput代码是否有任何内存泄漏