free

does exit() free allocated memory on both _SUCCESS and _FAILURE

笑着哭i 提交于 2019-12-04 02:21:39
This a short snippet of code, with two calls to exit(3) in case of failure. Do these calls deallocate memory allocated by malloc? Google search once says it does, and even more times, it doesn't... Should I add free()? Also: which is better if (!word) (it would also work for eg. word == 0 which is different from word == NULL, so I guess it is wrong) or if (word == NULL) ? char *word = NULL, *temp = NULL; word = (char *)malloc(sizeof(char) * size); if (!word) { /* or maybe rather it should be (word == NULL) */ perror("malloc fail"); if (fclose(fp)) { perror("fclose fail"); exit(3); /* exit

Do I need to free the strtok resulting string?

蹲街弑〆低调 提交于 2019-12-04 01:24:35
Or rather, how does strtok produce the string to which it's return value points? Does it allocate memory dynamically? I am asking because I am not sure if I need to free the token in the following code: The STANDARD_INPUT variables is for exit procedure in case I run out of memory for allocation and the string is the tested subject. int ValidTotal(STANDARD_INPUT, char *str) { char *cutout = NULL, *temp, delim = '#'; int i = 0; //Checks the number of ladders in a string, 3 is the required number temp = (char*)calloc(strlen(str),sizeof(char)); if(NULL == temp) Pexit(STANDARD_C); //Exit function,

Why do I have to use free on a pointer but not a normal declaration?

心不动则不痛 提交于 2019-12-04 00:34:19
Why do I have to use free() when I declare a pointer such as: int *temp = (int*)malloc(sizeof(int)) *temp = 3; but not when I do: int temp = 3; Normal declarations are put on the stack. When the function returns the stack pointer reverts to the value it had before the function was called, so the memory is automatically reclaimed. Malloc-based declarations are allocated from the 'heap', which requires the programmer to manage allocations and deallocations. You don't always have to use free on a pointer, just ones declared with malloc. You can declare a pointer that points to a memory location

How do I free a pointer returned from a function?

半腔热情 提交于 2019-12-03 11:48:25
#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? 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 leak. Remember though; you are not "free[ing] x ", you are freeing the memory that x refers to. When you

When do I need dynamic memory? [duplicate]

a 夏天 提交于 2019-12-03 09:30:51
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Malloc or normal array definition? We learn that there is dynamic memory in C and dynamic variables: #include <stdio.h> int a = 17; int main(void) { int b = 18; //automatic stack memory int * c; c = malloc( sizeof( int ) ); //dynamic heap memory *c = 19; printf("a = %d at address %x\n", a, &a); printf("b = %d at address %x\n", b, &b); printf("c = %d at address %x\n", *c, c); free(c); system("PAUSE"); return 0; }

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

若如初见. 提交于 2019-12-03 09:11:55
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 . First, the memory you're not freeing is the SimpleXy* Link * myList , not the memory inside the simpleXyList (you're freeing the memory referred to by that just fine). In general, it's up to you to figure out a way to

Allocating memory for 2d matrix using 1 malloc call

自古美人都是妖i 提交于 2019-12-03 09:02:29
We can allocate memory for 2d matrix using 1 malloc call as int (*a)[5]; int i,j; a=malloc(sizeof(int*) * 5); //allocating 5 pointers and each pointer points to an array of 5 ints How can we free this memory allocated successfully? Using free(a) gives run-time error Using for(i=0;i<5;i++) free(a[i]); free(a); This also gives run-time error Edit: THE WHOLE STORY. Previously I ignored THREE other ways to allocate 2d arrays. Dynamic 2d array method 1: This one works if you know the the number of columns at compile time. #define CCOLS 200 int (*m)[CCOLS] = malloc(cRows * sizeof(*m)); m[iRow][iCol]

glibc detected, realloc(): invalid pointer

こ雲淡風輕ζ 提交于 2019-12-03 08:26:07
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 *sketcher){ char line [MAX_WORD] = {"NULL"}; char word [MAX_WORD] = {"NULL"}; char figureName [MAX_WORD]

Problems with LD_PRELOAD and calloc() interposition for certain executables

风流意气都作罢 提交于 2019-12-03 08:04:50
Relating to a previous question of mine I've successfully interposed malloc , but calloc seems to be more problematic. That is with certain hosts, calloc gets stuck in an infinite loop with a possible internal calloc call inside dlsym . However, a basic test host does not exhibit this behaviour, but my system's "ls" command does. Here's my code: // build with: g++ -O2 -Wall -fPIC -ldl -o libnano.so -shared Main.cc #include <stdio.h> #include <dlfcn.h> bool gNanoUp = false;// global // Function types typedef void* (*MallocFn)(size_t size); typedef void* (*CallocFn)(size_t elements, size_t size)

When should I use free() in C?

烂漫一生 提交于 2019-12-03 07:13:00
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]); char file_to_open[length]; strcpy(file_to_open, argv[argc-1]); //printf("filename:%s\n",file_to