指针初始化到0x1,分段错误
Pointer Initializing to 0x1, Segmentation Fault
我已经搜索了一下,没有找到我遇到的确切问题或潜在的解决方案。
我有一个程序,它使用指向表的指针,当它们初始化时,我已经将问题缩小到我的指针。 以下是相关代码:
// Include header files
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int counter = 0;
// Structure prototype
struct numberList {
int value;
struct numberList *leftPtr = NULL;
struct numberList *rightPtr = NULL;
};
// Function prototypes
struct numberList *getNextNumber(); // Reads next number from input file
void addNumToList(numberList *, numberList *, numberList *, numberList *); // Takes next user import and adds it to the current linked list
void printPointerValues(numberList *, numberList *, numberList *, numberList *); // Used for debug purposes
// Main
int main() {
// Define variables
string filename;
int number;
struct numberList *firstPtr, *midPtr, *newPtr, *lastPtr;
printPointerValues(newPtr, firstPtr, midPtr, lastPtr);
return 0;
}
void printPointerValues(numberList *newPtr, numberList *firstPtr, numberList *midPtr, numberList *lastPtr) {
cout << "n";
cout << "counter:t" << counter++ << endl;
cout << "n";
// cout << "first value:t" << firstPtr->value << endl;
cout << "first address:t" << firstPtr << endl;
cout << "first left:t" << firstPtr->leftPtr << endl;
cout << "first right:t" << firstPtr->rightPtr << endl;
cout << "n";
// cout << "mid value:t" << midPtr->value << endl;
cout << "mid address:t" << midPtr << endl;
cout << "mid left:t" << midPtr->leftPtr << endl;
cout << "mid right:t" << midPtr->rightPtr << endl;
cout << "n";
// cout << "last value:t" << lastPtr->value << endl;
cout << "last address:t" << lastPtr << endl;
// cout << "last left:t" << lastPtr->leftPtr << endl;
// cout << "last right:t" << lastPtr->rightPtr << endl;
cout << "n";
// cout << "new value:t" << newPtr->value << endl;
cout << "newPtr address:t" << newPtr << endl;
cout << "newPtr left:t" << newPtr->leftPtr << endl;
cout << "newPtr right:t" << newPtr->rightPtr << endl;
cout << "nnn";
}
我已经删除了程序的其余代码,因为我目前在打印出 lastPtr->leftPtr
和 lastPtr->rightPtr
的值时遇到错误。
当我运行如上所示的程序时,我得到:
counter: 0
first address: 0xbfed29bc
first left: 0xbfed2c41
first right: 0xbfed2c53
mid address: 0xbfed29b4
mid left: 0
mid right: 0xbfed2c36
last address: 0x1
newPtr address: 0x8049653
newPtr left: 0xc629ffff
newPtr right: 0x8502fec1
当我在 printPointerValues
函数中使用未注释的leftPtr->leftPrt
和相关位运行代码时,我得到:
counter: 0
first address: 0xbf8c3ecc
first left: 0xbf8c5c41
first right: 0xbf8c5c53
mid address: 0xbf8c3ec4
mid left: 0
mid right: 0xbf8c5c36
last address: 0x1
Segmentation fault (core dumped)
该函数的全部意义在于调试出了什么问题。 幸运的是,它将其缩小到上面显示的内容。 问题是我不知道为什么有些指针可以正确初始化,而另一些则不能。 有什么想法吗? 另外,我不知道为什么midPtr->leftPtr
初始化为NULL
(或0
),而其他人都没有......
struct numberList *firstPtr, *midPtr, *newPtr, *lastPtr;
声明一堆指针,但不将它们设置为指向任何内容。你只是得到垃圾记忆
您需要将它们分配给指向全局、堆或堆栈列表
喜欢这个
firstPtr = new numberList();
或
numberList l1;
firstPtr = &l1;
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?