进入第一个元素后出现分段错误

时间:2013-08-02 12:50:13

标签: c pointers multidimensional-array

输入矩阵的第一个元素后,我收到“Segmentation fault(core dumped)”。我知道当尝试访问某些东西时,就会发生分段错误,这种错误在物理上并不在内存中,但我不知道为什么会出现这种错误。

我正在使用指针,因为我正在学习指针的使用。

#include<stdio.h>
#include<stdlib.h>
void main()
{
    int i, j, m, n;
    int **p, **q, **res;
    p = (int**) malloc(10 * sizeof(int));
    q = (int**) malloc(10 * sizeof(int));
    res = (int**) malloc(10 * sizeof(int));
    printf("Enter the number of rows and columns:");
    scanf("%d %d", &m, &n);
    printf("Enter the elements of the matrix\n");
    for(i=0;i<m;i++) 
    {
        for(j=0;j<n;j++) 
        {
            scanf("%d", &(*(*(p+i)+j)));
        }
    }

    for(i=0;i<m;i++) 
    {
        for(j=0;j<n;j++)
        {
            printf("%d      ", (*(*(p+i)+j)));
        }
        printf("\n");
    }
}

3 个答案:

答案 0 :(得分:4)

这是因为您实际上并未为pqres内的数据分配内存。您为10个整数分配大小,但您应该首先分配10个整数指针,然后为其中的数据分配。

所以这样:

/* Get `m` and `n`... */

p = malloc(m * sizeof(int *));
for (i = 0; i < m; i++)
    p[i] = malloc(n * sizeof(int));

当然,这也必须为其他人完成。


此外,您知道您可以使用与数组相同的语法来访问它们吗?不需要指针算术或指针取消引用。只需一个简单的p[i][j]即可。

答案 1 :(得分:1)

从这里开始:

 p = (int**) malloc(10 * sizeof(int));

由于p ist int **,这应该是

 p = malloc(10 * sizeof(int *));

现在你已经为10个指针分配了内存,但是对于单个整数仍然没有内存。所以添加

for( i=0; i<10; i++ )
     p[i] = malloc(10 * sizeof(int));

不,你可以使用&(*(*(p+i)+j)(我更愿意写&p[i][j])0&lt; = i,j&lt; 10; 同样适用于qres,如果您使用过它们

答案 2 :(得分:1)

代码中的各种错误。
我已经对这些变化进行了评论。我已移除qres

代码有两种变体,一种使用大小为m * n的单个“块”内存,另一种使用一个大小为m的内存块来保留m指针m其他大小为n的内存块。

使用大小为m * n 的单个“块”内存 当m对于m行

中的每一行都是常数时,是有用的
#include <stdio.h>
#include <stdlib.h>

void main()
{
    int i,j,m,n;

    /* Changed to *p. Instead of an array of arrays 
       you'll use a single block of memory */
    int *p;

    printf("Enter the number of rows and columns:");
    scanf("%d %d",&m,&n);

    /* m rows and n columns = m * n "cells" */
    p = (int*) malloc(m * n * sizeof(int));

    printf("Enter the elements of the matrix\n");

    for (i=0;i<m;i++)
    {
        for (j=0;j<n;j++)
        {
            /* the element at row i and column j is the (i * m) + j 
               element of the block of memory, so p + (i*m) + j .
               It's already an address to memory, so you don't need the & */
            scanf("%d", (p + (i*m) + j));
        }
    }

    for (i=0;i<m;i++)
    {
        for (j=0;j<n;j++)
        {
            /* same as before, but this time you have 
               to dereference the address */
            printf("%d      ", *(p + (i*m) + j));
        }

        printf("\n");
    }
}

使用一个大小为m的内存块,以m指向m其他内容大小为n #include<stdio.h> #include<stdlib.h> void main() { int i,j,m,n; int **p; printf("Enter the number of rows and columns:"); scanf("%d %d",&m,&n); /* We will have m "rows", each element a ptr to a full row of n columns Note that each element has size == sizeof(int*) because it's a ptr to an array of int */ p = (int**) malloc(m * sizeof(int*)); printf("Enter the elements of the matrix\n"); for (i=0;i<m;i++) { /* For each row we have to malloc the space for the columns (elements) */ *(p + i) = (int*)malloc(n * sizeof(int)); for (j=0;j<n;j++) { /* Compare the reference to the one before, note that we first dereference *(p + i) to get the ptr of the i row and then to this ptr we add the column index */ scanf("%d", *(p + i) + j); } } for (i=0;i<m;i++) { for (j=0;j<n;j++) { /* Note the double dereferencing, first to the address of the row of data and then to the exact column */ printf("%d ", *(*(p + i) + j)); } printf("\n"); } } 个指针> 当m对m行(“锯齿状”数组)中的每一行都是可变的时,很有用

{{1}}