free

Can still print a string after I freed it?

半世苍凉 提交于 2020-02-27 23:20:39
问题 I am learning and testing memory allocation in C and I want to test what happens if free() is called. I expected there could be a segmentation fault or pointer is NULL after I run the program below. However, I can still successfully print the string as in Output. I also tried to free str twice, then an error as Output 2 occurred. It seems the previously allocated memory is successfully deallocated, but the data on the memory is not cleaned up. Is that correct? If that is the case, when will

free() not freeing up memory properly?

不打扰是莪最后的温柔 提交于 2020-01-30 05:30:48
问题 I'm trying to free up the memory I've allocated with malloc , but the free command doesn't seem to do its job properly according to Eclipse's debugger. How's this possible? Below is a screenshot of my debugger after it supposedly freed up seCurrent->student->year , which is clearly not the case. year was allocated using malloc . alt text http://img693.imageshack.us/img693/7840/codeo.png 回答1: free() does not normally change any values in your program - it just makes adjustments to the C

Implementation of Realloc in C

廉价感情. 提交于 2020-01-28 10:08:16
问题 int getmin(int a, int b) { return a<b?a:b; } void *reallocation(void *ptr, size_t size) //size_t in bytes { void *newptr; int msize; msize = getsize(ptr); msize = getmin(msize, size); printf("msize = %d", msize); newptr = malloc(size); newptr = memcpy(newptr, ptr, msize); free(ptr); return newptr; } I have implemented my own realloc, and in order to get the size of the allocated memory using malloc(however i know there isn't any method for this in c). My reallocation function is working fine

Problems with LD_PRELOAD and calloc() interposition for certain executables

ⅰ亾dé卋堺 提交于 2020-01-22 12:51:06
问题 Relating to a previous question of mine I've successfully interposed malloc , but calloc seems to be more problematic. That is with certain hosts, calloc gets stuck in an infinite loop with a possible internal calloc call inside dlsym . However, a basic test host does not exhibit this behaviour, but my system's "ls" command does. Here's my code: // build with: g++ -O2 -Wall -fPIC -ldl -o libnano.so -shared Main.cc #include <stdio.h> #include <dlfcn.h> bool gNanoUp = false;// global //

Calling free on a pointer twice

独自空忆成欢 提交于 2020-01-22 07:09:34
问题 I have been taught in lectures, that calling free() on a pointer twice is really, really bad. I know that it is good practice, to set a pointer to NULL , right after having freed it. However, I still have never heard any explanation as to why that is. From what I understand, the way malloc() works, it should technically keep track of the pointers it has allocated and given you to use. So why does it not know, whether a pointer it receives through free() has been freed yet or not? I would love

Calling free on a pointer twice

喜欢而已 提交于 2020-01-22 07:09:19
问题 I have been taught in lectures, that calling free() on a pointer twice is really, really bad. I know that it is good practice, to set a pointer to NULL , right after having freed it. However, I still have never heard any explanation as to why that is. From what I understand, the way malloc() works, it should technically keep track of the pointers it has allocated and given you to use. So why does it not know, whether a pointer it receives through free() has been freed yet or not? I would love

Using free() on a copy of the actual pointer is acceptable / correct?

冷暖自知 提交于 2020-01-17 08:20:29
问题 Is this: int *a = malloc (sizeof (int) ); int *b = a; free (b); the same as this: int *a = malloc (sizeof (int) ); free (a); If yes, no need to explain, but if no, please elaborate why not! 回答1: Yes, they are equivalent. Quoting C11 , chapter §7.22.3.3, ( emphasis mine ) The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier

Is there a way to overwrite the malloc/free function in C?

微笑、不失礼 提交于 2020-01-14 09:43:11
问题 Is there a way to hook the malloc/free function call from a C application it self? 回答1: Yes you can. Here's an example program. It compiles and builds with gcc 4.8.2 but does not run since the implementations are not functional. #include <stdlib.h> int main() { int* ip = malloc(sizeof(int)); double* dp = malloc(sizeof(double)); free(ip); free(dp); } void* malloc(size_t s) { return NULL; } void free(void* p) { } 回答2: malloc() and free() are defined in the standard library; when linking code,

Basic Malloc/Free

喜你入骨 提交于 2020-01-11 09:52:33
问题 If I have a snippit of my program like this: struct Node *node; while(...){ node = malloc(100); //do stuff with node } This means that every time I loop through the while loop I newly allocate 100 bytes that is pointed to by the node pointer right? If this is true, then how do I free up all the memory that I have made with all the loops if I only have a pointer left pointing to the last malloc that happened? Thanks! 回答1: Please allocate exactly the size you need: malloc(sizeof *node); -- if

Printing pointer addresses in C [two questions]

谁说我不能喝 提交于 2020-01-11 01:46:31
问题 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()