free

Freeing malloced structure in a function

纵饮孤独 提交于 2019-11-29 14:28:50
I'm creating a source files containing buffer functionality that I want to use for my other library that I'm creating. It is working correctly but I'm having trouble getting rid of the buffer structure that I'm creating in one of the functions. The following snippets should help illustrate my problem: C header: //dbuffer.h ... typedef struct{ char *pStorage; int *pPosition; int next_position; int number_of_strings; int total_size; }DBUFF; ... C source: //dbuffer.c ... DBUFF* dbuffer_init(char *init_pArray) { //Find out how many elements the array contains int size = sizeof_pArray(init_pArray);

Implementation of Realloc in C

五迷三道 提交于 2019-11-29 12:07:43
int getmin(int a, int b) { return a<b?a:b; } void *reallocation(void *ptr, size_t size) //size_t in bytes { void *newptr; int msize; msize = getsize(ptr); msize = getmin(msize, size); printf("msize = %d", msize); newptr = malloc(size); newptr = memcpy(newptr, ptr, msize); free(ptr); return newptr; } I have implemented my own realloc, and in order to get the size of the allocated memory using malloc(however i know there isn't any method for this in c). My reallocation function is working fine on my system How do we get the size of the memory allocated by malloc(). Also can we do inplace

Does the pointer passed to free() have to point to beginning of the memory block, or can it point to the interior?

Deadly 提交于 2019-11-29 10:42:45
The question is in the title... I searched but couldn't find anything. Edit: I don't really see any need to explain this, but because people think that what I'm saying makes no sense (and that I'm asking the wrong questions), here's the problem: Since people seem to be very interested in the "root" cause of all the problem rather than the actual question asked (since that apparently helps things get solved better, let's see if it does), here's the problem: I'm trying to make a D runtime library based on NTDLL.dll, so that I can use that library for subsystems other than the Win32 subsystem. So

C - Does freeing an array of pointers also free what they're pointing to?

╄→尐↘猪︶ㄣ 提交于 2019-11-29 10:21:27
问题 Say I have an array of pointers to structs that contain a string each and so for something like this: printf("%s\n", array[0]); The output is: Hello. If I perform a free(array) will this free what array[0] is pointing to? ( "Hello." ). I've spent hours attempting to manually free each element and all I get is crashes. I'm hoping this is a shortcut :/ 回答1: This all depends on how the array was allocated. I'll give examples: Example 1: char array[10]; free(array); // nope! Example 2: char

Freeing in an atexit()

 ̄綄美尐妖づ 提交于 2019-11-29 09:29:35
Is there any point to freeing memory in an atexit() function? I have a global variable that gets malloc'ed after startup. I could write an atexit() function to free it, but isn't the system going to reclaim all that memory when the program exits anyway? Is there any benefit to being tidy and actively cleaning it up myself? Not in C - it's like rearranging the deck chairs while the ship sinks around you. In C++ the answer is different, because objects can delete temporary files and so forth in their destructors, so you need to make sure those get called. One benefit on freeing it is that if you

Function free() in C isn't working for me

匆匆过客 提交于 2019-11-29 09:01:53
I have been trying to free memory allocated via malloc() using free() . Some of the structs it does free but leaves some the way they were and they also remain linked to their children. It also never frees the root (gRootPtr) for a binary tree. I am using Xcode to find out if the memory used by the binary tree has been freed and also use the if statement. Code I am using to free the memory: void FreeMemory(InfoDefiner *InfoCarrier) { if ((*InfoCarrier) != NULL) { FreeMemory((&(*InfoCarrier)->left)); FreeMemory((&(*InfoCarrier)->right)); free((*InfoCarrier)); } } Code I am using to see if the

Reusing freed pointers in C

断了今生、忘了曾经 提交于 2019-11-29 06:02:07
There are many questions on this website regarding freeing pointers after use and, further, setting them to NULL. Arguments are fierce and the topic is seemingly divided equally. For example: This question . I am confused about freeing pointers in general. Imagine you have a pointer to some memory space. After using the space, you free the pointer but do not set it to NULL. Later, you have another pointer that calls malloc() , or some analog, and it is allocated memory including the memory freed earlier (that the original pointer still points to). If this new pointer writes in this memory

When a program terminates what happens to the memory allocated using malloc that is not free'ed?

倖福魔咒の 提交于 2019-11-29 05:42:41
Say I have the following program #include <stdio.h> #include <stdlib.h> int main(void) { int * i; if ((i = malloc(sizeof(int) * 100)) == NULL) { printf("EROOR: unable to allocate memory \n"); return -1; } /* memory is allocated successfully */ /* memory is not free'ed but program terminates */ // free(i); return 0; } The above program calls malloc to allocate some memory and does not call free to de-allocate it. And the program terminates without de-allocating the memory. Valgrind clearly detects a memory leak. <snap> ==14209== HEAP SUMMARY: ==14209== in use at exit: 400 bytes in 1 blocks =

Optimal way to free() a malloc'ed 2D array in C

烈酒焚心 提交于 2019-11-29 05:20:55
Supposing I have a 2 dimensional array which was created with something like this, char **foo = (char **) malloc(height * sizeof(char *)); for(i = 0; i <= height; i++) foo[i] = (char *) malloc (width * sizeof(char *)); First of all, Is this even the right way to create an array like this?. The catch here is, 'height' and 'width' is something that is set during runtime. This seems to work, but which is the best strategy to free this 2d array. free(funge) sounds wrong. Going by some other posts in here, I guess I will have free each row one by one? I did try something like this, for (height = 0;

LD_PRELOAD only working for malloc, not free

瘦欲@ 提交于 2019-11-29 04:39:27
I'm trying to interpose malloc/free/calloc/realloc etc with some interposers via LD_PRELOAD. In my small test, only malloc seems to be interposed, even though free is detected (see output). I'd expect the output to contain a line "NANO: free(x)" - but this line is missing. Given // compile with: gcc test.cc #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { void* p = malloc(123); printf("HOST p=%p\n", p); free(p); } And // compile with: g++ -O2 -Wall -fPIC -ldl -o libnano.so -shared main.cc #include <stdio.h> #include <dlfcn.h> typedef void *(*MallocFunc)(size_t size);