realloc

堆的分配和释放

梦想的初衷 提交于 2020-01-29 23:02:13
1、分配内存 void * malloc( size_t _Size); void * calloc( size_t _Count, size_t _Size); malloc函数在堆中分配参数_Size指定大小的内存,单位:字节,函数返回void *指针。 calloc函数分配内存的同时把内存清空。第一个参数是所需内存单元数量,第二个参数是每个内存单元的大小(单位:字 节),calloc自动将分配的内存置0。 2、内存重新分配 void * realloc( void *p, size_t _NewSize); 第一个参数 p为之前用malloc或者calloc分配的内存地址,_NewSize为重新分配内存的大小,单位:字节。成功返回新分配 的堆内存地址,失败返回NULL。 Realloc不会自动清理增加的内存,需要手动清理,如果指定的地址后面有连续的空间,那么就会在已有地址基础上增加内 存,如果指定的地址后面没有空间,那么realloc会重新分配新的连续内存,把旧内存的值拷贝到新内存,同时释放旧内存。 如果参数p等于NULL,那么realloc与malloc功能一致。 3、内存释放 void free(void *p); free负责在堆中释放malloc分配的内存。参数p为malloc返回的堆中的内存地址。 注:再释放完后,将指针置空。 参考:黑马程序员 来源: CSDN

Implementation of Realloc in C

廉价感情. 提交于 2020-01-28 10:08:16
问题 int getmin(int a, int b) { return a<b?a:b; } void *reallocation(void *ptr, size_t size) //size_t in bytes { void *newptr; int msize; msize = getsize(ptr); msize = getmin(msize, size); printf("msize = %d", msize); newptr = malloc(size); newptr = memcpy(newptr, ptr, msize); free(ptr); return newptr; } I have implemented my own realloc, and in order to get the size of the allocated memory using malloc(however i know there isn't any method for this in c). My reallocation function is working fine

realloc(): invalid next size - realloc dynamic struct

南笙酒味 提交于 2020-01-25 00:19:06
问题 I started learning about struct in C. Today I found a problem, that I can't solve. I have this code: typedef struct fraze { char *mostSearch = NULL; // for string from user double freq; } s_FRAZE; int readFraze( ) { int i = 2, k; size_t len = 0; char c; s_FRAZE *s; s = (s_FRAZE *)malloc( i * sizeof( int )); k = 0; while( (c = getchar()) != '\n') { ungetc( c, stdin ); if( scanf( "%lf%c", &s[k].freq, &c) != 2 || c != ':' ) { return 1; } if( k + 1 >= i ) { i *= 2; printf("%d\n", i ); s = (s

Dynamic Memory

风流意气都作罢 提交于 2020-01-18 21:29:28
Dynamic Memory 目录 Dynamic Memory 目录 malloc free realloc calloc 主要讨论三个函数,还有第四个函数,不过它只是其中一个的变种。 malloc(): 分配内存供使用 free(): 释放 malloc() 分配的内存 realloc(): 改变之前分配的内存的大小 calloc(): 与 malloc() 很像,区别只是它会先把分配的内存先清零 malloc() 当你使用 malloc() 时,它会分配你想要的内存。它的返回值是一个指针,指向一块内存。如果分配失败,返回 NULL。指针的类型是 void* 的,所以你可以强转成任何你想要的类型。 malloc() 操作的内存是以 byte 为单位的,如果想分配更大的内存空间(比如,”给我分配 12 个 int 的空间”),那么就要使用 sizeof() 操作符来决定你需要多少 byte 的空间,比如: int *p; p = malloc(12 * sizeof(int)); // 给我分配 12 个 int 内存的空间 分配好了之后,就可以像一般指针一样来 引用 (reference)这个指针了,毕竟,呃…它就是个指针。 当然,最好在使用它之前,校验一下它的有效性,因为可能 malloc() 会返回 NULL: int *p; p = malloc(12 * sizeof

Realloc setting pointer to empty

别说谁变了你拦得住时间么 提交于 2020-01-17 02:55:28
问题 void test(){ char *c = malloc(strlen("I like coffe") + 1); strcpy(c, "I like coffe"); char **s = &c; while(strlen(*s) < 25) my_function(s); } void my_function(char **s){ char *w = *s; char *tmp = realloc(w, len + 2);//Error HERE. *s gets = "" if(tmp != NULL) w = tmp; for(i= len; i>=p; i--){ w[i+1] = w[i]; } w[p] = c; } This function is used to insert a new character inside a char * . Also, this function is inside a while loop. It works fine but by the 3rd time the loop runs, it just sets *s =

Can the storage of trivially copyable objects be safely reallocated with realloc?

喜夏-厌秋 提交于 2020-01-14 07:48:08
问题 I know that trivially copyable objects can safely be copied my malloc into an appropriate storage location 1 and that the destination object will have the same value as the source. Is this also possible with realloc ? That is, if realloc some storage containing some objects of type T , and realloc decides to move and copy the block, will the objects in the newly allocated storage be intact and have started their lifetime, and will the lifetime of the objects in the old storage be safely ended

Reallocating memory of a C++ array. [duplicate]

做~自己de王妃 提交于 2020-01-13 13:06:25
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How do you realloc in C++? I know that C++ arrays can be reallocated (expanded) using realloc() if memory has been allocated via malloc() or calloc() . My question is, how can I expand an array in C++ whose memory has been allocated via the new operator? 回答1: You can't - that's why in C++ you use std::vector<> . If you wanted to do this, you'd have to allocate a new array (via new ), then copy the old items

Do I need to initiallize(set to 0) memory after calling realloc?

六月ゝ 毕业季﹏ 提交于 2020-01-10 05:17:08
问题 I need to implement a simple dynamic array of pointers to a typedef'ed pointer. Using realloc each time it's requested by the user, the array size will grow by sizeof(pointer). So what I have is this: #include <stdio.h> #include <string.h> #include <stdlib.h> typedef void* node; void printmem(node* a, int c) { int i; for (i = 0; i < c; ++i) { printf("%d\t\t%p\n", i, a[i]); } } int main(void) { node* nodes = NULL; int i = 0, count = 20; for (i = 0; i < count; ++i) { nodes = realloc(nodes,

Dynamic array in C — Is my understanding of malloc and realloc correct?

僤鯓⒐⒋嵵緔 提交于 2020-01-09 12:19:10
问题 I am learning how to create dynamic 1D arrays in C. The code below tries to do the following: Using malloc , create a dynamic array of length 10 , that holds values of type double . Set each entry of the array to j/100 for j = 0, 1,..., 9 . Then print it out. Add an additional empty entry to the end of the array using realloc . Set the new entry to j/100 and print out each entry again. Testing: double* data = (double*)malloc(10*sizeof(double)); for (j=0;j<10;j++) { data[j]= ((double)j)/100;

Realloc Invalid Pointer in C [closed]

馋奶兔 提交于 2020-01-07 06:27:42
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . This is a homework assignment so I don't want to post any code, but I'm pretty stumped with the bug that I have. Currently I have a array that has been malloced and am copying the pointer to the array. Now, I can