I have “segmentation fault” in my code. I’m curious if I allocate some space using “malloc()” in a function. After the function finished, is the space still valid?
Further question, when a create a child thread like this, I suppose it exists even after its parent exits. I’m using GCC in Linux.
void foo(){
void *child_stack;
child_stack=(void*)malloc(16384);
child_stack += 16384;
clone((void*)do_function,child_stack,0,NULL);
}
Memory created with malloc goes on the heap, which is available until it is explicitly destroyed with free. That’s the main reason to use malloc instead of creating variables on the stack, which are destroyed when they fall out of scope (eg, a function returning) and the stack frame is destroyed. The segfault is probably because you’re allocating 16384 bytes, then moving the pointer forward 16384 with the += (why are you doing this?) before passing it to a function that presumably tries to access it.
I’m not 100% sure, but From the wait() man page,