尝试使用指向此指针的指针重新分配指针时崩溃

标签 c pointers crash allocation pointer-to-pointer

我有一个指向指针(“路径”)的指针,我想重新分配每个指针(每个“路径”)。但我遇到了崩溃。一般来说,我试图找到一个数字的所有可能的幂,可以计算一定数量的运算(例如,对于两个运算,我们可以获得三和四的幂(一个运算用于数字的平方,然后另一个运算用于幂)三四个))。我在纸上弄清楚了如何做到这一点,现在我正在尝试用代码实现它。这是我的尝试:

#include <stdio.h>
#include <stdlib.h>

void print_path(const int *path, int path_length);

int main(void)
{
    fputs("Enter number of operations? ", stdout);
    int operations;
    scanf("%i", &operations);
    int **paths, *path, npaths, npath;
    npaths = npath = 2;
    path = (int*)malloc(npath * sizeof(int));
    paths = (int**)malloc(npaths * sizeof(path));
    int i;
    for (i = 0; i < npaths; ++i)    // paths initialization
    {
        int j;
        for (j = 0; j < npath; ++j)
            paths[i][j] = j+1;
    }
    for (i = 0; i < npaths; ++i)    // prints the paths, all of them are displayed correctly
        print_path(paths[i], npath);

    for (i = 1; i < operations; ++i)
    {
        int j;
        for (j = 0; j < npaths; ++j) // here I am trying to do it
        {
            puts("trying to reallocate");
            int *ptemp = (int*)realloc(paths[j], (npath + 1) * sizeof(int));
            puts("reallocated");    // tried to write paths[j] = (int*)realloc...
            paths[j] = ptemp;   // then tried to make it with temp pointer
        }
        puts("memory reallocated");
        ++npath;
        npaths *= npath;    // not sure about the end of the loop
        paths = (int**)realloc(paths, npaths * sizeof(path));
        for (j = 0; j < npaths; ++j)
            paths[j][npath-1] = paths[j][npath-2] + paths[j][j];
        for (j = 0; j < npaths; ++j)
            print_path(paths[j], npath);

        puts("\n");
    }
    int c;
    puts("Enter e to continue");
    while ((c = getchar()) != 'e');
    return 0;
}

void print_path(const int *p, int pl)
{
    int i;
    for (i = 0; i < pl; ++i)
        printf(" A^%i -> ", p[i]);
    puts(" over");
}

最佳答案

我不确定问题是否出在对 realloc() 的调用上,而是您正在尝试写入尚未为其创建空间的位置...
尽管您为指针创建了内存,但没有为实际存储位置创建空间(分配内存)。

下面是一个为 int 二维数组分配内存的函数示例:

int ** Create2D(int **arr, int cols, int rows)
{   
    int space = cols*rows; 
    int    y;

    arr   = calloc(space, sizeof(int));
    for(y=0;y<cols;y++)
    {
        arr[y] = calloc(rows, sizeof(int)); 
    }
    return arr;
}  

void free2DInt(int **arr, int cols)
{
    int i;
    for(i=0;i<cols; i++)
        if(arr[i]) free(arr[i]);
    free(arr);  
}

使用示例:

#include <ansi_c.h>
int main(void)
{
    int **array=0, i, j;
    array = Create2D(array, 5, 4);
    for(i=0;i<5;i++)
        for(j=0;j<4;j++)
            array[i][j]=i*j; //example values for illustration
    free2DInt(array, 5);

    return 0;

}

这里的另一点是,转换 [m][c][re]alloc() 的返回很少是一个好主意函数

编辑

此插图显示了我对您的代码的运行,正如您所呈现的那样: enter image description here

发生错误时,i==0 & j==0。位置 paths[0][0] 处的指针未初始化。

编辑 2
要重新分配 int 的二维数组,您可以使用类似以下内容的内容:

int ** Realloc2D(int **arr, int cols, int rows)
{   
    int space = cols*rows; 
    int    y;

    arr   = realloc(arr, space*sizeof(int));
    for(y=0;y<cols;y++)
    {
        arr[y] = calloc(rows, sizeof(int)); 
    }
    return arr;
}    

这是一个测试函数,演示了它是如何工作的:

#include <stdio.h>
#include <stdlib.h>

int ** Create2D(int **arr, int cols, int rows);
void free2DInt(int **arr, int cols);
int ** Realloc2D(int **arr, int cols, int rows);

int main(void)
{
    int **paths = {0};
    int i, j;

    int col = 5;
    int row = 8;

    paths = Create2D(paths, col, row);
    for(i=0;i<5;i++)
    {
        for(j=0;j<8;j++)
        {
            paths[i][j]=i*j;    
        }
    }
    j=0;
    for(i=0;i<5;i++)
    {

        for(j=0;j<8;j++)
        {
            printf("%d ", paths[i][j]); 
        }
        printf("\n");
    }
    //reallocation:
    col = 20;
    row = 25;

    paths = Realloc2D(paths, col, row);
    for(i=0;i<20;i++)
    {
        for(j=0;j<25;j++)
        {
            paths[i][j]=i*j;    
        }
    }
    j=0;
    for(i=0;i<20;i++)
    {

        for(j=0;j<25;j++)
        {
            printf("%d ", paths[i][j]); 
        }
        printf("\n");
    }

    free2DInt(paths, col);

    getchar();
    return 0;
}

关于尝试使用指向此指针的指针重新分配指针时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28306090/

相关文章:

Java虚拟机多次崩溃

c - 如何使用指针初始化 char 数组?

指向对象 vector 的 C++ 指针,需要访问属性

c - C 中的 QSORT 函数

eclipse - 如何诊断 eclipse 崩溃?

c++ - 超过 8 个元素的动态数组崩溃

c - 为什么网络连接中断会导致 stdin 上出现 EOF?

将 char 指针强制转换为 long

c - 获取 Debug Build 下 main() 函数的地址

c - 使用char指针模仿c中的strcpy函数