realloc

Why is there no reallocation functionality in C++ allocators?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 01:48:33
In C the standard memory handling functions are malloc() , realloc() and free() . However, C++ stdlib allocators only parallel two of them: there is no reallocation function. Of course, it would not be possible to do exactly the same as realloc() , because simply copying memory is not appropriate for non-aggregate types. But would there be a problem with, say, this function: bool reallocate (pointer ptr, size_type num_now, size_type num_requested); where ptr is previously allocated with the same allocator for num_now objects; num_requested >= num_now ; and semantics as follows: if allocator

realloc ------ 扩大malloc得到的内存空间

女生的网名这么多〃 提交于 2019-11-26 23:11:06
char* p = malloc(1024); char* q = realloc(p,2048); 现在的问题是我们应该如何处理指针 p。 刚开始按照我最直观的理解,如果就是直接将 p = NULL;。 到最后只需要释放 q的空间就可以了。 因为最近在做个封装。结果在做单元测试的时候发现。有时候我在 free(q); 的时候会出错。这样我就郁闷了。 后来仔细一跟踪,发现 realloc 完以后 q 和 p 的指针地址是一样。不过有时候又不一样。 仔细查了下资料。得到如下信息: 1.如果 当前连续内存块足够 realloc 的话,只是将p所指向的空间扩大,并返回p的指针地址。 这个时候 q 和 p 指向的地址是一样的。 2.如果 当前连续内存块不够长度,再找一个足够长的地方,分配一块新的内存,q,并将 p指向的内容 copy到 q,返回 q。并将p所指向的内存空间删除。 这样也就是说 realloc 有时候会产生一个新的内存地址 有的时候不会。所以在分配完成后。我们需要判断下 p 是否等于 q。并做相应的处理。 这里有点要注意的是要避免 p = realloc(p,2048); 这种写法。有可能会造成 realloc 分配失败后,p原先所指向的内存地址丢失。 来源: https://www.cnblogs.com/god-of-death/p/11334846.html

Does std::vector *have* to move objects when growing capacity? Or, can allocators “reallocate”?

冷暖自知 提交于 2019-11-26 20:15:33
A different question inspired the following thought: Does std::vector<T> have to move all the elements when it increases its capacity? As far as I understand, the standard behaviour is for the underlying allocator to request an entire chunk of the new size, then move all the old elements over, then destroy the old elements and then deallocate the old memory. This behaviour appears to be the only possible correct solution given the standard allocator interface. But I was wondering, would it make sense to amend the allocator to offer a reallocate(std::size_t) function which would return a pair

How do you 'realloc' in C++?

佐手、 提交于 2019-11-26 18:42:51
How can I realloc in C++? It seems to be missing from the language - there is new and delete but not resize ! I need it because as my program reads more data, I need to reallocate the buffer to hold it. I don't think delete ing the old pointer and new ing a new, bigger one, is the right option. f0b0s Use ::std::vector! Type* t = (Type*)malloc(sizeof(Type)*n) memset(t, 0, sizeof(Type)*m) becomes ::std::vector<Type> t(n, 0); Then t = (Type*)realloc(t, sizeof(Type) * n2); becomes t.resize(n2); If you want to pass pointer into function, instead of Foo(t) use Foo(&t[0]) It is absolutely correct C++

Is it safe to use realloc?

半世苍凉 提交于 2019-11-26 16:43:51
问题 Some time ago a friend of mine told me not to use realloc because it's unsafe, but he couldn't tell me why, so I made some research on the subject and the nearest references to my doubt were: https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/809-BSI.html http://www.iso-9899.info/wiki/Why_not_realloc I want to know if I can continue to use realloc in my code or if it's unsafe is there any other way to reallocate memory? Thanks for your attention. 回答1: The first of the two

Is it safe to realloc memory allocated with new?

故事扮演 提交于 2019-11-26 15:45:53
问题 From what is written here, new allocates in free store while malloc uses heap and the two terms often mean the same thing. From what is written here, realloc may move the memory block to a new location. If free store and heap are two different memory spaces, does it mean any problem then? Specifically I'd like to know if it is safe to use int* data = new int[3]; // ... int* mydata = (int*)realloc(data,6*sizeof(int)); If not, is there any other way to realloc memory allocated with new safely?

Does realloc overwrite old contents?

吃可爱长大的小学妹 提交于 2019-11-26 15:27:58
问题 When we reallocate memory via realloc() , are the previous contents over-written? I am trying to make a program which reallocates memory each time we enter the data into it. Please tell me about memory allocation via realloc, is it compiler dependent for example? 回答1: Don't worry about the old contents. The correct way to use realloc is to use a specific pointer for the reallocation, test that pointer and, if everything worked out ok, change the old pointer int *oldpointer = malloc(100); /* .

what does malloc(0) return? [duplicate]

荒凉一梦 提交于 2019-11-26 15:20:38
This question already has an answer here: what's the point in malloc(0)? 17 answers What does malloc(0) returns? Would the answer be same for realloc(malloc(0),0) ? #include<stdio.h> #include<malloc.h> int main() { printf("%p\n", malloc(0)); printf("%p\n", realloc(malloc(0), 0)); return 0; } Output from linux gcc: manav@manav-workstation:~$ gcc -Wall mal.c manav@manav-workstation:~$ ./a.out 0x9363008 (nil) manav@manav-workstation:~$ The output keep changing everytime for malloc(0) . Is this a standard answer? And why would anyone be interested in getting such a pointer, other than academic

Proper usage of realloc()

旧街凉风 提交于 2019-11-26 11:26:19
问题 From man realloc:The realloc() function returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails. So in this code snippet: ptr = (int *) malloc(sizeof(int)); ptr1 = (int *) realloc(ptr, count * sizeof(int)); if(ptr1 == NULL){ //reallocated pointer ptr1 printf(\"Exiting!!\\n\"); free(ptr); exit(0); }else{ free(ptr); //to deallocate the previous memory block pointed by ptr so as not to leave

Why is there no reallocation functionality in C++ allocators?

允我心安 提交于 2019-11-26 09:46:45
问题 In C the standard memory handling functions are malloc() , realloc() and free() . However, C++ stdlib allocators only parallel two of them: there is no reallocation function. Of course, it would not be possible to do exactly the same as realloc() , because simply copying memory is not appropriate for non-aggregate types. But would there be a problem with, say, this function: bool reallocate (pointer ptr, size_type num_now, size_type num_requested); where ptr is previously allocated with the