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

时间:2015-03-09 22:53:48

标签: c++ pointers

我已经四处寻找并且没有找到我遇到的确切问题或可能的解决方案。

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

// 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 << "\n\n\n";
}

我已经删除了该程序的其余代码,因为我目前在打印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

当我在leftPtr->leftPrt函数中运行带有printPointerValues和相关位的代码时,我得到:

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)而其他人都没有... {/ p>

1 个答案:

答案 0 :(得分:2)

 struct numberList *firstPtr, *midPtr, *newPtr, *lastPtr;

声明了一堆指针,但并没有将它们设置为指向任何东西。你刚刚获得垃圾记忆

您需要将它们指定为指向全局,堆或堆栈列表

像这样

firstPtr = new numberList();

numberList l1;
firstPtr = &l1;