realloc

What header should I include for memcpy and realloc?

早过忘川 提交于 2019-11-27 07:42:59
问题 I am porting a project to the iPhone and it uses realloc and memcpy which are not found. What is the header to include? It's a project mixing Objective C and C++ and I am starting to be lost. Thanks in advance for your help! 回答1: In C: #include <string.h> // memcpy #include <stdlib.h> //realloc In C++, remove the .h and prefix with a c . In C++, they will be placed in the std namespace, but are also global. 回答2: In C++ it's more idiomatic to use std::copy than C's memcpy , although the latter

realloc() dangling pointers and undefined behavior

泪湿孤枕 提交于 2019-11-27 07:33:11
问题 When you free memory, what happens to pointers that point into that memory? Do they become invalid immediately? What happens if they later become valid again? Certainly, the usual case of a pointer going invalid then becoming "valid" again would be some other object getting allocated into what happens to be the memory that was used before, and if you use the pointer to access memory, that's obviously undefined behavior. Dangling pointer memory overwrite lesson 1, pretty much. But what if the

Correct use of Realloc

穿精又带淫゛_ 提交于 2019-11-27 07:21:02
问题 This is the way I've been taught to use realloc() : int *a = malloc(10); a = realloc(a, 100); // Why do we do "a = .... ?" if(a == NULL) //Deal with problem..... Isn't that redundant? Can't i just do something like this? : if(realloc(a, 100) == NULL) //Deal with the problem Same for other realloc examples i've found around, for example: int *oldPtr = malloc(10); int * newPtr = realloc(oldPtr, 100); if(newPtr == NULL) //deal with problems else oldPtr = newPtr; Can't i just do this instead? :

realloc(): invalid next size when reallocating to make space for strcat on char * [duplicate]

本秂侑毒 提交于 2019-11-27 06:41:08
问题 This question already has answers here : Facing an error “*** glibc detected *** free(): invalid next size (fast)” (2 answers) Closed 5 years ago . I am getting invalid memory error on following code: printf(" %s\n","FINE 5"); printf("%s LENGTH IS: %d\n","FINE 6",strlen(": ")); buffer = (char *)realloc(buffer, strlen(buffer)* sizeof(char) + (strlen(": ")+1)* sizeof(char)); printf(" %s\n","FINE 7"); strcat(buffer, ": \0"); Output: FINE 5 FINE 6 LENGTH IS: 2 * glibc detected * ./auto: realloc()

Freeing allocated memory: realloc() vs. free()

感情迁移 提交于 2019-11-27 05:57:02
问题 so I have a piece of memory allocated with malloc() and changed later with realloc() . At some point in my code I want to empty it, by this I mean essentially give it memory of 0. Something which would intuitively be done with realloc(pointer,0) . I have read on here that this is implementation defined and should not be used. Should I instead use free() , and then do another malloc() ? 回答1: It depends on what you mean: if you want to empty the memory used , but still have access to that

Using realloc to shrink the allocated memory

荒凉一梦 提交于 2019-11-27 05:16:58
问题 Simple question about the realloc function in C: If I use realloc to shrink the memory block that a pointer is pointing to, does the "extra" memory get freed? Or does it need to be freed manually somehow? For example, if I do int *myPointer = malloc(100*sizeof(int)); myPointer = realloc(myPointer,50*sizeof(int)); free(myPointer); Will I have a memory leak? 回答1: No, you won't have a memory leak. realloc will simply mark the rest "available" for future malloc operations. But you still have to

Proper usage of realloc()

落爺英雄遲暮 提交于 2019-11-27 04:59:58
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 orphaned blocks of memory when ptr=ptr1 executes and ptr moves on to another block ptr = ptr1; /

Is it true, that modern OS may skip copy when realloc is called

爷,独闯天下 提交于 2019-11-27 04:29:08
问题 While reading the https://stackoverflow.com/a/3190489/196561 I have a question. What the Qt authors says in the Inside the Qt 4 Containers: ... QVector uses realloc() to grow by increments of 4096 bytes. This makes sense because modern operating systems don't copy the entire data when reallocating a buffer; the physical memory pages are simply reordered, and only the data on the first and last pages need to be copied. My questions are: 1) Is it true that modern OS ( Linux - the most

Using realloc (X, 0) instead of free() and using malloc with length of a string +1

我的梦境 提交于 2019-11-27 03:31:30
问题 So I don't really know how to put the title this time. First of all I'd like to say that I've seen several comments on this page about warning if the question is related to "homework". Mine is, but it's also completed and I just want to further understand what is going on with the code. I have also read posts and books for some time, but I think I am still missing things. I have 2 lines of code I don't quite understand in the code I worked with. The work is about getting whatever file is used

How to handle realloc when it fails due to memory?

痞子三分冷 提交于 2019-11-27 01:49:52
Question says it all but here is an example: typedef struct mutable_t{ int count, max; void **data; } mutable_t; void pushMutable(mutable_t *m, void *object) { if(m->count == m->max){ m->max *= 2; m->data = realloc(m->data, m->max * sizeof(void*)); } // how to handle oom?? m->data[m->count++] = object; } How can I handle running out of memory and not NULL out all of my data? edit - let's assume there is something which could be done e.g. free up some memory somewhere or at least tell the user "you can't do that - you're out of memory". Ideally I would like to leave what was allocated there.