realloc

realloc but only first few bytes is meaningful

假如想象 提交于 2019-12-05 22:51:06
问题 Assume I have used ptr = malloc(old_size); to allocate a memory block with old_size bytes. Only the first header_size bytes is meaningful. I'm going to increase the size to new_size . new_size is greater than old_size and old_size is greater than header_size . before: /- - - - - - - old_size - - - - - - - \ +===============+---------------------+ \-header_size-/ after: /- - - - - - - - - - - - - - - new_size - - - - - - - - - - - - - - - - - - -\ +===============+-----------------------------

What are the chances that realloc should fail?

陌路散爱 提交于 2019-12-05 21:18:02
问题 Does it fail when it runs out of free memory similar to malloc or could there be other reasons? 回答1: Any of the allocation functions ( malloc , realloc , calloc , and on POSIX, posix_memalign ) could fail for any of the following reasons, and possibly others: You've used up your entire virtual address space, or at least the usable portion of it. On a 32-bit machine, there are only 4GB worth of addresses, and possibly 1GB or so is reserved for use by the OS kernel. Even if your machine has

Malloc of arrays and structs within a struct

▼魔方 西西 提交于 2019-12-05 19:24:44
问题 How does one malloc a struct which is inside another struct? I would also like to malloc an array of items inside a struct and then realloc this array when needed, how is this done correctly? Could you please give an example of declaring a struct and then the above. Im a little unsure of the order of things. Would the array within a struct be freed and then the struct itself, must the struct be malloced when it is created and then its fields be malloced/declared etc? 回答1: A struct included

C realloc

安稳与你 提交于 2019-12-05 16:28:16
https://baike.baidu.com/item/realloc/659993?fr=aladdin 也就是说:原地址后面有连续可以空间可以满足需要,则追加在后面,否则开辟新空间,并拷贝数据 来源: https://www.cnblogs.com/do-your-best/p/11933119.html

ARC & Malloc: EXEC_BAD_ACCESS

丶灬走出姿态 提交于 2019-12-05 08:48:27
I have been working on a project for some time now, and I decided to make the jump to ARC. I came across some code that was bombing out every time, and I would like to know why. I have managed to simplify it down to this snippet: typedef __strong id MYID; int main(int argc, char *argv[]) { MYID *arr = (MYID *) malloc(sizeof(MYID) * 4); arr[0] = @"A"; // always get an EXEC_BAD ACCESS HERE arr[1] = @"Test"; arr[2] = @"Array"; arr[3] = @"For"; // uh oh, we need more memory MYID *tmpArray = (MYID *) realloc(arr, sizeof(MYID) * 8); assert(tmpArray != NULL); arr = tmpArray; arr[4] = @"StackOverflow"

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

梦想的初衷 提交于 2019-12-05 08:36:25
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 fragmented, I mean fragmentation of the memory space caused by allocating and deallocating, which realloc can

Can a pthread_mutex_t be moved in memory?

送分小仙女□ 提交于 2019-12-05 07:49:12
I would like to build a dynamic malloced array of pthread_mutex that will grow over time (adding more mutexes). My question is whether they will still work if the array gets moved with realloc(). My concern is that pthread_mutex_init() might somehow set up internal information that depends on the address of the mutex at that moment. To be more specific, here is a toy snippet that shows the issue: pthread_mutex_t *my_mutexes = (pthread_mutex_t *) malloc (sizeof(pthread_mutex_t)); pthread_mutex_init (my_mutexes, NULL); my_mutexes = (pthread_mutex_t *) realloc (my_mutexes, 2*sizeof(pthread_mutex

Passing a dynamic array in to functions in C

烂漫一生 提交于 2019-12-05 07:11:18
I'm trying to create a function which takes an array as an argument, adds values to it (increasing its size if necessary) and returns the count of items. So far I have: int main(int argc, char** argv) { int mSize = 10; ent a[mSize]; int n; n = addValues(a,mSize); for(i=0;i<n;i++) { //Print values from a } } int addValues(ent *a, int mSize) { int size = mSize; i = 0; while(....) { //Loop to add items to array if(i>=size-1) { size = size*2; a = realloc(a, (size)*sizeof(ent)); } //Add to array i++; } return i; } This works if mSize is large enough to hold all the potential elements of the array,

Should I enforce realloc check if the new block size is smaller than the initial?

独自空忆成欢 提交于 2019-12-05 05:48:26
Can realloc fail in this case? int *a = NULL; a = calloc(100, sizeof(*a)); printf("1.ptr: %d\n", a); a = realloc(a, 50 * sizeof(*a)); printf("2.ptr: %d\n", a); if(a == NULL){ printf("Is it possible?\n"); } return (0); } The output in my case is: 1.ptr: 4072560 2.ptr: 4072560 So 'a' points to the same adress. So should I enforce realloc check? Later edit : Using MinGW compiler under Windows XP. Is the behaviour similar with gcc on Linux ? Later edit 2: Is it OK to check this way ? int *a = NULL, *b = NULL; a = calloc(100, sizeof(*a)); b = realloc(a, 50 * sizeof(*a)); if(b == NULL){ return a; }

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

自作多情 提交于 2019-12-05 02:53:45
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? The alignment is not kept with the pointer. When you call realloc you can only rely on the alignment that realloc guarantees. You'll need to use aligned_alloc to perform any reallocations. 来源: https://stackoverflow.com/questions/20314602