分割错误,找不到任何指针为NULL
Segmentation Fault, cannot find where any pointers are NULL
#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
相关文章:
- 当使用通配符和null指针调用函数时,对输出的说明
- 当目标指针不是基类的类型时,为什么允许dynamic_cast为多态类生成 null 指针?
- API 返回智能指针的 std::optional 以明确指定指针可能为 null 是否有意义?
- 为什么在C++中增加指针后打印了一个值而不是 NULL/0?
- 指针永远不会在链表深层复制构造函数中达到 null
- 正在将对象指针数组初始化为NULL
- 为什么在C++中使用delete后指针不为NULL
- C++中的指针否定 (!ptr == NULL)
- 如何返回值为NULL的变量指针
- 打印空指针时,std::cout 可以打印 "NULL" 而不是 0 吗?
- 将位数组转换为 HBITMAP 后 bmBits 的 NULL 指针
- C++ 由于类析构函数中的指针设置为 NULL 而导致的内存泄漏
- 如果指针在C++为 NULL 则修改指针
- 在访问类的树层次结构中的对象时避免使用 null 指针
- if(NULL==指针)和if(指针==NULL)之间有什么区别
- 从转储中获取调用堆栈的几乎所有帧中的此指针 NULL 表明什么
- 指针 NULL 在C++中的函数
- 自动指针= NULL错误
- 如何使指针指向指针NULL
- 当我们分配指针 NULL 时会发生什么