realloc

Is it good coding practice to assign the address returned by realloc() to the same pointer?

谁说胖子不能爱 提交于 2020-06-23 04:18:24
问题 I saw some code related to realloc() on some sites as below. int *p = (int *)malloc(sizeof(int) * 10); p = (int *)realloc(p, 100); But as the standard says, if realloc fails, the original block is left untouched and it returns NULL. So if realloc fails, from above example, we will lose the ability to free p. Can any one please let me know is it good coding practice to assign the address returned by realloc() to the same pointer? 回答1: You are correct that directly assigning the return value of

How to realloc some memory allocated using calloc?

百般思念 提交于 2020-05-16 10:41:11
问题 I've allocated a string with the calloc function: //string1 and string2 previously declared char *stringClone = calloc(strlen(string1) + 1, sizeof(char)); Now I want to do the same thing on stringClone with a different string. Doing: stringClone = calloc(strlen(string2) + 1, sizeof(char)); I'm gonna have some memory leak, right? How should I use the realloc in this case? 回答1: You can use realloc() to reallocate memory allocated by malloc() , calloc() , realloc() , aligned_alloc() or strdup()

How to realloc some memory allocated using calloc?

点点圈 提交于 2020-05-16 10:38:15
问题 I've allocated a string with the calloc function: //string1 and string2 previously declared char *stringClone = calloc(strlen(string1) + 1, sizeof(char)); Now I want to do the same thing on stringClone with a different string. Doing: stringClone = calloc(strlen(string2) + 1, sizeof(char)); I'm gonna have some memory leak, right? How should I use the realloc in this case? 回答1: You can use realloc() to reallocate memory allocated by malloc() , calloc() , realloc() , aligned_alloc() or strdup()

Is there really no version of realloc() supporting alignment?

南笙酒味 提交于 2020-05-10 07:08:06
问题 There exist several aligned versions of the venerable malloc() , e.g.: #include <stdlib.h> int posix_memalign(void **memptr, size_t alignment, size_t size); void *aligned_alloc(size_t alignment, size_t size); #include <malloc.h> void *memalign(size_t alignment, size_t size); (originating in POSIX, glibc and Linux libc respectively). But - I can't seem to find any mention of a version of realloc() which supports alignment. Has it really never been implemented? It seems pretty trivial to

Is there really no version of realloc() supporting alignment?

*爱你&永不变心* 提交于 2020-05-10 07:07:26
问题 There exist several aligned versions of the venerable malloc() , e.g.: #include <stdlib.h> int posix_memalign(void **memptr, size_t alignment, size_t size); void *aligned_alloc(size_t alignment, size_t size); #include <malloc.h> void *memalign(size_t alignment, size_t size); (originating in POSIX, glibc and Linux libc respectively). But - I can't seem to find any mention of a version of realloc() which supports alignment. Has it really never been implemented? It seems pretty trivial to

Is there really no version of realloc() supporting alignment?

心不动则不痛 提交于 2020-05-10 07:06:26
问题 There exist several aligned versions of the venerable malloc() , e.g.: #include <stdlib.h> int posix_memalign(void **memptr, size_t alignment, size_t size); void *aligned_alloc(size_t alignment, size_t size); #include <malloc.h> void *memalign(size_t alignment, size_t size); (originating in POSIX, glibc and Linux libc respectively). But - I can't seem to find any mention of a version of realloc() which supports alignment. Has it really never been implemented? It seems pretty trivial to

Both dynamic column and row sized 2D array updated in recursive function in c programming

ぃ、小莉子 提交于 2020-04-16 08:34:29
问题 I am growing a 2D array by it's column and row size in c programming. #include <stdio.h> #include <malloc.h> //#include <conio.h> //#include <stdlib.h> void func(int** p, int d, int** sizes) { static int count = 0; int* item = NULL; int* temp = NULL; if (d == 5) return; *sizes = (int*) realloc(*sizes, (d + 1) * sizeof(*sizes)); *sizes[count] = d + 1; // <=== in second recursive call it throws memory allocation errors here in runtime //p = (int**) realloc(p, (count + 1) * sizeof(int*)); /

内存管理之堆

依然范特西╮ 提交于 2020-03-28 05:56:28
内存管理之堆 什么是堆 堆(heap)是一种内存管理方式。内存管理对操作系统来说是一件非常复杂的事情,因为首先内存容量很大,其次内存需求在时间和大小块上没有规律(操作系统上运行着的几十、几百、几千个进程随时都会电请或者释放内存,申请或者释放的内存块大小随意) 堆这种内存管理方式特点就是自由(随时申请、释放、大小块随意)。堆内存是操作系统划归给堆管理器(操作系统中的一段代码,属于操作系统的内存管理单元)来管理的,然后向使用者(用户进程)提供API (malloc和free)来使用堆内存。 我们什么时候使用堆内存?需要内存容量比较大时,需要反复使用及释放时,很多数据结构(譬如链表)的实现都要使用堆内存。 堆管理内存的特点 (大块内存、手工分配&使用&释放) 特点一容量不限(常规使用的需求容量都能满足) 特点二:申请及释放都需要手工进行,手工进行的含义就是需要程序员写代码明确进行申请malloc及释放free。如果程序员申请内存并使用后未释放,这段内存就丢失了(在堆管理器的记录中,这段内存仍然属于你这个进程,但是进程自己又以为这段内存已经不用了,再用的时候又会去申请新的内存块,这就叫吃内存),称为内存泄漏。在C/C++语言中,内存泄漏是最严重的程序bug,这也是别人认为Java/C#等语言比c/C+ +优秀的地方。 c语言操作堆内存的接口 (malloc free) 堆内存释放时最简单

malloc,calloc,realloc和new的比较

故事扮演 提交于 2020-03-10 08:55:49
malloc与calloc,realloc的区别 malloc 与 calloc,realloc 都可以用来动态分配内存空间,但两者也存在着一些区别: 1. Malloc 函数有一个参数,即要分配的内存空间的大小 2. calloc 有两个参数,分别为元素的数目和每个元素的大小,这两个参数的乘积就是要分配的内存空间的大小 3. 如果分配内存空间成功,则两者都返回分配的内存空间的首地址函数的程序开始 4. malloc 不能初始化所分配的内存空间,而 calloc 能 5. 由于 malloc 分配的内存空间原来没有被使用过,则其中每一位可能都是零,反之,如果这部分内存曾经被使用过,在其中可能留有各种各样的数据,也就是说,使用 malloc 时(内存空间还没有被重新分配)能正常运行,但经过一段时间 ( 内存空间已经重新分配 ) 可能会出现问题 6. 函数 calloc 将分配的内存空间中的每一位初始化为 0 ,也就是说如果你为字符型或数据类型的元素分配内存空间,那么这些元素将被初始化为 0 ,如果你是为指针类型 的元素分配内存空间,那么这些元素会被初始化为空指针,如果你为实型数据分配内存,这些元素会被初始化为浮点型的 0。 7. Realloc 是给一个已经分配了内存的地址指针重新分配空间,参数包括原因的空间地址和重新申请的地址长度 Malloc 与 new 的区别 1.

8.动态内存管理malloc calloc realloc free

半世苍凉 提交于 2020-03-01 21:24:36
malloc 描述 C 库函数 void *malloc(size_t size) 分配所需的内存空间,并返回一个指向它的指针。 声明 下面是 malloc() 函数的声明。 void *malloc(size_t size) 参数 size – 内存块的大小,以字节为单位。 返回值 该函数返回一个指针 ,指向已分配大小的内存。如果请求失败,则返回 NULL。 realloc 描述 C 库函数 void *realloc(void *ptr, size_t size) 尝试重新调整之前调用 malloc 或 calloc 所分配的 ptr 所指向的内存块的大小。 声明 下面是 realloc() 函数的声明。 void *realloc(void *ptr, size_t size) 参数 ptr – 指针指向一个要重新分配内存的内存块,该内存块之前是通过调用 malloc、calloc 或 realloc 进行分配内存的。如果为空指针,则会分配一个新的内存块,且函数返回一个指向它的指针。 size – 内存块的新的大小,以字节为单位。如果大小为 0,且 ptr 指向一个已存在的内存块,则 ptr 所指向的内存块会被释放,并返回一个空指针。 返回值 该函数返回一个指针 ,指向重新分配大小的内存。如果请求失败,则返回 NULL。 calloc 描述 C 库函数 void