Can realloc fail (return NULL) when trimming?

后端 未结 7 701
误落风尘
误落风尘 2020-12-19 00:15

If do the next:

int* array = malloc(10 * sizeof(int));

and them I use realloc:

array = realloc(array, 5 * sizeof(int));


        
相关标签:
7条回答
  • 2020-12-19 01:13

    The language (and library) specification makes no such guarantee, just like it does not guarantee that a "trimming" realloc will preserve the pointer value.

    An implementation might decide to implement realloc in the most "primitive" way: by doing an unconditional malloc for a new memory block, copying the data and free-ing the old block. Obviously, such implementation can fail in low-memory situations.

    0 讨论(0)
提交回复
热议问题