【发布时间】:2013-09-23 15:08:04
【问题描述】:
这可能是一个非常愚蠢的问题。
我正在使用 malloc 进行内存分配。
程序编译正常,但启动时出现分段错误。
这是最小代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int row,col,i;
int **mat;
row = 3; //made row as static for testing
col = 4; //made col as static for testing
*mat = (int *)malloc(sizeof(int)*row);
for (i=0;i<row;i++)
mat[i] = (int *) malloc (sizeof(int)*col);
}
我编译时使用:gcc -ggdb test.c
在 gdb 上它的捐赠:
(gdb) run
Starting program: /slowfs/swe91/abjoshi/clients/fw_cl_for_seg_fault/a.out
Program received signal SIGSEGV, Segmentation fault.
0x00000000004004cc in main () at 1test.c:10
10 *mat = (int *)malloc(sizeof(int)*row);
注意:gcc 版本 4.5.2
【问题讨论】:
-
*mat = (int *)malloc(sizeof(int)*row); mat 还没有指向任何东西。
-
@CharlieBurns:这就是我将内存分配给指针的内容,然后我会将值存储在该内存位置。如果我遗漏了什么,请告诉我。
标签: c gcc segmentation-fault