free

Why doesn't CudaFree seem to free memory?

老子叫甜甜 提交于 2019-11-28 00:03:46
I am trying to allocate device memory, copy to it, perform the calculations on the GPU, copy the results back and then free up the device memory I allocated. I wanted to make sure that I wasn't going over the limit and I wanted to see if I would have enough memory in the shared memory space to dump a few arrays. When I allocate device memory, there are no errors being returned. When I use cudaMemGetInfo to check the amount of memory allocated, it looks like one cudaMalloc hasn't allocated any memory. Also when I try to free the memory, it looks like only one pointer is freed. I am using the

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

倖福魔咒の 提交于 2019-11-27 22:49:39
问题 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

LD_PRELOAD only working for malloc, not free

爱⌒轻易说出口 提交于 2019-11-27 22:26:15
问题 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

Code for malloc and free

怎甘沉沦 提交于 2019-11-27 20:42:25
Where can I find the code for malloc my gcc compiler is using at the moment? I actually want to write my own malloc function which will be a little different from the original one. I know I can use hooks et all, but I want to see the real code. The POSIX interface of malloc is defined here . If you want to find out how the C library in GNU/Linux (glibc) implements malloc , go and get the source code from http://ftp.gnu.org/gnu/glibc/ and look at the malloc/malloc.c file. There is also the base documentation of the Memory Allocator by Doug Lea that describes the theory of a m (emory) alloc

Why free() doesn't really frees memory?

吃可爱长大的小学妹 提交于 2019-11-27 15:48:29
i'm doing some tests allocating and deallocating memory. This is the code i'm using: #include <stdlib.h> #include <stdio.h> #define WAVE_SIZE 100000000 int main(int argc,char* argv[]){ int i; int **p; printf("%d allocs...\n",WAVE_SIZE); // Malloc printf("Allocating memory...\n"); p = (int**)malloc(WAVE_SIZE*sizeof(int*)); for(i = 0;i < WAVE_SIZE;i++) p[i] = (int*)malloc(sizeof(int)); // Break printf("Press a key to continue...\n"); scanf("%*s"); // Dealloc printf("Deallocating memory...\n"); for(i = 0;i < WAVE_SIZE;i++) free(p[i]); free(p); // Break printf("Press a key to continue...\n");

Is there any danger in calling free() or delete instead of delete[]? [duplicate]

懵懂的女人 提交于 2019-11-27 15:39:30
Possible Duplicate: ( POD )freeing memory : is delete[] equal to delete ? Does delete deallocate the elements beyond the first in an array? char *s = new char[n]; delete s; Does it matter in the above case seeing as all the elements of s are allocated contiguously, and it shouldn't be possible to delete only a portion of the array? For more complex types, would delete call the destructor of objects beyond the first one? Object *p = new Object[n]; delete p; How can delete[] deduce the number of Object s beyond the first, wouldn't this mean it must know the size of the allocated memory region?

How to avoid long chain of free's (or deletes) after every error check in C?

百般思念 提交于 2019-11-27 14:41:05
Suppose I write my code very defensively and always check the return types from all the functions that I call. So I go like: char* function() { char* mem = get_memory(100); // first allocation if (!mem) return NULL; struct binder* b = get_binder('regular binder'); // second allocation if (!b) { free(mem); return NULL; } struct file* f = mk_file(); // third allocation if (!f) { free(mem); free_binder(b); return NULL; } // ... } Notice how quickly free() things get out of control. If some of the function fails, I have to free every single allocation before. The code quickly becomes ugly and all

Is free() zeroing out memory?

人走茶凉 提交于 2019-11-27 14:17:15
Until today I lived in belief that calling free() on memory space releases it for further allocation without any other modifications. Especially, considering this SO question that clearly states that free() DOESN'T zero out memory. Yet, let's consider this piece of code (test.c): #include<stdlib.h> #include<stdio.h> int main() { int* pointer; if (NULL == (pointer = malloc(sizeof(*pointer)))) return EXIT_FAILURE; *pointer = 1337; printf("Before free(): %p, %d\n", pointer, *pointer); free(pointer); printf("After free(): %p, %d\n", pointer, *pointer); return EXIT_SUCCESS; } Compiling (both GCC

Why is my pointer not null after free?

回眸只為那壹抹淺笑 提交于 2019-11-27 14:01:44
问题 void getFree(void *ptr) { if(ptr != NULL) { free(ptr); ptr = NULL; } return; } int main() { char *a; a=malloc(10); getFree(a); if(a==NULL) printf("it is null"); else printf("not null"); } Why is the output of this program not NULL? 回答1: Because the pointer is copied by value to your function. You are assigning NULL to the local copy of the variable ( ptr ). This does not assign it to the original copy. The memory will still be freed, so you can no longer safely access it, but your original

Allocate memory and save string in c

孤街浪徒 提交于 2019-11-27 13:39:47
问题 I was wondering why the following code isnt't working int main(int argc, char **argv) { char *test = (char*) malloc(12*sizeof(char)); test = "testingonly"; free(test); } After thinking about it my assumption was that first i allocate space for 12 chars in memory but the assignment in the next line creates a char array on the stack and the memory address of that is passed to test. So free() tries to release space on the stack which is not allowed. Is that correct? So what would be the correct