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

来源:互联网 发布:缺血半暗带的算法 编辑:程序博客网 时间:2024/04/29 16:16

代码如下

#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 *));

就可以了


0 0