free

How to free a control in its own event?

谁说我不能喝 提交于 2019-12-10 16:37:52
问题 I have a list of TPanels in a FMX application and I want to free a panel if I click on it. To free them directly in the Onclick-handler is not the right way, because I get an access-violation. And I don't want to use windowsmessages (recommended in How to free control inside its event handler? and Why does my program crash when I destroy a button in its own OnClick handler?) because it is a firemonkey application and I do not know how these messages work on android and mac. Is there another

Why no memory leak?

本小妞迷上赌 提交于 2019-12-10 15:43:19
问题 The following is designed to take a variable length constant char and print it out in a nice format for logging. I am certain readers will have suggestions on how this can be improved, and I'd welcome it. What puzzles me is that I expected it would be necessary to free() the returned static char each time ToHexString() is called. Instead, I see no memory leak whatsoever. Even tho I use the function inline and therefore do not assign its return value to a variable. I created a simple test that

does free() follow pointers?

柔情痞子 提交于 2019-12-10 13:54:55
问题 I'm sure it doesn't, but maybe there's black magic in it, so here's my question: If I have a struct like this: struct mystr { char * strp, unsigned int foo, }; and I allocate memory for it and want to release it later. Do I have to do free(mystr_var->strp); free(mystr_var); or is the last line enought, does the free() function follow the pointers and free them two? 回答1: Every individually allocated block of memory must be freed individually. free() will only free the memory block that the

How does C free() work? [duplicate]

↘锁芯ラ 提交于 2019-12-10 02:23:27
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How malloc() and free() work #include <stdio.h> #include <stdlib.h> int * alloc() { int *p = (int *)calloc(5,4); printf("%d\n",p); return p; } int main() { int *p = alloc(); free(p); printf("%d\n",p); p[0] = 1; p[1] = 2; printf("%d %d\n",p[0],p[1]); } As to the code segment, I allocate 5 ints,first. And then I free the memory. When I printf p, why does p sill have a value same to the memory address allocated

How to use strtok in C properly so there is no memory leak?

我们两清 提交于 2019-12-10 01:50:23
问题 I am somewhat confused by what happens when you call strtok on a char pointer in C. I know that it modifies the contents of the string, so if I call strtok on a variable named 'line', its content will change. Assume I follow the bellow approach: void function myFunc(char* line) { // get a pointer to the original memory block char* garbageLine = line; // Do some work // Call strtok on 'line' multiple times until it returns NULL // Do more work free(garbageLine); } Further assume that 'line' is

Determining Whether Pointer is Valid

非 Y 不嫁゛ 提交于 2019-12-10 01:16:08
问题 It has been my observation that if free( ptr ) is called where ptr is not a valid pointer to system-allocated memory, an access violation occurs. Let's say that I call free like this: LPVOID ptr = (LPVOID)0x12345678; free( ptr ); This will most definitely cause an access violation. Is there a way to test that the memory location pointed to by ptr is valid system-allocated memory? It seems to me that the the memory management part of the Windows OS kernel must know what memory has been

Weird behavior of malloc()

自古美人都是妖i 提交于 2019-12-09 23:18:00
问题 Trying to understand answers to my question what happens when tried to free memory allocated by heap manager, which allocates more than asked for? I wrote this function and puzzled by its output int main(int argc,char **argv){ char *p,*q; p=malloc(1); strcpy(p,"01234556789abcdefghijklmnopqrstuvwxyz"); //since malloc allocates atleast 1 byte q=malloc(2); // free(q); printf("q=%s\n",q); printf("p=%s\n",p); return 0; } Output q=vwxyz p=01234556789abcdefghijklm! Can any one explain this behavior?

Is it always a good practice to set pointers to NULL after free()-ing them? [duplicate]

北城余情 提交于 2019-12-09 16:38:58
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Setting variable to NULL after free … I am learning about good C programming practices and my friend told me to always set the pointers to NULL after free()ing them (or calling a specific freeing function). For example: char* ptr = malloc(100); ... free(ptr); ptr = NULL; or struct graph* graph = create_graph(); ... destroy_graph(graph); graph = NULL; Why is this a good practice? Update: After reading the answers

Why do I have to use free on a pointer but not a normal declaration?

安稳与你 提交于 2019-12-09 14:50:14
问题 Why do I have to use free() when I declare a pointer such as: int *temp = (int*)malloc(sizeof(int)) *temp = 3; but not when I do: int temp = 3; 回答1: Normal declarations are put on the stack. When the function returns the stack pointer reverts to the value it had before the function was called, so the memory is automatically reclaimed. Malloc-based declarations are allocated from the 'heap', which requires the programmer to manage allocations and deallocations. 回答2: You don't always have to

Why does free() only set the 8 first bytes to zero?

狂风中的少年 提交于 2019-12-09 06:50:28
I've a question about free() behavior. Juste a simple code which use malloc and strcpy a char*. So, all is set on the HEAP : (gdb) x/100b 0x602010 0x602010: 66 111 110 106 111 117 114 32 0x602018: 116 111 117 116 32 108 101 32 0x602020: 109 111 110 100 101 0 0 0 0x602028: 0 0 0 0 0 0 0 0 0x602030: 0 0 0 0 0 0 0 0 0x602038: 33 0 0 0 0 0 0 0 When I free the chunk with free(), the result is : (gdb) x/100b 0x602010 0x602010: 0 0 0 0 0 0 0 0 0x602018: 116 111 117 116 32 108 101 32 0x602020: 109 111 110 100 101 0 0 0 0x602028: 0 0 0 0 0 0 0 0 0x602030: 0 0 0 0 0 0 0 0 0x602038: 33 0 0 0 0 0 0 0