free

Should I free memory before exit?

旧街凉风 提交于 2019-11-26 09:45:34
问题 Should I free all my mallocated memory when I am exiting program in the due of error? something = (char**) malloc (x * sizeof(char*)); for (i = 0; i < x; i++) something[i] = (char*) malloc (y + 1); ... if (anything == NULL) { printf(\"Your input is wrong!\"); // should I free memory of every mallocated entity now? exit(1); } else { // work with mallocated entities ... free(something); // it must be here system(\"pause); } 回答1: It depends on the OS. Best practice I'd say you should explicitly

Unable to free const pointers in C

柔情痞子 提交于 2019-11-26 09:29:30
问题 How can I free a const char* ? I allocated new memory using malloc , and when I\'m trying to free it I always receive the error \"incompatible pointer type\" The code that causes this is something like: char* name=\"Arnold\"; const char* str=(const char*)malloc(strlen(name)+1); free(str); // error here 回答1: Several people have posted the right answer, but they keep deleting it for some reason. You need to cast it to a non-const pointer; free takes a void* , not a const void* : free((char*)str

C: Correctly freeing memory of a multi-dimensional array

那年仲夏 提交于 2019-11-26 09:20:00
问题 Say you have the following ANSI C code that initializes a multi-dimensional array : int main() { int i, m = 5, n = 20; int **a = malloc(m * sizeof(int *)); //Initialize the arrays for (i = 0; i < m; i++) { a[i]=malloc(n * sizeof(int)); } //...do something with arrays //How do I free the **a ? return 0; } After using the **a , how do I correctly free it from memory ? [Update] (Solution) Thanks to Tim\'s (and the others) answer, I can now do such a function to free up memory from my multi

Is the compiler allowed to recycle freed pointer variables?

僤鯓⒐⒋嵵緔 提交于 2019-11-26 07:43:34
问题 It has been claimed that a compiler is free to reuse the pointer variable for some other purpose after the realloc being freed , so you have no guarantee that it has the same value as it did before ie void *p = malloc(42); uintptr_t address = (uintptr_t)p; free(p); // [...] stuff unrelated to p or address assert((uintptr_t)p == address); might fail. C11 annex J.2 reads The value of a pointer that refers to space deallocated by a call to the free or realloc function is used (7.22.3) [ is

How to track down a “double free or corruption” error

安稳与你 提交于 2019-11-26 03:18:30
问题 When I run my (C++) program it crashes with this error. * glibc detected * ./load: double free or corruption (!prev): 0x0000000000c6ed50 *** How can I track down the error? I tried using print ( std::cout ) statements, without success. Could gdb make this easier? 回答1: If you're using glibc, you can set the MALLOC_CHECK_ environment variable to 2 , this will cause glibc to use an error tolerant version of malloc , which will cause your program to abort at the point where the double free is

Setting variable to NULL after free

丶灬走出姿态 提交于 2019-11-26 02:52:03
In my company there is a coding rule that says, after freeing any memory, reset the variable to NULL. For example ... void some_func () { int *nPtr; nPtr = malloc (100); free (nPtr); nPtr = NULL; return; } I feel that, in cases like the code shown above, setting to NULL does not have any meaning. Or am I missing something? If there is no meaning in such cases, I am going to take it up with the "quality team" to remove this coding rule. Please advice. Setting unused pointers to NULL is a defensive style, protecting against dangling pointer bugs. If a dangling pointer is accessed after it is

How much memory would be freed if pointer is changed in C?

最后都变了- 提交于 2019-11-26 02:39:05
问题 Let\'s say there are 20 blocks of memory and a pointer p is pointing to the first block. Now, when I do: p++; free(p); How many blocks of memory would be freed and why? 回答1: Un-deterministic. Cannot say. You need to supply the exact pointer which was returned by malloc() . Passing a pointer to free() which is not returned by malloc() [and family] is undefined behaviour. As per your question, if the malloc() -ed pointer is stored in p , and you do p++ and call free(p); , then there p is not

Setting variable to NULL after free

旧街凉风 提交于 2019-11-26 01:51:01
问题 In my company there is a coding rule that says, after freeing any memory, reset the variable to NULL. For example ... void some_func () { int *nPtr; nPtr = malloc (100); free (nPtr); nPtr = NULL; return; } I feel that, in cases like the code shown above, setting to NULL does not have any meaning. Or am I missing something? If there is no meaning in such cases, I am going to take it up with the \"quality team\" to remove this coding rule. Please advice. 回答1: Setting unused pointers to NULL is

How do free and malloc work in C?

痞子三分冷 提交于 2019-11-26 00:52:18
问题 I\'m trying to figure out what would happened if I try to free a pointer \"from the middle\" for example, look at the following code: char *ptr = (char*)malloc(10*sizeof(char)); for (char i=0 ; i<10 ; ++i) { ptr[i] = i+10; } ++ptr; ++ptr; ++ptr; ++ptr; free(ptr); I get a crash with an Unhandled exception error msg. I want to understand why and how free works so that I know not only how to use it but also be able to understand weird errors and exceptions and better debug my codeץ Thanks a lot

Will malloc implementations return free-ed memory back to the system?

不羁的心 提交于 2019-11-26 00:43:38
问题 I have a long-living application with frequent memory allocation-deallocation. Will any malloc implementation return freed memory back to the system? What is, in this respect, the behavior of: ptmalloc 1, 2 (glibc default) or 3 dlmalloc tcmalloc (google threaded malloc) solaris 10-11 default malloc and mtmalloc FreeBSD 8 default malloc (jemalloc) Hoard malloc? Update If I have an application whose memory consumption can be very different in daytime and nighttime (e.g.), can I force any of