free

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

☆樱花仙子☆ 提交于 2019-11-27 12:01:30
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 get the length of the allocated memory from a pointer, how does free() "automagically" know what to

Why does `free` in C not take the number of bytes to be freed?

折月煮酒 提交于 2019-11-27 09:55:55
问题 Just to be clear: I do know that malloc and free are implemented in the C library, which usually allocates chunks of memory from the OS and does its own management to parcel out smaller lots of memory to the application and keeps track of the number of bytes allocated. This question is not How does free know how much to free. Rather, I want to know why free was made this way in the first place. Being a low-level language, I think it would be perfectly reasonable to ask a C programmer to keep

Why do I get different results when I dereference a pointer after freeing it?

試著忘記壹切 提交于 2019-11-27 09:50:19
I've a question about the memory management in C (and GCC 4.3.3 under Debian GNU/Linux). According to the C Programming Language Book by K&R, (chap. 7.8.5), when I free a pointer and then dereference it, is an error. But I've some doubts since I've noted that sometimes, as in the source I've pasted below, the compiler (?) seems to work according a well-defined principle. I've a trivial program like this, that shows how to return an array dynamically allocated: #include <stdio.h> #include <stdlib.h> int * ret_array(int n) { int * arr = (int *) malloc(10 * sizeof(int)); int i; for (i = 0; i < n;

Why freed struct in C still has data?

℡╲_俬逩灬. 提交于 2019-11-27 09:48:14
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? When you free 'food', you are saying you are done with it. However, the pointer food still points to the same address, and that data is still there (it would be too much overhead to have to zero out every bit of memory

Problem usage memory in C

吃可爱长大的小学妹 提交于 2019-11-27 09:23:16
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 %MEM TIME+ COMMAND 23060 root 20 0 155m 153m 448 S 0 7.7 0:01.07 tmp_soft Full source : tmp_soft.c

Malloc, free and segmentation fault

孤人 提交于 2019-11-27 07:04:38
问题 I don't understand why, in this code, the call to "free" cause a segmentation fault: #include <stdio.h> #include <string.h> #include <stdlib.h> char *char_arr_allocator(int length); int main(int argc, char* argv[0]){ char* stringa = NULL; stringa = char_arr_allocator(100); printf("stringa address: %p\n", stringa); // same address as "arr" printf("stringa: %s\n",stringa); //free(stringa); return 0; } char *char_arr_allocator(int length) { char *arr; arr = malloc(length*sizeof(char)); arr =

There is no point in freeing blocks at end of program? [duplicate]

﹥>﹥吖頭↗ 提交于 2019-11-27 06:25:29
问题 Possible Duplicate: Is freeing allocated memory needed when exiting a program in C I was reading the page "Freeing Memory Allocated with malloc" and ran across this sentence: There is no point in freeing blocks at the end of a program, because all of the program's space is given back to the system when the process terminates. I realize what the author is trying to say, but shouldn't the sentence be: There is no point in freeing blocks at the end of a program, because all of the program's

when to carefully use free() to free up malloc() used memory?

雨燕双飞 提交于 2019-11-27 06:15:40
问题 I read from many question here in SO and some other articles regarding free() function in c that frees the memory of unused variables. In my case, I have the following code block. char *injectStrAt(char *str, char *strToIn, int pos) { char *strC = malloc(strlen(str) + strlen(strToIn) + 1); strncpy(strC, str, pos); strC[pos] = '\0'; strcat(strC, strToIn); strcat(strC, str + pos); return strC; } The above function I use to inject a string block in to an array. I am using malloc to create a new

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

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

橙三吉。 提交于 2019-11-27 04:25:30
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 problem. However, because the value of the string changes and the process allocates a new part of the