简体   繁体   English

c中的动态内存分配,释放在使用malloc()之前分配的内存的一部分

[英]dynamic memory allocation in c , free some part of memory that is allocated before using malloc()

Is there any way to free some part of memory you created by using malloc(); 有没有办法释放你使用malloc()创建的内存的一部分;

suppose:- 假设:-

int *temp;

temp = ( int *) malloc ( 10 * sizeof(int));
free(temp);

free() will release all 20 byte of memory but suppose i only need 10 bytes. free()将释放所有20字节的内存,但假设我只需要10个字节。 Can i free last 10 bytes. 我可以释放最后10个字节。

You should use the standard library function realloc . 您应该使用标准库函数realloc As the name suggests, it reallocates a block of memory. 顾名思义,它会重新分配一块内存。 Its prototype is (contained in the header stdlib.h ) 它的原型是(包含在标题stdlib.h

 void *realloc(void *ptr, size_t size);

The function changes the size of the memory block pointed to by ptr to size bytes. 该函数将ptr指向的内存块的大小更改为size字节。 This memory block must have been allocated by a malloc , realloc or calloc call. 此内存块必须已由mallocrealloccalloc调用分配。 It is important to note that realloc may extend the older block to size bytes, may keep the same block and free the extra bytes, or may allocate an entirely new block of memory, copy the content from the older block to the newer block, and then free the older block. 重要的是要注意realloc可以将旧块扩展为size字节,可以保持相同的块并释放额外的字节,或者可以分配一个全新的内存块,将内容从较旧的块复制到较新的块,以及然后free旧块。

realloc returns a pointer to the block of reallocated memory. realloc返回指向重新分配的内存块的指针。 If it fails to reallocate memory, then it returns NULL and the original block of memory is left untouched. 如果它无法重新分配内存,则它返回NULL并保持原始内存块不受影响。 Therefore, you should store the value of ptr in a temp variable before calling realloc else original memory block will be lost and cause memory leak. 因此,您应该在调用realloc之前将ptr的值存储在temp变量中,否则原始内存块将丢失并导致内存泄漏。 Also, you should not cast the result of malloc - Do I cast the result of malloc? 另外,你不应该转换malloc的结果 - 我是否转换了malloc的结果?

// allocate memory for 10 integers
int *arr = malloc(10 * sizeof *arr);
// check arr for NULL in case malloc fails

// save the value of arr in temp in case
// realloc fails
int *temp = arr;  

// realloc may keep the same block of memory
// and free the memory for the extra 5 elements
// or may allocate a new block for 5 elements,
// copy the first five elements from the older block to the
// newer block and then free the older block
arr = realloc(arr, 5 * sizeof *arr);
if(arr == NULL) {
    // realloc failed
    arr = temp;
}

You can use realloc function provided by the standard c library. 您可以使用标准c库提供的realloc功能。

C99 Standard 7.20.3.4-1: The realloc function: C99标准7.20.3.4-1:重新分配功能:

The realloc function deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size . realloc函数释放ptr指向的旧对象,并返回指向具有size指定大小的新对象的指针 The contents of the new object shall be the same as that of the old object prior to deallocation, up to the lesser of the new and old sizes. 新对象的内容应与解除分配之前的旧对象的内容相同,直到新旧大小中的较小者为止。 Any bytes in the new object beyond the size of the old object have indeterminate values. 新对象中超出旧对象大小的任何字节都具有不确定的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM