free


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()

Can I force a numpy ndarray to take ownership of its memory?

北战南征 提交于 2020-01-10 08:53:09
问题 I have a C function that mallocs() and populates a 2D array of floats. It "returns" that address and the size of the array. The signature is int get_array_c(float** addr, int* nrows, int* ncols); I want to call it from Python, so I use ctypes. import ctypes mylib = ctypes.cdll.LoadLibrary('mylib.so') get_array_c = mylib.get_array_c I never figured out how to specify argument types with ctypes. I tend to just write a python wrapper for each C function I'm using, and make sure I get the types

Why are the contents pointed to by a pointer not changed when memory is deallocated using free()?

佐手、 提交于 2020-01-10 05:15:20
问题 I am a newbie when it comes to dynamic memory allocation. When we free the memory using void free(void *ptr) the memory is deallocated but the contents of the pointer are not deleted. Why is that? Is there any difference in more recent C compilers? 回答1: Computers don't "delete" memory as such, they just stop using all references to that memory cell and forget that anything of value is stored there. For example: int* func (void) { int x = 5; return &x; } printf("%d", *func()); // undefined

Why do I get different results when I dereference a pointer after freeing it?

杀马特。学长 韩版系。学妹 提交于 2020-01-09 03:57:12
问题 I've a question about the memory management in C (and GCC 4.3.3 under Debian GNU/Linux). According to the C Programming Language Book by K&R, (chap. 7.8.5), when I free a pointer and then dereference it, is an error. But I've some doubts since I've noted that sometimes, as in the source I've pasted below, the compiler (?) seems to work according a well-defined principle. I've a trivial program like this, that shows how to return an array dynamically allocated: #include <stdio.h> #include

工具导航Map