free

Why doesn't free(p) set p to NULL?

ぃ、小莉子 提交于 2019-11-27 03:51:12
问题 Any reasons why this can not be standard behavior of free() ? multiple pointers pointing to the same object: #include <stdlib.h> #include <stdio.h> void safefree(void*& p) { free(p); p = NULL; } int main() { int *p = (int *)malloc(sizeof(int)); *p = 1234; int*& p2 = p; printf("p=%p p2=%p\n", p, p2); safefree((void*&)p2); printf("p=%p p2=%p\n", p, p2); safefree((void*&)p); // safe return 0; } assignment from malloc demands cast from void* vice versa: safefree() demands cast to void*&

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

Should one really set pointers to `NULL` after freeing them?

冷暖自知 提交于 2019-11-27 02:54:05
There seem to be two arguments why one should set a pointer to NULL after freeing them. Avoid crashing when double-freeing pointers. Short: Calling free() a second time, by accident, doesn't crash when it's set to NULL . Almost always this masks a logical bug because there is no reason to call free() a second time. It's safer to let the application crash and be able to fix it. It's not guaranteed to crash because sometimes new memory is allocated at the same address. Double free occurs mostly when there are two pointers pointing to the same address. Logical errors can lead to data corruption

Should I free memory before exit?

自作多情 提交于 2019-11-27 01:51:22
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); } It depends on the OS. Best practice I'd say you should explicitly free it. It also makes using tools like valgrind a PITA if you have memory not freed all over the place and I

Is freeing allocated memory needed when exiting a program in C

拈花ヽ惹草 提交于 2019-11-27 01:49:46
If I allocated memory in my C program using malloc and now I want to exit, do I have to free the allocated memory, or can I assume that since my entire program terminates, it will be freed by the OS? I run in Linux environment. Any modern operating system will clean up everything after a process terminates, but it's generally not a good practice to rely on this. It depends on the program you are writing. If it's just a command line tool that runs and terminates quickly, you may not bother cleaning up. But be aware that it is this mindset that causes memory leaks in daemons and long-running

Unable to free const pointers in C

喜你入骨 提交于 2019-11-27 00:48:12
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 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); Tim Post Your code is reversed. This: char* name="Arnold"; const char* str=(const char*)malloc(strlen(name)+1)

C: Correctly freeing memory of a multi-dimensional array

佐手、 提交于 2019-11-27 00:22:12
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-dimensional array : void freeArray(int **a, int m) { int i; for (i = 0; i < m; ++i) { free(a[i]); } free(a);

Memory not freed after calling free()

房东的猫 提交于 2019-11-26 23:41:54
问题 I have a short program that generates a linked list by adding nodes to it, then frees the memory allocated by the linked list. Valgrind does not report any memory leak errors, but the process continues to hold the allocated memory. I was only able to fix the error after I changed the memory allocated from sizeof(structure_name) to fixed number 512. (see commented code) Is this a bug or normal operation? Here is the code: #include <execinfo.h> #include <stdlib.h> #include <stdio.h> typedef

Why exactly should I not call free() on variables not allocated by malloc()?

坚强是说给别人听的谎言 提交于 2019-11-26 20:58:46
问题 I read somewhere that it is disastrous to use free to get rid of an object not created by calling malloc , is this true? why? 回答1: That's undefined behavior - never try it. Let's see what happens when you try to free() an automatic variable. The heap manager will have to deduce how to take ownership of the memory block. To do so it will either have to use some separate structure that lists all allocated blocks and that is very slow an rarely used or hope that the necessary data is located

new, delete ,malloc & free

微笑、不失礼 提交于 2019-11-26 20:26:40
问题 This question was asked to me in an interview: In C++, what if we allocate memory using malloc and use delete to free that allocated memory? what if we allocate the memory using new and free it using free ? What are the problems that we would face if the above things are used in the code? My answer was there is no difference. Was I right in saying so? 回答1: If you do so you will run into undefined behavior. Never try that. Although new might be implemented through malloc() and delete might be