free

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

大兔子大兔子 提交于 2019-12-02 14:01:11
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) result[strlen(result) - 1] = 0; //free(text); return result; } I have A LOT of short lines to read with

C - pointer is not null after freeing it

余生颓废 提交于 2019-12-02 13:32:55
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 printf("Not null\n"); Output in this case: Null In this case after freeing head1 (nodes also) the

GlibC Double free or corruption (fclose)

二次信任 提交于 2019-12-02 09:29:59
I got an error on my C program on runtime. I found some stuff about "double free or corruption" error but nothing relevant. Here is my code : void compute_crc32(const char* filename, unsigned long * destination) { FILE* tmp_chunk = fopen(filename, "rb"); printf("\n\t\t\tCalculating CRC..."); fflush(stdout); Crc32_ComputeFile(tmp_chunk, destination); printf("\t[0x%08lX]", *destination); fflush(stdout); fclose(tmp_chunk); printf("\t[ OK ]"); fflush(stdout); } It seems the fclose(tmp_chunk); raises this glibc error : *** glibc detected *** ./crc32: double free or corruption (out): 0x09ed86f0 ***

Destructor class in TObject and NIL Delphi

醉酒当歌 提交于 2019-12-02 08:18:36
I am wondering why after I invoke Free method the object is not nil . What I mean for example next class: type Ta = class(TObject) public i: integer; destructor Destroy; override; end; destructor Ta.Destroy; begin inherited; end; procedure Form1.Button1; var a: Ta; begin a := Ta.Create; a.Free; if a = nil then button1.Caption := 'is assigned' else button1.caption := 'is not assigned'; end; My question is why after freeing the object is not nil and how will I make a to be nil after destructor without using a := nil ? Explanation: The variable a will only become nil when it is assigned nil .

Why does not free() deallocate all allocated memory locations? [duplicate]

守給你的承諾、 提交于 2019-12-02 07:42:00
问题 This question already has answers here : free() not deallocating memory? (5 answers) Closed 3 years ago . I don't know if I am doing something wrong or if my concept is somewhat wrong #include<stdio.h> #include<stdlib.h> int main() { int *p; p=calloc(3,sizeof(int)); p[0]=10; p[1]=15; p[2]=30; printf("\n%d\n%p\n%d\n%p\n%d\n%p\n\n",p[0],p,p[1],p+1,p[2],p+2); free(p); //p=NULL; printf("\n%d\n%p\n%d\n%p\n%d\n%p\n\n",p[0],p,p[1],p+1,p[2],p+2); return 0; } When the 2nd, printf() is run, it shows p

How can free Interface implemented class?

人走茶凉 提交于 2019-12-02 07:10:23
I have a little problem. As the title says I want to release an object whose class implements an interface, however I get an error "invalid pointer operation" . My Interface: Interface Type // An interface definition IMessageEvents = Interface(IInterface) ['{BD27EFC6-CC9A-437A-A8B8-16F722518836}'] Procedure messageReceived(messageData: String); End; Implementation My Class: Type TChatManager = Class(TInterfacedObject, IMessageEvents) Private Protected Procedure messageReceived(messageData: String); Overload; Public Constructor Create; Overload; Destructor Destroy; Override; Procedure connect;

Why does not free() deallocate all allocated memory locations? [duplicate]

ε祈祈猫儿з 提交于 2019-12-02 04:48:11
This question already has an answer here: free() not deallocating memory? 5 answers I don't know if I am doing something wrong or if my concept is somewhat wrong #include<stdio.h> #include<stdlib.h> int main() { int *p; p=calloc(3,sizeof(int)); p[0]=10; p[1]=15; p[2]=30; printf("\n%d\n%p\n%d\n%p\n%d\n%p\n\n",p[0],p,p[1],p+1,p[2],p+2); free(p); //p=NULL; printf("\n%d\n%p\n%d\n%p\n%d\n%p\n\n",p[0],p,p[1],p+1,p[2],p+2); return 0; } When the 2nd, printf() is run, it shows p[2]=30, whereas p[0]=p[1]=0 (in gcc ubuntu and some arbitary values in Code::Blocks windows). I have 2 questions. Why free()

Is casting free() argument to void * neccessary?

[亡魂溺海] 提交于 2019-12-02 04:25:03
问题 Is it neccessary to cast the value passed to free() to a void pointer in this code snippet? free((void *) np->defn); np is a struct in a linked list and defn is a char * . 回答1: C11 : 7.22.3.3 The free function Synopsis #include <stdlib.h> void free(void *ptr); It means that it can take pointer to any type ( void * is a generic pointer type). In general no need to cast the argument to void * , but in cases like int const *a = malloc(sizeof(int)); free(a); compiler will generate a waring test.c

C Linked List valgrind Invalid Read of Size

南笙酒味 提交于 2019-12-02 03:10:23
I have a problem with my Linked List and the valgrind output. Without further adieu here is my linked list: typedef struct Map map; struct Map { void *address; double free_time; map* next; }*map_list; The list is created using a dummy head node. As you can see, the struct holds an address and a free time, which I try to associate them. In the find_and_free function I search this list using a time and if this time is smaller than the one stored in the list, I deallocate the saved address. And then I deallocate the list node as well. This is the function used to find any free time that is

Basic Malloc/Free

泄露秘密 提交于 2019-12-01 22:04:37
If I have a snippit of my program like this: struct Node *node; while(...){ node = malloc(100); //do stuff with node } This means that every time I loop through the while loop I newly allocate 100 bytes that is pointed to by the node pointer right? If this is true, then how do I free up all the memory that I have made with all the loops if I only have a pointer left pointing to the last malloc that happened? Thanks! Please allocate exactly the size you need: malloc(sizeof *node); -- if you move to a 64-bit platform that doubles the size of all your members, your old 96-byte structure might