free

Allocate memory and save string in c

空扰寡人 提交于 2019-11-30 04:44:33
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 approach to save a string on the heap? Is the following a common way? int main(int argc, char **argv) {

How to free a struct that contains only pointers

点点圈 提交于 2019-11-30 02:22:32
I have a struct which you see below: typedef struct _List { Person *person; // pointer for people list DoList *do; // Kinda timer, for checking list in some intervals } List; Are there any need to free this struct? If so, how can i free it? You have to free the struct if you allocated it dynamically. You have to free its members before deallocating the struct if you allocated the members dynamically and don't have a reference to them anywhere else. Here are some examples: void freeNotRequiredHere() { List nonDynamicList; Person nonDynamicPerson; DoList nonDynamicDoList; nonDynamicList.person =

How can I do automatic memory management in C?

拜拜、爱过 提交于 2019-11-29 18:43:30
问题 In C memory allocation/deallocation done by malloc and free . In C++ memory allocation/deallocation done by new and delete . There are some solutions in C++ for automatic memory management like: Smart Pointers. RAII (Resource Acquisition Is Initialization) Reference counting and cyclic references ... But how can I do automatic memory management in C? Is there any solutions for AUTOMATIC memory management in C? Is there any guidelines or something like that for C? I want when I foget free a

Free allocated memory before return a function

偶尔善良 提交于 2019-11-29 18:22:59
问题 I am trying to return an array using malloc in a function: char* queueBulkDequeue(queueADT queue, unsigned int size) { unsigned int i; char* pElements=(char*)malloc(size * sizeof(char)); for (i=0; i<size; i++) { *(pElements+i) = queueDequeue(queue); } return pElements; } The problem is that I need to free it because my MCU's heap size is limited. But I want to return it so I cannot free it in the function, right?. Can I free the allocated memory outside the function (where I call the function

I call free(), but the pointer still has data and it's content hasn't changed

纵然是瞬间 提交于 2019-11-29 18:15:34
The code is below. My question is about the result. I want to understand, why after calling free(p) p->elem turns to '0', but the p->str still contains "hello"? #define LEN (sizeof(struct node)) typedef struct node { int elem; char *str; }*p_node; int main(void) { p_node p; p=(p_node)malloc(LEN); p->elem=99; p->str="hello"; printf("the p->elem:%d\n",p->elem); printf("the p->str :%s\n",p->str); free(p); printf("the p->elem:%d\n",p->elem); printf("the p->str :%s\n",p->str); return 0; } Some programmer dude Freeing memory doesn't actually clear the pointer or the memory it pointed to (except

C++ pointer array is still accessible after delete[] is called [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-29 17:58:29
This question already has an answer here: c++ delete pointer issue, can still access data [closed] 6 answers C++ delete - It deletes my objects but I can still access the data? 13 answers Can a local variable's memory be accessed outside its scope? 20 answers In the following code, delete[] is called once to free up the memory allocated by new . However, the array elements is still accessible after delete[] is called. I called delete[] twice to confirm that I am getting a double free or corruption error, which I am getting, which means the memory is freed. If the memory is freed, how am I able

double free or corruption 3d array in C

蓝咒 提交于 2019-11-29 17:26:02
I get a 'double free or corruption' error while freeing up an 3d array. Can anyone please tell me where is the problem in the code? The size of the array is 2*N*N. Value of N here is 100. Even without casting, same result. Here is the code: // Mallocing double ***h = malloc(2 * (sizeof(double**))); for(i = 0; i < N; i++) { h[i] = malloc(N * sizeof(double*)); for(j = 0; j < N; j++) { h[i][j] = malloc(N * sizeof(double)); } } // Freeing for(i = 0; i < N; i++) { for(j = 0; j < N; j++) { free(h[i][j]); } free(h[i]); } free(h); The program works fine but at the end I get an error 'double free or

Does malloc reserve more space while allocating memory?

梦想与她 提交于 2019-11-29 15:58:45
问题 I am observing the following behavior in my test program: I am doing malloc() for 1 MB and then free() it after sleep(10) . I am doing this five times. I am observing memory consumption in top while the program is running. Once free() -d, I am expecting the program's virtual memory (VIRT) consumption to be down by 1 MB. But actually it isn't. It stays stable. What is the explanation for this behavior? Does malloc() do some reserve while allocating memory? 回答1: Once free() -d, I am expecting

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

一曲冷凌霜 提交于 2019-11-29 15:22:33
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? 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 behavior Once the function has finished, the program stops reserving the memory location where x is stored, any

Is it up to the programmer to deallocate on exit()?

走远了吗. 提交于 2019-11-29 15:12:27
I have a program and when I input wrong data from the keyboard it just exits with exit(1) . I was testing with Valgrind and while this happens there are no errors, but I can see that there are still reachable x bytes. So my question: Is it up to the programmer to free memory before hitting an exit() or is the OS going to take care of it? It's a good idea (and in old enough versions of Windows, it was essential), but when a program exit() s on modern operating systems its entire address space is reclaimed. In the end, the OS will take care of it (on every modern OS, it was not the case with