free

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

≯℡__Kan透↙ 提交于 2020-01-02 09:59:11
问题 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

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

落爺英雄遲暮 提交于 2020-01-02 07:53:48
问题 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

const char* and free()

空扰寡人 提交于 2020-01-02 04:15:14
问题 Given the next code example, I'm unable to free the parameter const char* expression : // removes whitespace from a characterarray char* removewhitespace(const char* expression, int length) { int i = 0, j = 0; char* filtered; filtered = (char*)malloc(sizeof(char) * length); while(*(expression + i) != '\0') { if(!(*(expression + i) == ' ')) { *(filtered + j) = *(expression + i); j++; } i++; } filtered[j] = '\0'; free(expression); //this doesn't seem to work return filtered; } Before I return

Free an assigned pointer

最后都变了- 提交于 2020-01-02 01:07:12
问题 Does the following code free the memory that was allocated for x ? int main() { char *x = (char*)calloc(100, sizeof(char)); char *y = x; free(y); } 回答1: Yes When you do char *y = x; you make y point to the location where x points to. Since y points to a memory location returned by calloc , free(y); is perfectly valid. As @haccks commented, this would not work if you make y point to another memory location, provided that this memory location wasn't returned by malloc / calloc / realloc . In C,

Allocating memory for 2d matrix using 1 malloc call

帅比萌擦擦* 提交于 2020-01-01 03:45:06
问题 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 回答1: 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

Freeing strings in C

三世轮回 提交于 2019-12-31 22:29:12
问题 If I would write: char *a=malloc(sizeof(char)*4); a="abc"; char *b="abc"; do I need to free this memory, or is it done by my system? 回答1: In your situation you won't have any way to free the dynamic allocated memory because you are losing the reference to it. Try this out: #include <stdio.h> #include <stdlib.h> int main() { char *a=(char*)malloc(sizeof(char)*4); printf("Before: %p\n",a); a = "abc"; printf("After: %p\n",a); free(a); char *b = "abc"; return 0; } You will obtain Before:

Destructor class in TObject and NIL Delphi

烈酒焚心 提交于 2019-12-31 05:36:07
问题 I am wondering why after I invoke Free method the object is not nil . What I mean for example next class: type Ta = class(TObject) public i: integer; destructor Destroy; override; end; destructor Ta.Destroy; begin inherited; end; procedure Form1.Button1; var a: Ta; begin a := Ta.Create; a.Free; if a = nil then button1.Caption := 'is assigned' else button1.caption := 'is not assigned'; end; My question is why after freeing the object is not nil and how will I make a to be nil after destructor

C language: Releasing memory of pointers to struct

徘徊边缘 提交于 2019-12-31 02:10:46
问题 Say I have declared a pointer to a struct and assign it with malloc() using this definition typedef struct node { int info; struct node *next; } NODE; Then somewhere in the code I declared two pointers to it NODE *node1, *node2 = NULL; node1 = malloc(sizeof(NODE)); node2 = node1; My question, should I use "free()" to release node2 just like people always do to node1 via free(node1) . What's exactly the effect of the assignment node2 = node1; Thanks. 回答1: When you do node1 = malloc(sizeof(NODE

Can I assume that calling realloc with a smaller size will free the remainder?

冷暖自知 提交于 2019-12-29 03:56:10
问题 Let’s consider this very short snippet of code: #include <stdlib.h> int main() { char* a = malloc(20000); char* b = realloc(a, 5); free(b); return 0; } After reading the man page for realloc, I was not entirely sure that the second line would cause the 19995 extra bytes to be freed. To quote the man page: The realloc() function changes the size of the memory block pointed to by ptr to size bytes. , but from that definition, can I be sure the rest will be freed? I mean, the block pointed by b

free() on stack memory

泄露秘密 提交于 2019-12-28 06:46:33
问题 I'm supporting some c code on Solaris, and I've seen something weird at least I think it is: char new_login[64]; ... strcpy(new_login, (char *)login); ... free(new_login); My understanding is that since the variable is a local array the memory comes from the stack and does not need to be freed, and moreover since no malloc/calloc/realloc was used the behaviour is undefined. This is a real-time system so I think it is a waste of cycles. Am I missing something obvious? 回答1: You can only free()