free

glibc detected, realloc(): invalid pointer

安稳与你 提交于 2019-12-21 02:42:09
问题 I apologize for the lengthy code. I have a simple question, but I thought I include my code so it will be clear where I am coming from. I get a realloc corruption. I think the corruption is because I am not freeing correctly. In reality I am not sure what glibc even says or means. Can any one briefly explain that to me? Again sorry for the lengthy code. #include "draw2.h" #include "draw2a.h" #include "draw2b.h" const char Exec_c[] = "java -jar Sketchpad.jar"; void parseFile(FILE * fp, FILE

How can free Interface implemented class?

情到浓时终转凉″ 提交于 2019-12-20 03:49:12
问题 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:

Does freeing an uninitialized pointer result in undefined behavior?

大兔子大兔子 提交于 2019-12-20 03:02:08
问题 If you have a pointer that is not initialized and, by mistake, try to free it, is this going to result in undefined behavior? Like: int main(void){ char *string; free(string); return 0; } 回答1: Does freeing an uninitialized pointer result in undefined behavior? Yes. However, freeing a null pointer is well-defined. From the C99 standard: The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action

Returning dynamically allocated memory back to OS without terminating the program

跟風遠走 提交于 2019-12-20 02:06:19
问题 I am working on a program where I am using large but limited amount of memory. Memory is allocated and freed on run time on different threads. However, I noticed that the memory usage of the program would not remain within specified bounds. It would increase as time passed on. I wrote the following sample program to check whether memory is being freed back to the OS. Half the allocated memory was freed to check if the memory usage went down. int main() { char *p[COUNT]; for(int i = 0; i <

Should I free allocated memory on abnormal termination?

谁说胖子不能爱 提交于 2019-12-19 12:51:20
问题 My program (a text-mode web browser) is dynamically allocating memory. I do free unneeded blocks during runtime, of course. And I do free everything before normal termination - so that memory leak checkers won't give me false positives (and to be flexible should major refactorings ever become needed). Now, what I do not do is freeing memory before abnormal termination. (Currently, my program terminates on signals and after failed mallocs/reallocs.) My question is: Do you consider this bad

multi dimensional char array?

我是研究僧i 提交于 2019-12-19 12:00:07
问题 What I am trying to do is create an 2-d array of character strings The following seg faults instantly, what is wrong? void add2(char***b, char *i) { if (!i) { b[0][0] = (char*) malloc(120); sprintf(b[0][0], "%s", "hithere"); b[0][1] = (char*) malloc(120); sprintf(b[0][1], "%s", "bithere"); } else { strcat(b[0][0], "\\\\"); strcat(b[0][0], i); strcat(b[0][1], "\\\\"); strcat(b[0][1], i); } } void add1(char ***b) { add2(b,NULL); add2(b,"one"); add2(b,"two"); add2(b,"three"); } int main() { char

Exists a way to free memory in atexit or similar without using global variables?

你。 提交于 2019-12-19 08:03:58
问题 I am developing a project in C, and I need to free the allocated memory and also close all the open files before it exits. I decided to implement a clean function that will do all this stuff and call it with atexit because there are a lot of possible exit scenarios. The problem is that atexit doesn't allow me to set functions with parameters, so I can't send to clean the pointers that need to be freed in the end of the process. So I need to declare as global variables every pointer that may

Can a call to free() in C ever fail?

笑着哭i 提交于 2019-12-19 05:03:49
问题 Can a call to free() fail in any way? For example: free(NULL); 回答1: Freeing a NULL pointer cannot fail. And free doesn't return any error, but freeing unallocated memory, already freed memory or the middle of an allocated block is undefined behaviour - it may cause a memory error and the program may abort (or worse, it will corrupt the heap structure and crash later). Or, even worse than that, keep running but totally corrupt your data and write it to disk without you realising :-) The

C Programming: malloc and free within a loop

家住魔仙堡 提交于 2019-12-19 03:08:23
问题 I just started out with C and have very little knowledge about performance issues with malloc() and free() . My question is this: if I were to call malloc() followed by free() inside a while loop that loops for, say, 20 iterations, would it run slower compared to calling free() outside the loop? I am actually using the first method to allocate memory to a buffer, read a variable-length string from a file, perform some string operations, and then clear the buffer after every iteration. If my

Understanding of pointers with malloc and free

六眼飞鱼酱① 提交于 2019-12-18 16:52:48
问题 Pointers are a really tricky thing in C. For a lot of people is hard to understand it, so for a good understanding I wrote following code: #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { int *p; // pointer -> will be dynamic allocated int *a; // array -> will be dynamic allocated // print before allocate memory (1) printf("&p: %p\tp: %p\t*p: %d\n", &p, p, *p); printf("&a: %p\ta: %p\t*a: %d\n", &a, a, *a); printf("\n"); // allocate memory (2) p = (int *)malloc(sizeof