分段错误.是因为指针不好吗?
Segmentation Fault. Is it because of bad pointer?
为什么会出现此分段错误(核心转储(?我最初被告知这是因为我的指针 p_p_tictactoe = new char*[cols],但我被告知的不正确。每组代码的用途在整个代码中都有注释。代码正在运行,但我得到这个结果。我知道for循环必须是主要问题。
Please enter a number of rows: 4
Please enter number of columns: 3
Enter a single character for position( << i << ): a
Enter a single character for position( << j << ): b
Segmentation fault (core dumped)
#include <iostream>
using namespace std;
int main()
{
// TODO: Below each Bp_tictactoe` of type pointer-to-a-pointer-to-a-char
char **p_p_tictactoe;
// 2. Prompt your user to enter a number of rows, then a number of columns.
// store their answers in the variables `rows` and `cols`.
char rows;
char cols;
cout << "Please enter a number of rows: ";
cin >> rows;
cout << "Please enter number of columns: ";
cin >> cols;
// 3. Allocate a 1-dimensional array of pointers-to-chars (length == `rows`)
// and store its address in `p_p_tictactoe`
p_p_tictactoe = new char*[rows];
// 4. Use a for-loop to allocate a dynamic array of chars (length == `cols`)
// for i from 0 to rows - 1 and store its address in `p_p_tictactoe[i]`.
for (int i = 0; i < rows - 1; i++)
{
p_p_tictactoe = new char*[cols];
}
// 5. Use a for-loop to prompt the user to enter a char for each position in
// (" << i << ", " << j << "): "
// As you read each char, store it in the array.
// 6. Use a nested for-loop to print the array, one row per line. The chars
// for each row should be space-separated. For example, if the array is
// 2 x 3 and stores the values A, B, C, X, !, &, the output should look
// like:
// A B C
// X ! &
char new_input1;
char new_input2;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << "Enter a single character for position( << i << ): ";
cin >> new_input1;
cout << "Enter a single character for position( << j << ): ";
cin >> new_input2;
*p_p_tictactoe[i] = new_input1;
*p_p_tictactoe[j] = new_input2;
cout << *p_p_tictactoe[i] <<endl;
}
}
// *** Prevent memory leaks by deallocating dynamic memory when you are done
// using it. ***
// 7. Use a for-loop to delete each row of the dynamic array.
// 8. Delete the pointer-to-a-pointer to release the array of row pointers,
// and set its value to NULL to avoid accessing invalid memory.
for (int i = 0; i < 3; i++)
{
delete[] p_p_tictactoe[i];
delete[] p_p_tictactoe;
}
cout << "Bye!" << endl;
return 0;
}
除其他外,您对p_p_tictactoe
的分配不正确。 这是一个双指针,这仅仅意味着它是一个指向指针数组的指针。 您的两步分配是正确的想法,但您在 for 循环中的内容是不正确的。 在行p_p_tictactoe = new char*[rows]
之后,您现在有一个指向char*
类型数组的指针。因此,如果rows
是 4,则内存中现在的内容如下所示:
p_p_tictactoe[0] == char* --> junk
[1] == char* --> junk
[2] == char* --> junk
[3] == char* --> junk
现在,您必须遍历这 4 个char*
中的每一个并为它们分配空间。 每个char*
都必须指向一个chars
数组。 这是注释为您提供有关循环和使用p_p_tictactoe[i]
索引的提示的地方:
for (int i = 0; i < rows - 1; i++)
{
p_p_tictactoe[i] = new char[cols];
}
现在,对于cols == 3
,在内存中您有:
p_p_tictactoe[0] == char* --> 3 consecutive bytes
[1] == char* --> 3 consecutive bytes
[2] == char* --> 3 consecutive bytes
[3] == char* --> 3 consecutive bytes
您发布的代码是内存泄漏。 每次执行p_p_tictactoe = new char*[#]
时,操作系统都会进入堆以获取足够的内存以进行分配。 您没有跟踪上一个指针,也没有先释放它,因此分配的内存现在没有任何指向它的内容。
同样的理论也适用于释放记忆。 你最后得到的不太正确。 解除分配始终是分配的镜像。 这显然是一个家庭作业,所以我不会发布该代码,但它与分配相同,只是相反。
我强烈建议使用 gdb
,它是 linux 的文本调试器(或任何等效的调试器(。 如果你想有希望用 C/C++ 成功编码,你必须学习内存在堆栈和堆上是如何工作的,你必须学习如何正确管理它,否则你将陷入一个受伤的世界。 gdb
一开始有点令人生畏,但它可以让您打印出内存地址并检查内存,这对于学习和强化您认为您知道的内容非常有帮助。
我认为这里的问题是你应该为双指针变量使用一个双数组..你把两个不同的东西重新分配给一个变量。
你的代码的一个问题是你的宽度和高度没有按照你认为的方式解释。当你写这个的时候:
char width;
cin >> width;
。生成的程序读取一个字符并将其 ASCII 值分配给 width
。因此,如果您输入"4",则width == 52
的计算结果为 true
,width == 4
会false
.(但width == '4'
会因为'4' == 52
而true
(。
这个问题很容易解决:只需使用int
而不是char
。无论如何,您都不会节省任何内存,因为new
可能会创建单词对齐的指针。
这是Tarang Gupta指出的问题之外的。
这是因为以下几行:
*p_p_tictactoe[i] = new_input1;
*p_p_tictactoe[j] = new_input2;
您将其视为具有 2 维的数组。
以下内容写错了:
p_p_tictactoe = new char*[rows];
for (int i = 0; i < rows - 1; i++)
{
p_p_tictactoe = new char*[cols];
}
您首先为大小sizeof (char*[rows])
p_p_tictactoe
分配了一个空间,并将其内容替换为 p_p_tictactoe = new char*[cols];
(int)row
次。代码中有很多未使用和未引用的内存。
即使在更改代码以防止此错误后,以下内容也会再次导致问题:
for (int i = 0; i < 3; i++)
{
delete[] p_p_tictactoe[i];
delete[] p_p_tictactoe;
}
您正在连续删除 3 次分配和引用p_p_tictactoe
的空间。它应该在循环之外。
- 为什么我在空指针错误(链表)中获取成员访问权限
- std::函数指针错误:无法将 &A::a 转换为类型 std::function<>&&
- 为什么我得到 munmap_chunk():无效的指针:错误
- 成员模板函数指针错误
- 删除指针时,未分配被释放的 C++ 指针错误
- Visual Studio 中的"指向引用的指针"错误不会按发生位置的文件名显示
- C++数组指针错误无法将“int*”转换为“int**”
- C++ 通过函数指针错误调用成员函数
- 从 BST 中删除元素(指针错误)
- 随机生成的多维布尔指针错误
- 在测试中使用unique_ptr时指针错误无效
- 使用 malloc 而不是 new 会导致 free():指针错误无效
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- 抽象类和唯一指针 c++ 错误
- 返回函数指针错误而不带类型定义
- 练习中出现空指针错误
- 指针错误导致的分段错误(简单..)
- free():C++中的指针错误无效
- 使用布尔值数组"表达式必须具有指向对象类型的指针"错误C++
- 指针错误的C 数组