C 双指针重新分配程序在连续运行两次后挂起

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

每当我尝试运行我编写的这段代码时,它第一次就可以正常工作。第二次,执行在最后挂起(它执行了函数中的所有内容,但从未退出函数)。所有文件都在同一个文件夹中。这些是文件:

main.c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "matrixTests.h"

int main() {
    
    int8_t a = 0;
    int8_t *p = &a;
    printf("\n%d ", *p);
    printf("\n0x%X ", p);
    printf("\n0x%X ", &a);
    printf("\n0x%X ", &p);
    printf("\n-----------------\n");
    //printSeven();
    //multiDimensionalTest();
    array2Type row_sizes[] = {1,2,3,2,1,7};
    matrixCustomRowLengthsResize(6, row_sizes);
    printf("\n-----------------\n");    

    return 0;
}

矩阵测试.h

#ifndef matrixTests
#define matrixTests
#include <stdint.h>

#define array1Type uint16_t 
#define array2Type uint16_t 

void multiDimensionalTest();
void multiDimensionalDemo();
void matrixCustomRowLengths(array2Type nr_rows, array2Type row_lenghts[]);
void matrixCustomRowLengthsResize(array2Type nr_rows, array2Type row_lenghts[]);
void printSeven();


#endif

matrixTests.c(我只包含相关函数)

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "matrixTests.h"

void matrixCustomRowLengths(array2Type nr_rows, array2Type row_lenghts[])
{
    array2Type i = 0;
    array2Type j = 0;
    array2Type arr_size_1 = nr_rows;
    array2Type **arr = (array2Type**)malloc(arr_size_1 * sizeof(array2Type));
    for(i=0;i<arr_size_1;i++)
    {
        printf("Array %d: ", i);
        *(arr+i) = (array2Type*)malloc(row_lenghts[i] * sizeof(array2Type));
        for(j = 0; j < row_lenghts[i]; j++)
        {
            *(*(arr+i) + j) = (array2Type)(i + j);
            printf("Collumn %d: val = %hu | ", j, *(*(arr+i) + j));
        }
        printf("\n");
    }

    for( i = arr_size_1-1; i>0; i--)
    {
        free(*(arr+i));
    }
    free(arr);

}

void matrixCustomRowLengthsResize(array2Type nr_rows, array2Type row_lenghts[])
{
        array2Type i = 0;
    array2Type j = 0;
    array2Type arr_size_1 = nr_rows;
    array2Type **arr = (array2Type**)malloc(arr_size_1 * sizeof(array2Type));
    for(i=0;i<arr_size_1;i++)
    {
        printf("Array %d: ", i);
        *(arr+i) = (array2Type*)malloc(row_lenghts[i] * sizeof(array2Type));
        for(j = 0; j < row_lenghts[i]; j++)
        {
            *(*(arr+i) + j) = (array2Type)(i + j);
            printf("Collumn %d: val = %hu | ", j, *(*(arr+i) + j));
        }
        printf("\n");
    }
    
    printf("\nResize Example\n");
    uint16_t index_resize = nr_rows / 2;
    uint16_t resize_size = 10;
    i = index_resize;
    row_lenghts[i] = (array2Type)resize_size;
    *(arr+i) = (array2Type*)realloc(*(arr+i), row_lenghts[i] * sizeof(array2Type));
    for(i=0;i<arr_size_1;i++)
    {
        printf("Array %d: ", i);
        for(j = 0; j < row_lenghts[i]; j++)
        {
            *(*(arr+i) + j) = (array2Type)(i + j);
            printf("Collumn %d: val = %hu | ", j, *(*(arr+i) + j));
        }
        printf("\n");
    }

    for( i = arr_size_1-1; i>0; i--)
    {
        free(*(arr+i));
    }
    free(arr);
}

我用这个脚本运行我的代码:

运行这个.bat

gcc -o test test.c matrixTests.c
.\test

该问题仅在运行“matrixCustomRowLengthsResize”函数时出现。

尝试运行它。预计每次都能工作。第二次执行时被绞死。

编辑:修复了 main.c 中的拼写错误

c pointers memory-management freeze realloc
1个回答
0
投票

看来问题已经解决了。问题是这一行:

array2Type **arr = (array2Type**)malloc(arr_size_1 * sizeof(array2Type));

本来应该是:

array2Type **arr = malloc(arr_size_1 * sizeof(array2Type*));

当我重新分配给定行的内存时,这会在程序中引起问题,因为我会将 malloc 转换为 (array2Type*),但该内存块的大小为 (array2Type)。

*(arr+i) = (array2Type*)realloc(*(arr+i), row_lenghts[i] * sizeof(array2Type)); //here is where the discrepancy between (array2Type*) and (array2Type) would cause a problem.

由于某种原因,仅在第二次运行时才会导致不正确的行为。然而,这个问题实际上在在线 C 编译器中引发了分段错误错误,但在我的机器上却没有。

malloc 转换不是问题,但我知道它们是不必要的。

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