您的位置:首页 > 其它

由于未分配内存的指针导致段错误

2016-09-19 19:59 225 查看
代码如下

#include<iostream>
using namespace std;

void f(char **s) {
*s = "chenyu";
}

typedef struct _node {
int val;
struct _node *next;
} Node;

typedef struct _link {
Node *front;
Node *rear;
} Link;

void queue(Link *link) {
//	link.front = link.rear = NULL; //这个地方不是.而是-> 请注意
link->front = link->rear = NULL;
}

int main() {
cout<<1<<endl;
char *s;
char **a;
a = (char **)malloc(sizeof(char **));
*a = "chenyu";
f(&s);
cout<<s<<endl;
cout<<*s<<endl;
cout<<*a<<endl;
char *b = {"hello"};
printf("%p\n",b);
printf("%s\n",b);
cout<<*b<<endl;
Link *link;
//link = (Link *)malloc(sizeof(Link *));
queue(link);
return 0;
}


编译没错

然后运行就出问题了

然后加上

link = (Link *)malloc(sizeof(Link *));


就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: