free

How does free know how much to free?

空扰寡人 提交于 2019-11-25 23:59:18
问题 In C programming, you can pass any kind of pointer you like as an argument to free, how does it know the size of the allocated memory to free? Whenever I pass a pointer to some function, I have to also pass the size (ie an array of 10 elements needs to receive 10 as a parameter to know the size of the array), but I do not have to pass the size to the free function. Why not, and can I use this same technique in my own functions to save me from needing to cart around the extra variable of the

What REALLY happens when you don't free after malloc?

早过忘川 提交于 2019-11-25 23:00:00
问题 This has been something that has bothered me for ages now. We are all taught in school (at least, I was) that you MUST free every pointer that is allocated. I\'m a bit curious, though, about the real cost of not freeing memory. In some obvious cases, like when malloc is called inside a loop or part of a thread execution, it\'s very important to free so there are no memory leaks. But consider the following two examples: First, if I have code that\'s something like this: int main() { char *a =

How do malloc() and free() work?

旧巷老猫 提交于 2019-11-25 22:23:46
问题 I want to know how malloc and free work. int main() { unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char)); memset(p,0,4); strcpy((char*)p,\"abcdabcd\"); // **deliberately storing 8bytes** cout << p; free(p); // Obvious Crash, but I need how it works and why crash. cout << p; return 0; } I would be really grateful if the answer is in depth at memory level, if it\'s possible. 回答1: OK some answers about malloc were already posted. The more interesting part is how free works (and

How do free and malloc work in C?

北战南征 提交于 2019-11-25 20:40:29
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 When you malloc a block, it actually allocates a bit more memory than you asked for. This extra memory is

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

自作多情 提交于 2019-11-25 19:21:36
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 malloc's to return freed memory to the system? Without such return freed memory will be swapped out and in