How to use realloc in a function in C

后端 未结 3 1447
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 06:34

Building on what I learned here: Manipulating dynamic array through functions in C.

void test(int data[])
{
    data[0] = 1;    
}    

int main(void)
{    
         


        
3条回答
  •  迷失自我
    2020-11-29 06:46

    Both code are very problematic, if you use the same pointer to send and receive from realloc, if it fails, you will lose your pointer to free it later.

    you should do some thing like this :

    { ... ...

    more = realloc(area , size);
    if( more == NULL )
        free(area);
    else
        area=more;
    

    ... ...

    }

提交回复
热议问题