free

C - Design your own free( ) function

a 夏天 提交于 2019-12-18 11:48:27
问题 Today, I appeared for an interview and the interviewer asked me this, Tell me the steps how will you design your own free( ) function for deallocate the allocated memory. How can it be more efficient than C's default free() function ? What can you conclude ? I was confused, couldn't think of the way to design. What do you think guys ? EDIT : Since we need to know about how malloc() works, can you tell me the steps to write our own malloc() function 回答1: That's actually a pretty vague question

What is the difference between freeing the pointer and assigning it to NULL?

空扰寡人 提交于 2019-12-18 11:45:09
问题 Could somebody tell me the difference between: int *p; p=(int*)malloc(10*sizeof(int)); free(p); or int *p; p=(int*)malloc(10*sizeof(int)); p=NULL; 回答1: free will deallocate the memory that p points to - simply assigning it to NULL will not (and thus you will have a memory leak). It is worth mentioning that it is good practice to assign your pointer to NULL AFTER the call to free , as this will prevent you from accidentally trying to access the freed memory (which is still possible, but

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

我只是一个虾纸丫 提交于 2019-12-18 09:03:11
问题 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? 回答1: 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

Freeing malloced structure in a function

别等时光非礼了梦想. 提交于 2019-12-18 08:55:56
问题 I'm creating a source files containing buffer functionality that I want to use for my other library that I'm creating. It is working correctly but I'm having trouble getting rid of the buffer structure that I'm creating in one of the functions. The following snippets should help illustrate my problem: C header: //dbuffer.h ... typedef struct{ char *pStorage; int *pPosition; int next_position; int number_of_strings; int total_size; }DBUFF; ... C source: //dbuffer.c ... DBUFF* dbuffer_init(char

In C, is it necessary to free a pointer at exit? [duplicate]

南笙酒味 提交于 2019-12-18 08:49:45
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: When you exit a C application, is the malloc-ed memory automatically freed? In C, is it necessary to free a pointer at exit? When the program exists, does it free memory from pointers still pointing to an allocated block? Is it dependent on the OS? 回答1: From what I know, for the most part, the OS will free any process memory when the process terminates, at least under most common user OSes (Windows, Linux, etc).

Is it legal to use the well-known free memory code in ipad/iphone app? [closed]

∥☆過路亽.° 提交于 2019-12-18 03:04:07
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Memory is an issue for iphone ipad app, especially for ipad, if one wants to do something big. Well, these two snippets of codes can 1. get current available memory of the device; 2. force free memory. Can we use it in the app? Will appstore allow this? #import <mach/mach.h> #import <mach/mach_host.h> static

Is this a good way to free memory?

不打扰是莪最后的温柔 提交于 2019-12-17 22:57:57
问题 The function for freeing an instance of struct Foo is given below: void DestroyFoo(Foo* foo) { if (foo) free(foo); } A colleague of mine suggested the following instead: void DestroyFoo(Foo** foo) { if (!(*foo)) return; Foo *tmpFoo = *foo; *foo = NULL; // prevents future concurrency problems memset(tmpFoo, 0, sizeof(Foo)); // problems show up immediately if referred to free memory free(tmpFoo); } I see that setting the pointer to NULL after freeing is better, but I'm not sure about the

Can I free() static and automatic variables in C?

ぃ、小莉子 提交于 2019-12-17 20:49:47
问题 The code is as follow : #include <stdlib.h> int num = 3; // Static external variable int *ptr = &num; int main(void) { int num2 = 4; // Automatic variable int *ptr2 = &num2; free(ptr); //Free static variable free(ptr2); //Free automatic variable return 0; } I try to compile the above code and it works, I'm curious does the free() function able to free both the static variable and also automatic variable? Or basically it does nothing? 回答1: Calling free() on a pointer not returned by memory

Why doesn't CudaFree seem to free memory?

淺唱寂寞╮ 提交于 2019-12-17 16:37:13
问题 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

Code for malloc and free

不羁的心 提交于 2019-12-17 15:58:14
问题 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. 回答1: 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