free

Do I need to free memory returned from a C function called via CFFI?

隐身守侯 提交于 2019-12-06 01:53:26
I have this example code that has a function text() returning a newly allocated string: ffi_test = FFI() ffi_test.set_source('_test', ''' char* test() { return strdup("hello world"); } ''') ffi_test.cdef(''' char* test(); void free(void *); ''') ffi_test.compile(verbose=True) This works fine: In [1]: from _test import ffi, lib In [2]: x = lib.test() In [3]: ffi.string(x) Out[3]: b'hello world' In [4]: lib.free(x) However, I could not find anything in the docs whether I actually need to manually free() the returned string of if CFFI takes ownership of the pointer as soon as it's returned to

对malloc和free的思考

我们两清 提交于 2019-12-05 20:54:26
进程的内存布局: Heap的顶端的限制叫做program break,通过系统调用brk活着sbrk可以想内核申请内存从而改变break,也就是增加或收缩heap的大小。 进程的地址空间所面对的都是虚拟地址,kernel为每个进程维护一个page table,建立了虚拟地址空间的页和物理内存页或swap空间的映射(虚拟内存或物理内存都是以页为单位)。 很重要的一个特点是,虚拟地址空间是连续的! 虚拟内存管理有很多好处:1、进程相互之间或进程与内核之间相互隔离,进程不能操作其他进程的内存空间,更不能操作内核的空间。2、多个进程可以共享内存。 (多个进程执行相同的程序,也就是多个进程的text segment所对应的物理内存是同一份),节约内存。3、进程维护的页表可以更容易的实现内存保护。(标记page table的entry即可) 虚拟内存这块让我想到了,“软件的很多问题都可以靠加一个中间层解决”;o(∩_∩)o 哈哈 下面是一个简单的malloc和free实现,通过系统调用sbrk来实现: 实现细节: 1、在需要的内存块前面追加一小块空间,来存储当前块的大小(貌似都这样) 2、维护两个全局变量,managed_memory_start、所维护内存的起始地址 last_valid_address、所维护内存的最后有效地址,也就是program break 3、free的实现很简单

freeing memory of a binary tree C

随声附和 提交于 2019-12-05 17:20:33
I would like to free memory from my allocated binary tree what traversal is the best for doing so? typedef struct Node{ struct Node * right; struct Node * left; void * data; }Node; typedef int (*cmp) (void*,void *); Node* init(void * element){ Node * newNode=(Node*)malloc(sizeof(Node)); newNode->data=element; newNode->left=NULL; newNode->right=NULL; return newNode; } void insert(void * element, Node** root,cmp compareTo){ if(*root==NULL){ *root=init(element); return; } if(compareTo(element,(*root)->data)==1) insert(element,&((*root)->left),compareTo); else insert(element,&((*root)->right)

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

倾然丶 夕夏残阳落幕 提交于 2019-12-05 15:42:14
问题 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

free a pointer to dynamic array in c

北战南征 提交于 2019-12-05 15:03:45
I create a dynamic array in c with malloc. e.g.: myCharArray = (char *) malloc(16); now if I make a function like this and pass myCharArray to it: reset(char * myCharArrayp) { free(myCharArrayp); } will that work, or will I somehow only free the copy of the pointer (myCharArrayp) and not the actual myCharArray? That will be fine and free the memory as you expect. I'd consider writing a function like void reset(char** myPointer) { if (myPointer) { free(*myPointer); *myPointer = NULL; } } So that the pointer is set to NULL after being freed. Reusing previously freed pointers is a common source

Understanding glibc malloc trimming

给你一囗甜甜゛ 提交于 2019-12-05 11:09:58
Some program that I am currently working on consumes much more memory than I think it should. So I am trying to understand how glibc malloc trimming works. I wrote the following test: #include <malloc.h> #include <unistd.h> #define NUM_CHUNKS 1000000 #define CHUNCK_SIZE 100 int main() { // disable fast bins mallopt(M_MXFAST, 0); void** array = (void**)malloc(sizeof(void*) * NUM_CHUNKS); // allocating memory for(unsigned int i = 0; i < NUM_CHUNKS; i++) { array[i] = malloc(CHUNCK_SIZE); } // releasing memory ALMOST all memory for(unsigned int i = 0; i < NUM_CHUNKS - 1 ; i++) { free(array[i]); }

How to handle free() errors in C?

天大地大妈咪最大 提交于 2019-12-05 10:52:56
Suppose that I have used a free() function to free a memory that,for many reasons, I'm not allowed to. How can I stop my C application from crashing and just generate an error and continue the execution? I don't have try-catch kind of provision here (like C++/java...). Is there any way to ignore this error and continue execution? If yes, How do you do that? More importantly, is it advisable to do so (continuing execution considering this memory error occurred)? Thank you There is nothing in the C standard that you can use to do what you want. The description of the free function is very clear

Freeing memory returned from C functions

▼魔方 西西 提交于 2019-12-05 10:48:10
In C, which is the better practice when it comes to freeing memory returned from functions: Provide a "destructor" function that encapsulates the call to free(). Require users to free() the returned pointer themselves. For example, to open and close a file we do: FILE* f = fopen("blah", "w"); fclose(f); Is this preferable to: FILE* f = fopen("blah", "w"); fclose(f); free(f); Warning: Don't call free() on a FILE pointer. I only use it a hypothetical implementation here. And what about cases where local variables are pointed to the returned memory? Is free() harmful here? (or perhaps this should

How to use strtok in C properly so there is no memory leak?

与世无争的帅哥 提交于 2019-12-05 00:49:45
I am somewhat confused by what happens when you call strtok on a char pointer in C. I know that it modifies the contents of the string, so if I call strtok on a variable named 'line', its content will change. Assume I follow the bellow approach: void function myFunc(char* line) { // get a pointer to the original memory block char* garbageLine = line; // Do some work // Call strtok on 'line' multiple times until it returns NULL // Do more work free(garbageLine); } Further assume that 'line' is malloced before it is passed to myFunc. Am I supposed to free the original string after using strtok

Howto check if a char* points to a string literal in C

微笑、不失礼 提交于 2019-12-04 22:30:02
I have a struct struct request { int code; char *message; }; that I'd like to free properly. I have the following function to do that: void free_request(struct request *req) { if (req->message != NULL) { free(req->message); } free(req); req = NULL; } The problem is that I get an "free(): invalid pointer"/segfault error from the compiler when I try to free a request that has been created using a string literal: struct request *req; req = malloc(sizeof(struct request)); req->message = "TEST"; free_request(req); Since I want to create request structs in different places, once using literals (on