分割错误,找不到任何指针为NULL

Segmentation Fault, cannot find where any pointers are NULL

本文关键字:指针 NULL 任何 找不到 错误 分割      更新时间:2023-10-16
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;

标准#包含,定义了原型。

struct housingNode * readIn(housingNode * name); 
void displayAll(housingNode * name);  

我认为这个结构是正确的;

struct housingNode
{
  int index; 
  int rent;
  int size;
  int room;
  char currentLocation[101];
  char tFeat1[101];
  char tFeat2[101];
  char tFeat3[101];
  char * location;
  char * Feat1;
  char * Feat2;
  char * Feat3;
  housingNode * next;
};

int main()
{   
  housingNode * head;
  head = NULL;
  readIn(head);
  return 0; 
}

现在,我知道它一定在这两个函数中的一个中,因为第二个函数还没有被调用,我打赌它是这个函数,但是我找不到任何地方可以让我的指针为空,或者以这种方式引用它们。

struct housingNode* readIn(housingNode * name)
{
  housingNode * current;
  housingNode * temp;
  housingNode * head;   
  head = current;
  temp = current; 

  //file with housing information
  ifstream file_in;
  file_in.open("housingFile.txt");
  for(int i = 0; i < 12; ++i)
  { 
    //starts the building process for the LLL
    current = new housingNode;
    //reads in the information for the LLL
    file_in >> current -> rent;
    file_in.ignore(100,';');
    file_in >> current -> size;
    file_in.ignore(100,';');
    file_in >> current -> room;
    file_in.ignore(100,';');
    file_in.get(current -> currentLocation,100,';');
    file_in.ignore(100,';');
    //Allocates the needed memory for storing the information into a LLL. 
    current -> location = new char[strlen(current -> currentLocation)+1]; 
    strcpy(current -> location, current -> currentLocation);        
    file_in.get(current -> tFeat1,100,';');
    file_in.ignore(100,';');
    current -> Feat1 = new char[strlen(current -> tFeat1)+1];
    strcpy(current -> Feat1, current -> tFeat1);
    file_in.get(current -> tFeat2,100,';');
    file_in.ignore(100,';');
    current -> Feat2 = new char[strlen(current -> tFeat2)+1];
    strcpy(current -> Feat2, current -> tFeat2);
    file_in.get(current -> tFeat3,100,';');
    file_in.ignore(100,'n');
    current -> Feat3 = new char[strlen(current -> tFeat3)+1];
    strcpy(current -> Feat3, current -> tFeat3);
    temp -> next = current;
    temp = temp -> next; 
  }
  //resets the node to null
  current -> next = NULL;
  //Closes file after reading in information.
  file_in.close();
  //returns the head pointer
  return head;
}   
void  displayAll(housingNode * name)
{
  housingNode * traverse;
  housingNode * head;
  traverse = head;
  cout << "Listing housing information: " << 'n';
  for(int i = 0; i < 12; ++i)
  { 
    cout <<"Cost" << traverse -> rent <<'n';
    cout <<"Size: " << traverse -> size <<'n';
    cout <<"Number of Rooms: " << traverse -> room <<'n';
    cout <<"Location: " << traverse -> currentLocation <<'n';
    cout << "Features: " << 'n';
    cout <<"#1: " << traverse -> Feat1 <<'n';
    cout <<"#2: " << traverse -> Feat2 <<'n';
    cout <<"#3: " << traverse -> Feat3 << endl; 
    traverse = traverse -> next;
    //Full completed function for displaying all. 
  }
}

有一个问题:

struct housingNode* readIn(housingNode * name)
{
housingNode * current;
housingNode * temp;
housingNode * head; 
head = current;  // <-----

current在这里没有初始化,之后你使用的temp也没有正确初始化

这是其中一个问题(在函数中):

housingNode * current;  // and you do not initialize it
housingNode * temp;
housingNode * head; 

我认为你想用name代替current…

,但你也应该在调用函数

之前初始化它
head = current;
temp = current; 
current = new housingNode; // here the old value went away
...
temp -> next = current; // but tmp still points to an invalid address