free

Why free() doesn't really frees memory?

北城余情 提交于 2019-12-28 04:27:13
问题 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");

How are we able to access the pointer after deallocating the memory?

这一生的挚爱 提交于 2019-12-28 03:12:21
问题 As per my understanding, free() is used to deallocate the memory that we allocated using malloc before. In my following snippet, I have freed the memory i have allocated. But i was able to access the pointer even after freeing? How it is possible? How free works internally? #include<iostream> using namespace std; int main() { int *p=(int *)malloc(sizeof(int)); *p=17; free(p); *p=*p+1; printf("\n After freeing memory :: %d ",*p ); return 0; } 回答1: You can certainly continue to use p after

Calling free() causes my program to crash

主宰稳场 提交于 2019-12-25 18:45:35
问题 I'm having a really weird issue where trying to call free on an allocated piece of memory causes my program to crash. Here's the relevant code: int i, count; char *specifier; char aisle[1]; count = 0; /*Find name of the new item and assign to the name field of new_node...*/ for (i = 0; input[i] != ','; i++){ count++; } specifier = (char*)malloc((count+1)*sizeof(char)); if (specifier == NULL){ printf("Out of memory. Shutting down.\n"); exit(EXIT_FAILURE); } for (i = 0; input[i] != ','; i++){

Freeing a double pointer from a struct

依然范特西╮ 提交于 2019-12-25 18:26:27
问题 I have a problem with my delete_table function. So i have 2 structs struct _entry_ { int key; int data; struct _entry_* next; struct _entry_* prev; }; typedef struct _entry_ entry; struct _table_ { entry** entries; int size; }; typedef struct _table_ table; I initialise my table with calloc. void table_init(table* ht, int initial_size) { ht->entries = (entry**)calloc(initial_size, sizeof(entry*)); if (ht->entries) { ht->size = initial_size; } } Now my free function that i wrote void table

freeing allocated memory

风流意气都作罢 提交于 2019-12-24 10:39:02
问题 gcc 4.4.5 c89 I have a function called create_object where I allocate memory for a global structure. And I have a function called destroy_object where I check that the pointer is not null, then I free. Just incase I free memory that hasn't been allocated. However, I have tested this by making 2 consecutive calls to destroy_object. However, I get a stack dump on the second call. However, I am sure that it would not free as I have assigned the pointer to NULL. So it should skip the free

Freeing array of structs inside struct

允我心安 提交于 2019-12-24 08:38:43
问题 I have two structs struct obj_t { int id; float x; float y; }; struct cluster_t { int size; int capacity; struct obj_t *obj; }; As you can see, there is pointer to first obj_t inside cluster_t What I want to do is to free every obj_t from array inside cluster_t Do I have to write it with for loop like this? void clear_cluster(struct cluster_t *c) { for(int i = 0; i<c->size;i++) { free(&c->obj[i]); } free(c->obj); } Or is it ok to free the memory just like this ? void clear_cluster(struct

How does free function on pointer in C work? [duplicate]

流过昼夜 提交于 2019-12-24 05:24:33
问题 This question already has answers here : C - What Happens To Memory After free()? [duplicate] (3 answers) Can still print a string after I freed it? (2 answers) C - Accessing data AFTER memory has been free()ed? (2 answers) Still able to access a struct that has been freed [duplicate] (2 answers) Why freed struct in C still has data? (7 answers) Closed last year . Have a look at this C code: #include<stdio.h> #include<stdlib.h> int main(){ int *ptr_one; ptr_one = (int *)malloc(sizeof(int));

How do I know when I ought to free strings in C returned by library functions?

时光毁灭记忆、已成空白 提交于 2019-12-24 04:56:12
问题 Which strings ought I to free in C on my own, using free() ¹? My state of knowledge: char a[256]; : no char *a = "abcdefg"; : no char *a = malloc(15L); : yes a string returned by getenv() : no strings returned by Windows functions²: ??? ¹ or LocalFree()/ GlobalFree() / VirtualFree() ² in particular by GetCommandLineW() 回答1: This will always be mentioned in the documentation for any API you use that returns strings (or other data larger than a single simple value such as an integer). Yes, this

cudaFree is not freeing memory

淺唱寂寞╮ 提交于 2019-12-24 03:07:40
问题 The code below calculates the dot product of two vectors a and b. The correct result is 8192. When I run it for the first time the result is correct. Then when I run it for the second time the result is the previous result + 8192 and so on: 1st iteration: result = 8192 2nd iteration: result = 8192 + 8192 3rd iteration: result = 8192 + 8192 and so on. I checked by printing it on screen and the device variable dev_c is not freed. What's more writing to it causes something like a sum, the result

How does std::vector copy objects to its internal storage

三世轮回 提交于 2019-12-24 02:27:08
问题 I have the following problem: void add(){ cRow Row(); Row.add("Column", "Value"); std::vector<cRow> mRows; mRows.push_back(Row); } cRow::cRow(): mCol(NULL), mVal(NULL) { } cRow::add(const char* Col, const char* Val){ mCol = strdup(Col); mVal = strdup(Val); } cRow::~cRow(){ free(mCol); free(mVal); } After adding the local variable Row to the vector, the destructor is called for that Row and the strings are freed. Obviously, the pointers to the strings of the stored row in the vector are now