free

Problem usage memory in C

我只是一个虾纸丫 提交于 2019-11-26 17:49:45
问题 Please help :) OS : Linux Where in " sleep(1000);", at this time "top (display Linux tasks)" wrote me 7.7 %MEM use. valgrind : not found memory leak. I understand, wrote correctly and all malloc result is NULL. But Why in this time "sleep" my program NOT decreased memory ? What missing ? Sorry for my bad english, Thanks ~ # tmp_soft For : Is it free?? no Is it free?? yes For 0 For : Is it free?? no Is it free?? yes For 1 END : Is it free?? yes END ~ #top PID USER PR NI VIRT RES SHR S %CPU

Is there any danger in calling free() or delete instead of delete[]? [duplicate]

淺唱寂寞╮ 提交于 2019-11-26 17:15:21
问题 Possible Duplicate: ( POD )freeing memory : is delete[] equal to delete ? Does delete deallocate the elements beyond the first in an array? char *s = new char[n]; delete s; Does it matter in the above case seeing as all the elements of s are allocated contiguously, and it shouldn't be possible to delete only a portion of the array? For more complex types, would delete call the destructor of objects beyond the first one? Object *p = new Object[n]; delete p; How can delete[] deduce the number

How to avoid long chain of free's (or deletes) after every error check in C?

僤鯓⒐⒋嵵緔 提交于 2019-11-26 16:51:06
问题 Suppose I write my code very defensively and always check the return types from all the functions that I call. So I go like: char* function() { char* mem = get_memory(100); // first allocation if (!mem) return NULL; struct binder* b = get_binder('regular binder'); // second allocation if (!b) { free(mem); return NULL; } struct file* f = mk_file(); // third allocation if (!f) { free(mem); free_binder(b); return NULL; } // ... } Notice how quickly free() things get out of control. If some of

free char*: invalid next size (fast) [duplicate]

不羁的心 提交于 2019-11-26 16:49:26
This question already has an answer here: Facing an error “*** glibc detected *** free(): invalid next size (fast)” 2 answers I am freeing a char* after a concatenation process. But I receive this error: free(): invalid next size (fast): 0x0000000001b86170 Below is my code: void concat(stringList *list) { char *res = (char*)malloc(sizeof(char*)); strcpy(res, list->head->string); list->tmp = list->head->next; while (list->tmp != NULL) { strcat(res, ","); strcat(res, list->tmp->string); list->tmp = list->tmp->next; } printf("%s\n", res); free(res); } Your code is wrong. You are allocating space

If free() knows the length of my array, why can't I ask for it in my own code?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 15:59:55
问题 I know that it's a common convention to pass the length of dynamically allocated arrays to functions that manipulate them: void initializeAndFree(int* anArray, size_t length); int main(){ size_t arrayLength = 0; scanf("%d", &arrayLength); int* myArray = (int*)malloc(sizeof(int)*arrayLength); initializeAndFree(myArray, arrayLength); } void initializeAndFree(int* anArray, size_t length){ int i = 0; for (i = 0; i < length; i++) { anArray[i] = 0; } free(anArray); } but if there's no way for me to

Why freed struct in C still has data?

我是研究僧i 提交于 2019-11-26 14:50:43
问题 When I run this code: #include <stdio.h> typedef struct _Food { char name [128]; } Food; int main (int argc, char **argv) { Food *food; food = (Food*) malloc (sizeof (Food)); snprintf (food->name, 128, "%s", "Corn"); free (food); printf ("%d\n", sizeof *food); printf ("%s\n", food->name); } I still get 128 Corn although I have freed food. Why is this? Is memory really freed? 回答1: When you free 'food', you are saying you are done with it. However, the pointer food still points to the same

Where Do malloc() / free() Store Allocated Sizes and Addresses?

拟墨画扇 提交于 2019-11-26 12:44:21
问题 where do malloc() and free() store the allocated addresses and their sizes (Linux GCC)? I\'ve read that some implementations store them somewhere before the actual allocated memory, but I could not confirm that in my tests. The background, maybe someone has another tip for this: I\'m experimenting a little bit with analyzing the heap memory of a process in order to determine the current value of a string in the other process. Accessing the process heap memory and strolling through it is no

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

纵然是瞬间 提交于 2019-11-26 12:37:04
问题 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

malloc implementation?

十年热恋 提交于 2019-11-26 10:31:50
问题 I\'m trying to implement malloc and free for C, and I am not sure how to reuse memory. I currently have a struct that looks like this: typedef struct _mem_dictionary { void *addr; size_t size; int freed; } mem_dictionary; My malloc looks like this: void *malloc(size_t size) { void *return_ptr = sbrk(size); if (dictionary == NULL) dictionary = sbrk(1024 * sizeof(mem_dictionary)); dictionary[dictionary_ct].addr = return_ptr; dictionary[dictionary_ct].size = size; dictionary[dictionary_ct].freed

Does free(ptr) where ptr is NULL corrupt memory?

。_饼干妹妹 提交于 2019-11-26 10:21:47
Theoretically I can say that free(ptr); free(ptr); is a memory corruption since we are freeing the memory which has already been freed. But what if free(ptr); ptr=NULL; free(ptr); As the OS will behave in an undefined manner I cannot get an actual theoretical analysis for this about what's happening. Whatever I am doing, is this memory corruption or not? Is freeing a NULL pointer valid? 7.20.3.2 The free function Synopsis #include <stdlib.h> void free(void *ptr); Description The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation.