在C.内存泄漏中调整二维数组的大小

在C.内存泄漏中调整二维数组的大小,c,dynamic-memory-allocation,realloc,alloc,C,Dynamic Memory Allocation,Realloc,Alloc,所以我有一个二维动态数组,我必须调整它的大小。在我调整它的大小后,编译器说它不能通过如下格式访问内存:array[I][j] void resize(int **array,int newsize,int size){ int newarraysize=WINDOWY/newsize; int arraysize=WINDOWY/size; array= (int**)realloc(array,(newarraysize*sizeof(int*))); if(

所以我有一个二维动态数组,我必须调整它的大小。在我调整它的大小后,编译器说它不能通过如下格式访问内存:array[I][j]

void resize(int **array,int newsize,int size){
    int newarraysize=WINDOWY/newsize;
    int arraysize=WINDOWY/size;
    array= (int**)realloc(array,(newarraysize*sizeof(int*)));

    if(newarraysize>arraysize){
        for(int i=0; i<arraysize; i++)
         array[i]=(int*)realloc(tomb[i],(newarraysize*sizeof(int)));
    }
    else{
        for(int i=0; i<newarraysize; i++)
         array[i]=(int*)realloc(tomb[i],(newarraysize*sizeof(int)));
    }
    printf("\n");
    for(int i=0;i<newarraysize;i++)
        {
            for(int j=0;j<newarraysize;j++)
            printf("[%d][%d]: %p ",i,j,&array[i][j]);
            printf("\n");
        }
}
void resize(int**array,int newsize,int size){
int newarraysize=WINDOWY/newsize;
int arraysize=窗口Y/大小;
数组=(int**)realloc(数组,(newarraysize*sizeof(int*));
if(newarraysize>arraysize){

对于(int i=0;i如果newsize较小,则必须释放从newsize到size的元素。
然后可以重新分配阵列。
如果newsize更大,则从size到newsize的元素需要设置为NULL。
然后从元素0重新分配到新闻大小。
对于指向指针的指针,可以返回并分配指针。另一个选项是传递指向指针的指针
int***array

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

int **resize ( int **array, int *newsize, int size){
    int **temp = NULL;

    if ( *newsize < size) {
        for ( int i = *newsize; i < size; i++) {
            printf ( "free[%d]\n", i);
            free ( array[i]);
        }
    }

    if ( NULL == ( temp = realloc ( array, *newsize * sizeof *array))) {
        if ( 0 != *newsize) {
            fprintf ( stderr, "realloc problem\n");
            *newsize = size;
            return array;
        }
        else {
            return NULL;
        }
    }
    array = temp;

    for ( int i = size; i < *newsize; i++) {
        array[i] = NULL;
    }

    for ( int i = 0; i < *newsize; i++) {
         array[i] = realloc ( array[i], *newsize * sizeof **array);
    }
    printf("\n");
    for ( int i = 0; i < *newsize; i++) {
        for ( int j = 0; j < *newsize; j++) {
            printf ( "[%d][%d]: %p\n", i, j, (void *)&array[i][j]);
        }
    }
    return array;
}

int main ( void) {
    char line[6] = "";
    int **items = NULL;
    int elements = 0;
    int oldsize = 0;

    printf ( "enter a number\n");
    while ( fgets ( line, sizeof line, stdin)) {
        if ( 1 == sscanf ( line, "%d", &elements)) {
            if ( 0 <= elements) {
                items = resize ( items, &elements, oldsize);
                oldsize = elements;
            }
            if ( 0 == elements) {
                free ( items);
                break;
            }
        }
        else {
            printf ( "enter a number\n");
        }
    }

    return 0;
}
#包括
#包括
整数**调整大小(整数**数组,整数*新闻大小,整数大小){
int**temp=NULL;
如果(*新闻大小<大小){
for(inti=*newsize;i如果(0如果newsize较小,则必须释放从newsize到size的元素。
然后可以重新分配阵列。
如果newsize更大,则从size到newsize的元素需要设置为NULL。
然后从元素0重新分配到新闻大小。
对于指向指针的指针,可以返回并分配指针。另一个选项是传递指向指针的指针
int***array

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

int **resize ( int **array, int *newsize, int size){
    int **temp = NULL;

    if ( *newsize < size) {
        for ( int i = *newsize; i < size; i++) {
            printf ( "free[%d]\n", i);
            free ( array[i]);
        }
    }

    if ( NULL == ( temp = realloc ( array, *newsize * sizeof *array))) {
        if ( 0 != *newsize) {
            fprintf ( stderr, "realloc problem\n");
            *newsize = size;
            return array;
        }
        else {
            return NULL;
        }
    }
    array = temp;

    for ( int i = size; i < *newsize; i++) {
        array[i] = NULL;
    }

    for ( int i = 0; i < *newsize; i++) {
         array[i] = realloc ( array[i], *newsize * sizeof **array);
    }
    printf("\n");
    for ( int i = 0; i < *newsize; i++) {
        for ( int j = 0; j < *newsize; j++) {
            printf ( "[%d][%d]: %p\n", i, j, (void *)&array[i][j]);
        }
    }
    return array;
}

int main ( void) {
    char line[6] = "";
    int **items = NULL;
    int elements = 0;
    int oldsize = 0;

    printf ( "enter a number\n");
    while ( fgets ( line, sizeof line, stdin)) {
        if ( 1 == sscanf ( line, "%d", &elements)) {
            if ( 0 <= elements) {
                items = resize ( items, &elements, oldsize);
                oldsize = elements;
            }
            if ( 0 == elements) {
                free ( items);
                break;
            }
        }
        else {
            printf ( "enter a number\n");
        }
    }

    return 0;
}
#包括
#包括
整数**调整大小(整数**数组,整数*新闻大小,整数大小){
int**temp=NULL;
如果(*新闻大小<大小){
for(inti=*newsize;i如果(0)这回答了你的问题吗?请出示一个@user3121023我试过了,但没有用。它做了同样的事情。哦,是的,对不起。我忘了翻译那个。坟墓的意思是“数组”.这回答了你的问题吗?请出示一个@user3121023我试过了,但没有用。它做了同样的事情。哦,是的,对不起。我忘了翻译那个。坟墓的意思是“数组”。哇谢谢。我会学习这个方法,你救了我的命。哇谢谢。我会学习这个方法,你救了我的命。