free

C Programming: malloc and free within a loop

社会主义新天地 提交于 2019-12-01 06:10:15
I just started out with C and have very little knowledge about performance issues with malloc() and free() . My question is this: if I were to call malloc() followed by free() inside a while loop that loops for, say, 20 iterations, would it run slower compared to calling free() outside the loop? I am actually using the first method to allocate memory to a buffer, read a variable-length string from a file, perform some string operations, and then clear the buffer after every iteration. If my method results in a lot of overhead then I'd like to ask for a better way for me to achieve the same

Is this a double free in C?

☆樱花仙子☆ 提交于 2019-12-01 06:02:19
问题 Normally, if a pointer is freed twice, it's a double free. For example, char *ptr; ptr=malloc(5 * sizeof(*ptr)); free(ptr); free(ptr); The above code is considered as double free. Is the following considered as double free as well? char *ptr; char *ptr1; ptr=malloc(5 * sizeof(*ptr)); ptr1=ptr; free(ptr); free(ptr1); Thank you. 回答1: Yes. The library doesn't care what name you gave a varaible in your source code (it's long gone by the time the code is executed). All that matters is the value,

Exists a way to free memory in atexit or similar without using global variables?

≯℡__Kan透↙ 提交于 2019-12-01 05:54:01
I am developing a project in C, and I need to free the allocated memory and also close all the open files before it exits. I decided to implement a clean function that will do all this stuff and call it with atexit because there are a lot of possible exit scenarios. The problem is that atexit doesn't allow me to set functions with parameters, so I can't send to clean the pointers that need to be freed in the end of the process. So I need to declare as global variables every pointer that may need to be freed, and every file that may remaining open in the program? (I already did that but doesn't

When to use variable length array in C, but when a dynamic allocation?

ⅰ亾dé卋堺 提交于 2019-12-01 05:32:27
I find out about Variable Length Array in C99, but it looks like it behave almost the same as malloc + free. The practical differences I found: Too big array handling: unsigned size = 4000000000; int* ptr = malloc(size); // ptr is 0, program doesn't crash int array[size]; // segmentation fault, program crashes Memory leaks: only possible in dynamic array allocation: int* ptr = malloc(size); ... if(...) return; ... free(ptr); Life of object and possibility to return from function: dynamically allocated array lives until the memory is frees and can be returned from function which allocated the

When to use variable length array in C, but when a dynamic allocation?

倖福魔咒の 提交于 2019-12-01 03:01:39
问题 I find out about Variable Length Array in C99, but it looks like it behave almost the same as malloc + free. The practical differences I found: Too big array handling: unsigned size = 4000000000; int* ptr = malloc(size); // ptr is 0, program doesn't crash int array[size]; // segmentation fault, program crashes Memory leaks: only possible in dynamic array allocation: int* ptr = malloc(size); ... if(...) return; ... free(ptr); Life of object and possibility to return from function: dynamically

Can a call to free() in C ever fail?

≯℡__Kan透↙ 提交于 2019-12-01 02:43:42
Can a call to free() fail in any way? For example: free(NULL); E.Benoît Freeing a NULL pointer cannot fail. And free doesn't return any error, but freeing unallocated memory, already freed memory or the middle of an allocated block is undefined behaviour - it may cause a memory error and the program may abort (or worse, it will corrupt the heap structure and crash later). Or, even worse than that, keep running but totally corrupt your data and write it to disk without you realising :-) The relevant portion of the standard (C99) is section 7.20.3.2 : #include <stdlib.h> void free(void *ptr);

How to free and garbage collect a WebGL context?

感情迁移 提交于 2019-12-01 00:06:33
问题 I'm developing a WebGL application for web and mobile. I often use hard-refreshs to test the results of my WebGL implementation. After a view tries, I get the error: Error: WebGL: Exceeded 16 live WebGL contexts for this principal, losing the least recently used one. This does not appear on a fresh started browser, but after refreshing the site multiple times. I guess the WebGL contexts are not finished, released, destroyed, cleaned up, freed correctly. How can I do this? Khronos Group

Freeing of allocated memory in Solaris/Linux

可紊 提交于 2019-11-30 22:40:18
I have written a small program and compiled it under Solaris/Linux platform to measure the performance of applying this code to my application. The program is written in such a way, initially using a sbrk(0) system call, I have taken base address of the heap region. After that I have allocated 1.5 GB of memory using a malloc system call, Then I used a memcpy system call to copy 1.5 GB of content to the allocated memory area. Then, I freed the allocated memory. After freeing, I used the sbrk(0) system call again to view the heap size. This is where I get a little confused. In Solaris, even

Printing pointer addresses in C [two questions]

大兔子大兔子 提交于 2019-11-30 19:08:55
I know that my questions are very simple but googleing them didn't get me any useful results... They'r probably too simple!! No. 1 char* createStr(){ char* str1 = malloc(10 * sizeof(char)); printf("str1 address in memory : %p\n", &str1); return str1; } int main(void){ char* str2 = createStr(); printf("str2 address in memory : %p\n", &str2); } Result: str1 address in memory : 0x7fffed611fc8 str2 address in memory : 0x7fffed611fe8 Why are the addresses different in and out of the createStr() function and how can I free(str1)??? No. 2 int main(int argc, int *argv[]){ printf("Basename is %s ",

Freeing of allocated memory in Solaris/Linux

瘦欲@ 提交于 2019-11-30 17:50:23
问题 I have written a small program and compiled it under Solaris/Linux platform to measure the performance of applying this code to my application. The program is written in such a way, initially using a sbrk(0) system call, I have taken base address of the heap region. After that I have allocated 1.5 GB of memory using a malloc system call, Then I used a memcpy system call to copy 1.5 GB of content to the allocated memory area. Then, I freed the allocated memory. After freeing, I used the sbrk(0