realloc

动态内存分配与指向它的指针变量

谁说我不能喝 提交于 2020-01-04 05:49:44
1.动态内存分配的含义   c语言允许建立动态内存分配区域,以存放一些临时用的数据,这些数据不必再程序的声明部分定义,也不必等到函数结束时才释放,而是要随时开辟,不需要随时释放,这些数据是临时存放在一个特定的自由存储区(堆),可以根据需要向系统申请所需要大小的空间,由于未在声明部分定义它们为变量或数组,因此不能通过变量名或数组名去引用这些数据,只能通过指针来引用。 2.建立内存的动态分配   对内存的动态分配是通过系统提供的函数库来实现的,主要有malloc,calloc,free,realloc这四个函数: (1).使用malloc函数   其函数原型为void *malloc(unsigned int size);   其作用是在内存的动态存储区域中分配一个长度为size的连续空间,形参size的类型定义为无符号整形(不允许为负数)。次函数的值(即返回值)是所分配区域的第一个字节的地址,或者说,次函数是一个指针型函数,返回的指针指向该分配区域的开头位置,如:   malloc(100)//开辟100字节的临时分配区域,函数值为其第一个字节的地址   注意其指针的基类型为void,即不能执行任何类型的数据,只能提供一个地址,如果此函数未能成功执行(如内存空间不足),则返回空指针。 (2).使用calloc函数   其函数原型为void *calloc(unsigned n,

Realloc an array of Structs

半世苍凉 提交于 2020-01-02 09:54:27
问题 I am trying to dynamically reallocate memory for an array of structs (actually an array each of 2 structs but 1 included here for simplicity) that is being read from/to a file or inputted by the user. typedef Struct { char surname[21]; char firstname[21]; char username[21]; ... } User; ...in main(): int size = 0; /* stores no. of structs */ User* user_array = (User *) calloc(1, sizeof(User)); if(user_array == NULL) { printf("Cannot allocate initial memory for data\n"); exit(1); } else size++;

Realloc an array of Structs

流过昼夜 提交于 2020-01-02 09:53:34
问题 I am trying to dynamically reallocate memory for an array of structs (actually an array each of 2 structs but 1 included here for simplicity) that is being read from/to a file or inputted by the user. typedef Struct { char surname[21]; char firstname[21]; char username[21]; ... } User; ...in main(): int size = 0; /* stores no. of structs */ User* user_array = (User *) calloc(1, sizeof(User)); if(user_array == NULL) { printf("Cannot allocate initial memory for data\n"); exit(1); } else size++;

Is it fair to always recommend std::vector over realloc?

别来无恙 提交于 2020-01-02 04:05:13
问题 From Bjarne Stroustrup's FAQ: If you feel the need for realloc() - and many do - then consider using a standard library vector. I'll preface my question by agreeing that std::vector is better for many reasons, and I personally would always choose to use it over writing my own dynamic arrays with C memory allocation. But , std::vector fragments memory as it grows because C++ has no equivalent of realloc ( edit To clarify, I know that std::vector 's storage is contiguous and won't get

Does realloc of memory allocated by C11 aligned_alloc keep the alignment?

不羁的心 提交于 2020-01-02 01:31:05
问题 Consider the following (C11) code: void *ptr = aligned_alloc(4096, 4096); ... // do something with 'ptr' ptr = realloc(ptr, 6000); Since the memory that ptr points to has a 4096-byte alignment from aligned_alloc , will it (read: is it guaranteed to) keep that alignment after a (successful) call to realloc ? Or could the memory revert to the default alignment? 回答1: The alignment is not kept with the pointer. When you call realloc you can only rely on the alignment that realloc guarantees. You

git out of memory on checkout

帅比萌擦擦* 提交于 2020-01-02 01:02:38
问题 I have cloned a large repo and got an error (after several attempts) Clone succeeded, but checkout failed When trying to fix this with git checkout -f HEAD an error comes back Fatal: Out of memory, realloc failed2 I've already set some memory limits higher because the cloning also ran into problems by setting git config pack.WindowMemory 256m && git config pack.packSizelimit 256m Based on advice below from Punit Vara (below) I've also edited the .git/config to: [core] packedGitLimit = 128m

realloc structure with function in C

与世无争的帅哥 提交于 2019-12-31 07:21:09
问题 My C program is crashing and I am too new to figure it out. It's very simple so far and I imagine the code is enough to figure out what is going wrong. I am simply trying to read a file line by line. I will double the memory of a structure once I am out of memory. If this is not enough information, I will give whatever else you need. Thank you very much for any help, as I have been stuck for hours now. /* John Maynard 1000916794 7/15/2013 HW-06 */ #include <stdio.h> #include <stdlib.h>

malloc() of struct array with varying size structs

北城以北 提交于 2019-12-30 05:28:10
问题 How does one malloc an array of structs correctly if each struct contains an array of strings which vary in size? So each struct might have a different size and would make it impossible to realloc(numberOfStructs * sizeof(structName)) after malloc(initialSize * sizeof(structName) How does one allocate memory for this and keep track of what is going on? 回答1: I am making some guesses here, based on the information you have provided. The only reason I can see for wanting to realloc an array of

Can I assume that calling realloc with a smaller size will free the remainder?

冷暖自知 提交于 2019-12-29 03:56:10
问题 Let’s consider this very short snippet of code: #include <stdlib.h> int main() { char* a = malloc(20000); char* b = realloc(a, 5); free(b); return 0; } After reading the man page for realloc, I was not entirely sure that the second line would cause the 19995 extra bytes to be freed. To quote the man page: The realloc() function changes the size of the memory block pointed to by ptr to size bytes. , but from that definition, can I be sure the rest will be freed? I mean, the block pointed by b

Does realloc free the former buffer if it fails?

≯℡__Kan透↙ 提交于 2019-12-28 05:49:05
问题 If realloc fails and returns NULL is the former buffer free'd or it is kept intact? I didn't found that particular piece of information in the man page and I'm quite unsure what to do. If memory is freed then double-free could be risky. If not then the leakage would occur. 回答1: No, it does not. That aspect has often annoyed me since you can't just use: if ((buff = realloc (buff, newsize)) == NULL) return; in your code if you want to free the original on failure. Instead you have to do