free

Returning dynamically allocated memory back to OS without terminating the program

坚强是说给别人听的谎言 提交于 2019-12-01 20:29:53
I am working on a program where I am using large but limited amount of memory. Memory is allocated and freed on run time on different threads. However, I noticed that the memory usage of the program would not remain within specified bounds. It would increase as time passed on. I wrote the following sample program to check whether memory is being freed back to the OS. Half the allocated memory was freed to check if the memory usage went down. int main() { char *p[COUNT]; for(int i = 0; i < COUNT; i++) { p[i] = new char[1048576]; memset (p[i], 0, 1048576); printf("%p\n", p[i]); } printf("done

free() not freeing up memory properly?

落爺英雄遲暮 提交于 2019-12-01 19:08:33
I'm trying to free up the memory I've allocated with malloc , but the free command doesn't seem to do its job properly according to Eclipse's debugger. How's this possible? Below is a screenshot of my debugger after it supposedly freed up seCurrent->student->year , which is clearly not the case. year was allocated using malloc . alt text http://img693.imageshack.us/img693/7840/codeo.png free() does not normally change any values in your program - it just makes adjustments to the C runtime heap. This means that the values in the memory that was just freed are retained. However, attempts to

Does freeing an int* which was assigned to a char* (allocated by `malloc`) invoke Undefined Behavior?

白昼怎懂夜的黑 提交于 2019-12-01 17:26:58
The title maybe confusing. Suppose str is a pointer allocated by malloc . ptr , of type int* , is assigned to it and is freed as shown by the code snippet below: char* str = malloc(64); int* ptr = str; free(ptr); I've tried to compile the above code. It just gives a warning: source_file.c: In function ‘main’: source_file.c:10:16: warning: initialization from incompatible pointer type int* ptr = str; ^ Does the above code invoke Undefined Behavior? Does the above code snippet free the memory allocated by malloc for str ? alk Does the above code invoke Undefined Behavior? It depends. From C11

C free() routine and incremented array pointers

主宰稳场 提交于 2019-12-01 17:04:40
Will the free() routine work if I dynamically allocate an array and then pass, not the initial pointer, but a pointer to the middle of the array? Example: int* array = malloc(10 * sizeof *array); if(array) { array += 5; // adjusting the indicies free(array); } Or do I need to set the pointer back to the start of the array before calling free() ? Absolutely not. The value passed to free() must be exactly the same value returned by malloc() . In fact, to ensure this is the case, I would recommend you use a copy of the pointer if you need a pointer you can increment or otherwise modify. No (And

Does freeing an int* which was assigned to a char* (allocated by `malloc`) invoke Undefined Behavior?

断了今生、忘了曾经 提交于 2019-12-01 16:57:55
问题 The title maybe confusing. Suppose str is a pointer allocated by malloc . ptr , of type int* , is assigned to it and is freed as shown by the code snippet below: char* str = malloc(64); int* ptr = str; free(ptr); I've tried to compile the above code. It just gives a warning: source_file.c: In function ‘main’: source_file.c:10:16: warning: initialization from incompatible pointer type int* ptr = str; ^ Does the above code invoke Undefined Behavior? Does the above code snippet free the memory

C free() routine and incremented array pointers

六月ゝ 毕业季﹏ 提交于 2019-12-01 16:39:51
问题 Will the free() routine work if I dynamically allocate an array and then pass, not the initial pointer, but a pointer to the middle of the array? Example: int* array = malloc(10 * sizeof *array); if(array) { array += 5; // adjusting the indicies free(array); } Or do I need to set the pointer back to the start of the array before calling free() ? 回答1: Absolutely not. The value passed to free() must be exactly the same value returned by malloc() . In fact, to ensure this is the case, I would

multi dimensional char array?

匆匆过客 提交于 2019-12-01 14:58:40
What I am trying to do is create an 2-d array of character strings The following seg faults instantly, what is wrong? void add2(char***b, char *i) { if (!i) { b[0][0] = (char*) malloc(120); sprintf(b[0][0], "%s", "hithere"); b[0][1] = (char*) malloc(120); sprintf(b[0][1], "%s", "bithere"); } else { strcat(b[0][0], "\\\\"); strcat(b[0][0], i); strcat(b[0][1], "\\\\"); strcat(b[0][1], i); } } void add1(char ***b) { add2(b,NULL); add2(b,"one"); add2(b,"two"); add2(b,"three"); } int main() { char **keys[2] = {0}; int i,j; add1(keys); for (i = 0; keys[i]; i++) for (j = 0; keys[j]; j++) { fprintf

how to freeing pointers using macro in c

你。 提交于 2019-12-01 10:11:36
问题 I want to write macro in c code to freeing many pointers like this : FREE(ptr1, ptr2, ptr3, ptr4, ptrx); For me, this is better than FREE(ptr1); FREE(ptr2); FREE(ptr3); FREE(ptr4); FREE(ptrx); Thanks in advance Regards, 回答1: You can pass variable number of arguments in macro. Following code works fine: #define FREE_ALL(...) \ do { \ int i=0;\ void *pta[] = {__VA_ARGS__}; \ for(i=0; i < sizeof(pta)/sizeof(void*); i++) \ { \ free(pta[i]); \ }\ } while(0) 回答2: Use a function with variable number

Is this a double free in C?

走远了吗. 提交于 2019-12-01 07:03:35
Normally, if a pointer is freed twice, it's a double free. For example, char *ptr; ptr=malloc(5 * sizeof(*ptr)); free(ptr); free(ptr); The above code is considered as double free. Is the following considered as double free as well? char *ptr; char *ptr1; ptr=malloc(5 * sizeof(*ptr)); ptr1=ptr; free(ptr); free(ptr1); Thank you. Yes. The library doesn't care what name you gave a varaible in your source code (it's long gone by the time the code is executed). All that matters is the value, and in this case the values passed to free() would be the same. Yes. You're freeing the same memory twice.

free() invalid pointer

家住魔仙堡 提交于 2019-12-01 06:37:57
问题 I'm getting the familiar free(): invalid pointer error. In trying to debug, I ended up commenting out each free() in my code, one by one, until there were none left and I'm still getting this runtime error. Has anyone else run into a similar issue? By the way - it's difficult for me to debug this using gdb, because the entire server doesn't actually crash when the error message is printed, just the particular forked process that was handling the single client. Thank you. =====================