[英]segmentation fault in during dynamic memory allocation with malloc
This might be very silly question to ask. 这可能是一个非常愚蠢的问题。
I am using malloc to memory allocation. 我正在使用malloc来进行内存分配。
Program compiles fine but segmentation fault occurs while launching. 程序编译很好但启动时出现分段错误。
Here is min code: 这是最小代码:
#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);
}
i compiled with: gcc -ggdb test.c 我用以下代码编译:gcc -ggdb test.c
on gdb its giving: 在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);
Note: gcc version 4.5.2 注意:gcc版本4.5.2
The common malloc
idiom is as folows: to allocate an array of n
elements pointed by pointer p
use the following universal syntax 常见的malloc
习语如下:要分配由指针p
指向的n
元素的数组,请使用以下通用语法
p = malloc(n * sizeof *p);
That will help you to avoid errors as the one in your code. 这将帮助您避免代码中的错误。 Note the key points here: don't cast the result of malloc
and don't use types under sizeof
. 请注意这里的关键点:不要malloc
的结果,也不要在sizeof
下使用类型。 And in general: as much as possible, avoid mentioning types anywhere outside declarations. 一般而言:尽可能避免在声明之外的任何地方提及类型。
In your case the first allocation should look as 在您的情况下,第一次分配应该看起来像
mat = malloc(row * sizeof *mat);
The allocations inside the cycle should look as 循环内的分配应该如下
for (i = 0; i < row; ++i)
mat[i] = malloc(col * sizeof *mat[i]);
The important benefit of this approach is that the code is type-independent: at any moment you can change the declaration of mat
from int **mat
to, say, double **mat
, but the allocation code will continue to remain valid - you won't have to make any changes there. 这种方法的重要好处是代码是独立于类型的:在任何时候你都可以将mat
的声明从int **mat
更改为,例如, double **mat
,但分配代码将继续保持有效 - 你不必在那里做任何改变。
Instead of 代替
*mat = (int *)malloc(sizeof(int)*row);
I think you want 我想你想要的
mat = (int **)malloc(sizeof(int *) * row);
As it is, you are dereferencing mat which is uninitialized. 实际上,您正在取消引用未初始化的mat。 You need to malloc() mat before you deference it. 在你尊重它之前,你需要malloc()mat。
You can leave the cast off the malloc as well: 您也可以将演员阵容从malloc中移除:
mat = malloc(sizeof(int *) * row);
As well as the sizeof(int *) reference: 以及sizeof(int *)引用:
mat = malloc( row * sizeof(*mat));
Compiler would have told it with -Wall 编译器会用-Wall告诉它
$ gcc -Wall y.c
y.c: In function ‘main’:
y.c:14:1: warning: control reaches end of non-void function [-Wreturn-type]
y.c:11:8: warning: ‘mat’ is used uninitialized in this function [-Wuninitialized]
In the line *mat = (int *) malloc(sizeof(int)*row);
在行*mat = (int *) malloc(sizeof(int)*row);
you are trying to dereference mat. 你试图取消引用垫子。 mat is currently uninitialized. 垫目前尚未初始化。
Here's what: 这是什么:
mat
is a double pointer and it is uninitialized (or NULL). mat
是一个双指针,它是未初始化的(或NULL)。 When you dereference it: 当你取消引用它时:
*mat = (int *)malloc(sizeof(int)*row);
you are dereferencing a NULL value and trying to assign to it, causing the segmentation fault. 您正在取消引用NULL值并尝试分配给它,从而导致分段错误。
First initialize mat with a pointer to list of (int*) pointers. 首先使用指向(int *)指针列表的指针初始化mat。 Then individually take each pointer in the list and allocate (int) space for each. 然后单独获取列表中的每个指针并为每个指针分配(int)空间。
mat = (int *)malloc(cols*sizeof(int *));
for (i=0;i<cols;i++)
mat[i] = malloc(rows*sizeof(int))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.