free

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

雨燕双飞 提交于 2019-11-29 00:47:25
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 void print_free_memory () { mach_port_t host_port; mach_msg_type_number_t host_size; vm_size_t pagesize; host_port = mach_host_self(); host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t); host_page_size(host_port, &pagesize); vm_statistics_data_t vm_stat; if (host_statistics(host

How to free a struct that contains only pointers

徘徊边缘 提交于 2019-11-28 23:15:35
问题 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? 回答1: 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

Why is my pointer not null after free?

自古美人都是妖i 提交于 2019-11-28 21:40:25
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? 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 pointer will not be NULL . This the same as if you were passing an int to a function instead. You wouldn't

Is this a good way to free memory?

旧城冷巷雨未停 提交于 2019-11-28 21:00:44
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 following: Do we really need to assign the pointer to a temporary one? Does it help in terms of concurrency

Can I assume that calling realloc with a smaller size will free the remainder?

北战南征 提交于 2019-11-28 20:19:57
Let’s consider this very short snippet of code: #include <stdlib.h> int main() { char* a = malloc(20000); char* b = realloc(a, 5); free(b); return 0; } After reading the man page for realloc, I was not entirely sure that the second line would cause the 19995 extra bytes to be freed. To quote the man page: The realloc() function changes the size of the memory block pointed to by ptr to size bytes. , but from that definition, can I be sure the rest will be freed? I mean, the block pointed by b certainly contains 5 free bytes, so would it be enough for a lazy complying allocator to just not do

LinkedList - How to free the memory allocated using malloc

你。 提交于 2019-11-28 18:40:15
问题 I have a very simple C code for constructing a Singly Linked list as below, in which I allocate memory for each node dynamically using malloc. At the end of code, I want to free the memory for each node allocated, was wondering how to go about it - If I start from head node first and free it, the pointers to the subsequent nodes are lost and memory leak happens. Other way is start from head node and keep storing the node pointer in a separate array of pointers or something, traverse the list

Why does `free` in C not take the number of bytes to be freed?

若如初见. 提交于 2019-11-28 16:46:51
Just to be clear: I do know that malloc and free are implemented in the C library, which usually allocates chunks of memory from the OS and does its own management to parcel out smaller lots of memory to the application and keeps track of the number of bytes allocated. This question is not How does free know how much to free . Rather, I want to know why free was made this way in the first place. Being a low-level language, I think it would be perfectly reasonable to ask a C programmer to keep track not only of what memory was allocated but how much (in fact, I commonly find that I end up

free() not deallocating memory?

巧了我就是萌 提交于 2019-11-28 14:44:42
free(str); printf("%d\n", str->listeners); The call to printf succeeds (as do any other calls to str's members). How is this possible? You're just (un)lucky. That code exhibits undefined behavior - anything can happen, including looking like the memory wasn't freed. The memory is freed, but there is no point in actively clearing it, so its original content is likely to still be there. But you can't rely on that. Here's an analogy for you: imagine you're renting an apartment (that's the memory) and you terminate your lease but keep a duplicate of the key (that's the pointer). You might be able

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

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 13:55:00
问题 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; } 回答1:

double free or corruption 3d array in C

时光怂恿深爱的人放手 提交于 2019-11-28 12:23:34
问题 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