C6001:使用单元化内存"str[i]"
C6001: using unititialized memory "str[i]"
程序运行良好,尽管拳头打印的数字始终是"3452816845"。我尝试通过在定义数组时添加大括号或为其提供 NULL 值来初始化"str[i]",但随后第一个打印的数字始终为"零",然后它才会打印我输入的内容。请看下面:
#include <iostream>
using namespace std;
int main() {
unsigned* str = new unsigned[1000];
int cnt = 0;
char ch;
int a;
cout << "Please enter text: ";
do {
cin.get(ch);
if (ch <=57 && ch >=48) {
int a = ch - '0';
cnt++;
str[cnt] = a;
}
} while (ch != 'n');
cout << "The entered numbers are: ";
for (int i = 0; i <= cnt; i++) {
cout << str[i] << " "; // here is where the error appears
}
delete[] str;
return 0;
}
不要using namespace std;
.特别是不要在标题中,但也尽量不要在普通.cpp
文件中使用它。调试代码会更方便,这些代码明确地告诉您标识符来自哪个命名空间,就在使用该标识符的位置。
unsigned* str = new unsigned[1000];
自从 C++11 出现以来,像这样的"裸"内存分配是不受欢迎的,在这里绝对没有必要。
- 你可以只使用静态数组(
unsigned str[1000];
(。 - 您可以使用智能指针 (
auto str = std::make_unique<char[]>(1000);
(。 - 最好的选择,使用C++容器,如
<vector>
、<string>
或(如果开销真的困扰你(<array>
。
if (ch <=57 && ch >=48) {
int a = ch - '0';
不要在代码中使用"幻数"。如果想知道输入的字符是否为数字,请使用isdigit
,它更具表现力,甚至适用于非 ASCII 编码,这些编码的数字可能位于代码表中的不同位置。
int a = ch - '0';
这并没有错,因为标准保证这适用于数字。请注意,类似的字符算术(臭名昭著的... - 'a'
(是不受欢迎的,一旦你离开严格的ASCII-7编码领域,就会中断。
cnt++;
str[cnt] = a;
C/C++从零开始计数。您刚刚将数组中的第一项保留为未初始化状态。后增量的美妙之处在于,您可以在使用索引的位置进行操作,即str[cnt++] = a;
.
for (int i = 0; i <= cnt; i++)
cout << str[i] << " "; // here is where the error appears
}
非常C,也是错误的。您没有初始化str[0]
,因此通过该循环的第一轮访问未初始化的内存。如果您已经初始化了str[0]
(仅在将其用作索引后才递增cnt
(,i <= cnt
将超出您写入str[]
的项目,再次访问未初始化的内存。循环应该从0
运行到< cnt
(而不是<=
(。
如果您采用我之前的建议来使用<vector>
或<string>
,那么有一种更好的方法来循环存储在其中的项目,即范围。
#include <iostream>
#include <vector>
int main()
{
char ch;
std::vector< int > digits;
std::cout << "Please enter text: ";
do
{
std::cin.get( ch );
if ( std::isdigit( ch ) )
{
digits.push_back( ch - '0' );
}
} while (ch != 'n');
std::cout << "The entered numbers are: ";
for ( auto & i : digits )
{
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
你从不初始化str[0]
,但你输出它。
问题就在这里:
...
if (ch <=57 && ch >=48) {
int a = ch - '0';
cnt++;
str[cnt] = a;
}
...
您过早地递增cnt
,使str[0]
未初始化。你应该做:
if (ch <=57 && ch >=48) {
int a = ch - '0';
str[cnt++] = a;
}
此外,您的for
循环中确实存在问题;您应该从 0 开始,直到字符串中最后一个初始化的元素,该元素位于索引cnt - 1
处。它应该是这样的:
for (int i = 0; i < cnt; i++) {
cout << str[i] << " ";
}
或
for (int i = 0; i <= cnt - 1; i++) {
cout << str[i] << " ";
}
- 将字符串存储在c++中的稳定内存中
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- Win32编译器选项和内存分配
- 当vector是tje全局变量时,c++中vector的内存管理
- 带内存和隔离功能的SQLite
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 迭代时从向量和内存中删除对象
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 为什么示例代码访问IUnknown中已删除的内存
- 如何在C++类内存结构中创建"spacer"?
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何针对特定情况调试和修复此双自由内存损坏问题
- C6001:使用单元化内存"str[i]"
- 在 std::ostrstream::str 之后使用 std::ostrstream::冻结以防止内存泄漏
- 字符 *str;str= "HELLO" ;如何在不为字符串分配任何内存的情况下工作?