指针初始化到0x1,分段错误

Pointer Initializing to 0x1, Segmentation Fault

本文关键字:分段 错误 0x1 初始化 指针      更新时间:2023-10-16

我已经搜索了一下,没有找到我遇到的确切问题或潜在的解决方案。

我有一个程序,它使用指向表的指针,当它们初始化时,我已经将问题缩小到我的指针。 以下是相关代码:

// 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->leftPtrlastPtr->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;