free

what happens when tried to free memory allocated by heap manager, which allocates more than asked for?

落爺英雄遲暮 提交于 2019-12-04 21:44:55
问题 This question was asked to me in an interview. Suppose char *p=malloc(n) assigns more than n,say N bytes of memory are allocated and free(p) is used to free the memory allocated to p. can heap manager perform such faulty allocation ? what happens now, will n bytes are freed or N bytes are freed? is there any method to find how much memory is freed? EDIT is there any method to find how much memory is freed? better than nothing, mallinfo() can shed some light as pointed by "Fred Larson" 回答1:

Weird behavior of malloc()

末鹿安然 提交于 2019-12-04 19:46:06
Trying to understand answers to my question what happens when tried to free memory allocated by heap manager, which allocates more than asked for? I wrote this function and puzzled by its output int main(int argc,char **argv){ char *p,*q; p=malloc(1); strcpy(p,"01234556789abcdefghijklmnopqrstuvwxyz"); //since malloc allocates atleast 1 byte q=malloc(2); // free(q); printf("q=%s\n",q); printf("p=%s\n",p); return 0; } Output q=vwxyz p=01234556789abcdefghijklm! Can any one explain this behavior? or is this implementation specific? also if free(q) is uncommented, I am getting SIGABRT. You are

c malloc questions (mem corruption)

三世轮回 提交于 2019-12-04 18:31:13
When using malloc, if it produces a core dump with the error: malloc(): memory corruption: ....... *** Does this mean that malloc tried to allocate memory that was not free to allocate? IF so what are the causes of this? It completely depends on your malloc implementation, but usually what this is means is that at some point prior to that malloc something wrote more data to a malloced buffer than its size. A lot of malloc implementations store some of their data inline with their memory, in other words: +--------------------------------+ |14 bytes -> Padding | +--------------------------------

How do I free a pointer returned from a function?

岁酱吖の 提交于 2019-12-04 18:09:30
问题 #include <stdio.h> #include <string.h> #include <stdlib.h> char* f(void) { char *x; x = malloc(sizeof(char) * 4); strcpy(x, "abc"); return(x); } int main(void) { char *a; a = f(); printf("%s", a); free(a); return(0); } Does the variable x in the function have to be freed? If so, how is that possible when I need to return it? 回答1: Does the variable x in the function have to be freed? Yes (kinda, see my later comment). Every call to malloc requires a later call to free . Otherwise, you have a

How to Free Memory of A Structure with Pointers to another Structure

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 14:49:16
问题 I have two structures struct SimpleXY { double x; double y; }; struct SimpleXyLink { int num_xy; SimpleXY *simpleXyList; }; I wonder what is the proper way to free the memory hold by SimplyXyLink ? I am currently using void Free(SimpleXyLink *myList) { free(myList->simpleXyList); } But I think this is wrong because it doesn't free the memory inside the element of simpleXyList . 回答1: First, the memory you're not freeing is the SimpleXy* Link * myList , not the memory inside the simpleXyList

When should I use free() in C?

六月ゝ 毕业季﹏ 提交于 2019-12-04 11:48:05
问题 The code works as it is supposed to, though it never frees the memory allocated by malloc() . I have tried to free memory in any place that I can, but no matter where I do it, it breaks the program. Specifically, I get a "double free or corruption error." This is more of a question as to what free() and malloc() actually do? All of the problems with free are in the main: int main(int argc, char *argv[]){ if(argc!=2){ exit(1); } printf("CSA WC version 1.0\n\n"); int length = strlen(argv[argc-1

Conflicting types for “free”

守給你的承諾、 提交于 2019-12-04 10:56:57
I'm getting the error Conflicting types for 'free' on the call to free() function below. int main ( ) { char fx [] = "x^2+5*x-1"; node * fxNode = buildTree(fx, sizeof(fx)/sizeof(char)); printf(deriveFromTree(fxNode)); // Should print "2*x+5" free(fxNode); return 0; } Can't figure out why. Not sure if this matter, but what's above it is #include <stdio.h> char opstack [5] = {'+','-','*','^', '\0'}; unsigned short int lowerOpPrecedence ( char, char, char * ); int stringToUnsignedInt ( char *, unsigned int * ); int stringToDouble ( char * , double * ); unsigned short int stringCompare ( char * ,

The differences between free in C and delete in C++?

让人想犯罪 __ 提交于 2019-12-04 09:38:27
I know the free operation in C is to tell the compiler this particular memory block is free for compiler to use for further allocation, but the memory is not released. What about the delete in C++? the same as free? There are two notions of delete in C++: One is the operator , declared as ::operator delete(void*) , which basically only frees up the memory and isn't usually thought about by most programmers. The other is the delete expression , delete p; , where p is a T* . The expression invokes the destructor of the object pointed to by p (and then frees the memory), which is a crucial

C - pointer is not null after freeing it

前提是你 提交于 2019-12-04 07:08:23
问题 Does the value of pointer become NULL after freeing it? int* p = malloc(sizeof(*p)); free(p); if(p==NULL) printf("Null\n"); else printf("Not null\n"); Output: Not null Well, I assume not; Anyway, I have asked a question earlier today : Check it out here: C - How can I free dynamically allocated memory? List* head1 = NULL; insertFront(&head1, 1); insertFront(&head1, 2); print(head1); while (head1) { List *temp = head1; head1 = head1->next; free(temp); } if(head1 == NULL) printf("Null\n"); else

C - How to Read String Lines from Stdin or File Memory Save

心已入冬 提交于 2019-12-04 06:54:17
问题 I need a version of read line that is memory save. I have this "working" solution. But I'm not sure how it behaves with memory. When I enable free(text) it works for a few lines and then I get an error. So now neither text nor result is ever freed although I malloc text. Is that correct ? And why is that so ? #include <stdio.h> #include <stdlib.h> #include <string.h> char* readFromIn() { char* text = malloc(1024); char* result = fgets(text, 1024, stdin); if (result[strlen(result) - 1] == 10)