free():在 tcache 2 中检测到双重释放,在 2d 数组创建中中止(核心转储)

问题描述 投票:0回答:1

我正在创建一个具有动态行和动态列大小的二维数组。

我需要在主函数中分配内存。为了获得价值,打印应该在不同的函数中进行

我的代码按照我的预期工作,但最后我遇到了运行时错误

 free(): double free detected in tcache 2 Aborted (core dumped)

但是我最终在 main 中只使用了一个免费函数。出现这个错误的原因是什么。 当我使用以上 5 行和列时出现此错误。我正在使用 gcc 编译器。

代码:

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

void arr_value_getting_printing(int row,int col,int ** ptr)
{
    // Getting element from the user
    printf("Enter the elements:\n");
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col ; j++)
        {
            scanf("%d",&ptr[i][j]);
        }
    }
    

    // 2d array rows modification
    char ch;
    __fpurge(stdin);
    printf("Do you want to increase the Row(y/n):\n");
    scanf("%c",&ch);
    if(ch == 'y')
    {
    int new_row=0;
    printf("Enter the final row size (already 0 to %d row present):\n",row-1);
    scanf("%d",&new_row); 
    int**p =realloc(ptr, new_row * sizeof(int*));
    if (p != NULL)
    {
        ptr = p;
        // Getting the elements for the Newly created rows
        for(int i = row ; i < new_row ; i++ ) 
        {
            printf("Enter the elements for the new rows[%d]:",i);
            // Allocating memory for Newly created rows
            ptr[i] = calloc(col,sizeof(int)); 
            for(int j = 0; j < col ; j++)
            {
                scanf("%d",&ptr[i][j]);
            }
        }
        row = new_row;
    }   
    }


    // 2d array column modification
    __fpurge(stdin);
    // This arr pointer used for if any modification happens on any of the column it store the newly added column size
    int * arr = calloc(row,sizeof(int)); 
    printf("Do you want to increase the Col(y/n):\n");
    scanf("%c",&ch);
    while(ch == 'y')
    {
    int Row,Column;
    printf("Which Rows col you want to increase(Rows 0 - %d):\n",row-1);
    scanf("%d",&Row); // which row i need to modify
    printf("Enter the new col size(current col size %d):\n",col+arr[Row]);
    scanf("%d",&Column); // final column size
    int *temp = realloc (ptr[Row],Column); // roallocating
    if (temp != NULL)
    {
        ptr[Row] = temp;
        // updating the newly added column
        arr[Row] = Column - col; 
        printf("Enter the element for the new cols:\n");
        for(int j = col ; j < Column ; j++)
        {
            scanf("%d",&ptr[Row][j]);
        }
    }
    __fpurge(stdin);
    printf("Do you want to continue\n");
    scanf("%c",&ch);
    }


    // Printing the 2d array element
    printf("Printing the updated element array elenent:\n");
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col + arr[i] ; j++)
        {
            printf("%d ",ptr[i][j]);
        }
        printf("\n");
    }
}



int main()
{
    int row , col;
    printf("Enter the row and cols:\n");
    scanf("%d %d",&row,&col);
    int **ptr=calloc(row,sizeof(int*)); // 2d arr row creation 
    if (ptr != NULL)
    {
        for(int i = 0 ; i < row ; i++ )
        {
            ptr[i] = calloc(col,sizeof(int)); // 2d array column creation 
        } 

arr_value_getting_printing(row,col,ptr); //function call
    }
    free(ptr); // Freeing only one time
}
c multidimensional-array dynamic
1个回答
0
投票

当您

realloc()
中的指针
arr_value_getting_printing()
时,您需要释放新指针,但在返回时会丢失该值。
int **ptr
按值传递,因此
main()
仍将拥有旧指针,该指针现在正在被双重释放。

© www.soinside.com 2019 - 2024. All rights reserved.