由于未分配内存的指针导致段错误
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 *));
就可以了
相关文章推荐
- 未分配内存的指针导致段错误
- 捕获由于无效指针导致的内存读写异常,最后导致进程异常中止(仅适用于Windows系统)
- 给结构体指针分配内存后依然出现段错误的解决
- 深入理解C指针学习笔记---多级指针的内存分配问题(五)
- C语言指针的内存分配和Java中的引用
- 【百度分享】频繁分配释放内存导致的性能问题的分析
- 包含双重指针的结构体指针的分配内存与释放内存
- delphi 指针的内存分配和释放
- java 虚拟机参数设置堆大小的分配可能导致内存暴增
- socket通信中的动态分配内存导致的错误
- C和指针之动态内存分配malloc、calloc、realloc简单使用和区别
- 指针定义时未明确内存区域导致的内存溢出
- 频繁分配释放内存导致的性能问题的分析
- C和指针之动态内存分配常见问题和总结
- 指针与数组的内存分配
- 内存的堆分配和栈分配 & 字符数组,字符指针,Sizeof总结
- C和指针之动态内存分配堆、栈、全局区(静态区)、常量区对比总结学习笔记
- 指针操作_1,指针基础Pointer Fundamentals与内存分配storage allocation
- C语言 内存分配 地址 指针 数组 参数 实例解析
- 解决Python中由于logging模块误用导致的内存泄露