free

Can I force a numpy ndarray to take ownership of its memory?

北战南征 提交于 2020-01-10 08:53:09
问题 I have a C function that mallocs() and populates a 2D array of floats. It "returns" that address and the size of the array. The signature is int get_array_c(float** addr, int* nrows, int* ncols); I want to call it from Python, so I use ctypes. import ctypes mylib = ctypes.cdll.LoadLibrary('mylib.so') get_array_c = mylib.get_array_c I never figured out how to specify argument types with ctypes. I tend to just write a python wrapper for each C function I'm using, and make sure I get the types

Why are the contents pointed to by a pointer not changed when memory is deallocated using free()?

佐手、 提交于 2020-01-10 05:15:20
问题 I am a newbie when it comes to dynamic memory allocation. When we free the memory using void free(void *ptr) the memory is deallocated but the contents of the pointer are not deleted. Why is that? Is there any difference in more recent C compilers? 回答1: Computers don't "delete" memory as such, they just stop using all references to that memory cell and forget that anything of value is stored there. For example: int* func (void) { int x = 5; return &x; } printf("%d", *func()); // undefined

Why do I get different results when I dereference a pointer after freeing it?

杀马特。学长 韩版系。学妹 提交于 2020-01-09 03:57:12
问题 I've a question about the memory management in C (and GCC 4.3.3 under Debian GNU/Linux). According to the C Programming Language Book by K&R, (chap. 7.8.5), when I free a pointer and then dereference it, is an error. But I've some doubts since I've noted that sometimes, as in the source I've pasted below, the compiler (?) seems to work according a well-defined principle. I've a trivial program like this, that shows how to return an array dynamically allocated: #include <stdio.h> #include

malloc and free with a dynamically changing structure

a 夏天 提交于 2020-01-07 02:25:28
问题 I am having trouble with moving my pointer in a dynamically changing structure. I have created my code where you can malloc more memory and this seems to be working. The problems that I am running into is how to add to the structure, how to free memory and how to move from structure to structure and print all items. I am trying to test add and print (the delete function that is there does not seem to work, segfaults) When I add to the struct and then print the struct I get a segfault from the

C - pointer being freed was not allocated

蹲街弑〆低调 提交于 2020-01-06 20:29:06
问题 I am trying to free a pointer that I assigned from a vector allocated with malloc() , when I try to remove the first element(index [0]), it works, when I try to remove the second(index [1]) I receive this error: malloc: *** error for object 0x100200218: pointer being freed was not allocated The code: table->t = malloc (sizeof (entry) * tam); entry * elem = &table->t[1]; free(elem); 回答1: You can only call (or need to) free() on the pointer returned by malloc() and family. Quoting C11 , chapter

ANSI C Dynamic Memory Allocation and when exactly we should free the memory

大憨熊 提交于 2020-01-06 15:40:51
问题 I am trying to get my head around memory allocations and freeing them in ANSI C. The problem is I don't know when to free them. 1) Does program exit free the allocated memory itself (even if I didn't do it by free() )? 2) Let's say my code is something like this: (please don't worry about the full code of those structs at the moment. I am after the logic only) snode = (stock_node *) realloc(snode, count * sizeof(stock_node)); struct stock_list slist = { snode, count }; stock_list_ptr slist

Is there a way to determine if free() would fail?

橙三吉。 提交于 2020-01-05 02:35:53
问题 Is there a way to determine if free() would fail if ever called on a certain memory block pointer? I have the following situation: a thread having access to a shared resource fails whilst it may have been in the state of freeing the said resource. Now I need to devise a safe way to clean-up this shared resource. Of course I have assigned ownership of the resource for the normal case but what about the aforementioned limit case? UPDATED: If I use additional synchronizing mechanisms it only

Is there a way to determine if free() would fail?

假如想象 提交于 2020-01-05 02:35:29
问题 Is there a way to determine if free() would fail if ever called on a certain memory block pointer? I have the following situation: a thread having access to a shared resource fails whilst it may have been in the state of freeing the said resource. Now I need to devise a safe way to clean-up this shared resource. Of course I have assigned ownership of the resource for the normal case but what about the aforementioned limit case? UPDATED: If I use additional synchronizing mechanisms it only

freeing substring in c - loop

旧城冷巷雨未停 提交于 2020-01-05 02:11:11
问题 I'm trying to get a sub-string for each member of the struct ' structs ' and then assign that sub-string to a new member of the temp_struct . The problem I'm having is how to free the sub-string on each iteration, for some reason the code runs, however valgrind throws an Invalid read of size 1 , which I assume I'm reading off the block of memory. How could I free the sub-string? Thanks #include <stdio.h> #include <stdlib.h> #include <string.h> struct st_ex { char product[16]; float price; };

Is there a better/cleaner/more elegant way to malloc and free in cuda?

大兔子大兔子 提交于 2020-01-02 09:59:59
问题 I am trying to cudaMalloc a bunch of device pointers, and gracefully exit if any of the mallocs didn't work. I have functioning code - but bloated because I have to cudaFree everything I'd previously malloc'd if one fails. So now I am wondering if there is a more succinct method of accomplishing this. Obviously I can't free something that hasn't been malloc'd - that will definitely cause problems. Below is the snippet of code I am trying to make more elegant. //define device pointers float d