realloc

c realloc, what does size really mean

我的未来我决定 提交于 2019-12-08 02:29:32
问题 Could someone explain what second parameter in realloc really is as I cant find a way to test it. So suppose that we have something like this int *p = malloc(sizeof(int)); //we have just enough space to store single int value now if I want to store 2 int values in p do I need to send to realloc as second parameter 2 * sizeof(int) new size of the block or sizeof(int) as it needs to extend memory for size of int In case I should send to realloc total value of new block in this case 2 * sizeof

Why realloc deadlock after clone syscall?

你离开我真会死。 提交于 2019-12-08 01:52:52
问题 I have a problem that realloc() deadlocks sometime after clone() syscall. My code is: #include <sched.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/syscall.h> #include <sys/types.h> #include <linux/types.h> #define CHILD_STACK_SIZE 4096*4 #define gettid() syscall(SYS_gettid) #define log(str) fprintf(stderr, "[pid:%d tid:%d] "str, getpid(),gettid()) int clone_func(void *arg){ int *ptr=(int*)malloc(10); int i; for (i=1; i<200000; i++) ptr = realloc(ptr, sizeof(int)

Why is realloc eating tons of memory?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 17:55:52
问题 This question is a bit long due the source code, which I tried to simplify as much as possible. Please bear with me and thanks for reading along. I have an application with a loop that runs potentially millions of times. Instead of several thousands to millions of malloc / free calls within that loop, I would like to do one malloc up front and then several thousands to millions of realloc calls. But I'm running into a problem where my application consumes several GB of memory and kills itself

realloc 用法

瘦欲@ 提交于 2019-12-07 17:42:26
//转至: https://blog.csdn.net/snlying/article/details/4005238 最近在写source code时需要在数组的buffer小时重新申请一块buffer,故找了一些资料,乖乖,竟然原指针还可以“漂移”。。。。。。 realloc 原型:extern void *realloc(void *mem_address, unsigned int newsize); 用法:#include <stdlib.h> 有些编译器需要#include <alloc.h> 功能:改变mem_address所指内存区域的大小为newsize长度。 说明:如果重新分配成功则返回指向被分配内存的指针,否则返回空指针NULL。 当内存不再使用时,应使用free()函数将内存块释放。 注意:这里原始内存中的数据还是保持不变的。 举例: // realloc.c #include <syslib.h> #include <alloc.h> main() { char *p; clrscr(); // clear screen p=(char *)malloc(100); if(p) printf("Memory Allocated at: %x",p); else printf("Not Enough Memory!/n"); getchar(); p=

ARC & Malloc: EXEC_BAD_ACCESS

放肆的年华 提交于 2019-12-07 05:42:26
问题 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

Can a pthread_mutex_t be moved in memory?

余生长醉 提交于 2019-12-07 03:52:19
问题 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

c语言中realloc()函数解析

匆匆过客 提交于 2019-12-06 15:00:19
真是有点惭愧,这些内容本应该很早就掌握的,以前只是糊里糊涂的用,不知道在内存中具体是怎么回事,现在才弄清楚。 realloc(void *__ptr, size_t __size):更改已经配置的内存空间,即更改由malloc()函数分配的内存空间的大小。 如果将分配的内存减少,realloc仅仅是改变索引的信息。 如果是将分配的内存扩大,则有以下情况: 1)如果当前内存段后面有需要的内存空间,则直接扩展这段内存空间,realloc()将返回原指针。 2)如果当前内存段后面的空闲字节不够,那么就使用堆中的第一个能够满足这一要求的内存块,将目前的数据复制到新的位置,并将原来的数据块释放掉,返回新的内存块位置。 3)如果申请失败,将返回NULL,此时,原来的指针仍然有效。 注意:如果调用成功,不管当前内存段后面的空闲空间是否满足要求,都会释放掉原来的指针,重新返回一个指针,虽然返回的指针有可能和原来的指针一样,即不能再次释放掉原来的指针。 看一下示例代码 #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[], char* envp[]) { int input; int n; int *numbers1; int *numbers2; numbers1=NULL; if((numbers2=(int

Is the poor performance of std::vector due to not calling realloc a logarithmic number of times?

喜夏-厌秋 提交于 2019-12-06 03:40:15
问题 EDIT: I added two more benchmarks, to compare the use of realloc with the C array and of reserve() with the std::vector. From the last analysis it seems that realloc influences a lot, even if called only 30 times. Checking the documentation I guess this is due to the fact that realloc can return a completely new pointer, copying the old one. To complete the scenario I also added the code and graph for allocating completely the array during the initialisation. The difference from reserve() is

Why is realloc eating tons of memory?

倖福魔咒の 提交于 2019-12-06 03:29:47
This question is a bit long due the source code, which I tried to simplify as much as possible. Please bear with me and thanks for reading along. I have an application with a loop that runs potentially millions of times. Instead of several thousands to millions of malloc / free calls within that loop, I would like to do one malloc up front and then several thousands to millions of realloc calls. But I'm running into a problem where my application consumes several GB of memory and kills itself, when I am using realloc . If I use malloc , my memory usage is fine. If I run on smaller test data

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

六眼飞鱼酱① 提交于 2019-12-06 01:39:35
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? 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 across ( std::copy for example), then delete[] the previous array. Just use std::vector - let it do all that