Process 线程和主进程的堆栈地址

Process 线程和主进程的堆栈地址,process,pthreads,Process,Pthreads,我有一个关于线程和进程的内存地址的问题。问题是:- 在正常通话中 int func(int a, int b){ int c; c = a + b; return c; } int main(){ int ret = func(a,b); return 0; } 在上面对函数func的函数调用中,函数变量a和b将存储在堆栈中。如果我错了,请纠正我 另一种情况是,当我们从主进程创建线程时 void * func(void *dummy

我有一个关于线程和进程的内存地址的问题。问题是:- 在正常通话中

int func(int a, int b){
    int c;
    c = a + b;
    return c;
}         

int main(){
    int ret = func(a,b);
     return 0;
}
在上面对函数func的函数调用中,函数变量a和b将存储在堆栈中。如果我错了,请纠正我

另一种情况是,当我们从主进程创建线程时

void * func(void *dummy_ptr){
    int c;
    c = a + b;
    pthread_exit();
}         

int main(){
    pthread_t  id;
    int ret = pthread_create(&id, NULL, & func(), NULL);
    return 0;
}

我的问题是pthread_create的变量将存储在哪里。它是存储在main的堆栈上,还是存储在线程的堆栈上

pthread\u create
为堆中新线程的堆栈分配空间。因此
func
中的变量存储在线程堆栈中,线程堆栈本身位于程序堆中

变量
pthread\t id
是main的本地变量,因此必须在main的堆栈上创建它

main
完成执行并

  • main
    中没有
    pthread\u join
    可以等待线程终止
  • 线没有断开

main
退出,导致所有其他线程突然终止(终止)

是的,这是正确的。对于函数func()变量(在pthread_create api中调用),变量将存储在线程堆栈中,但是我们在api pthread_create()中传递的变量(如pthread_t id)呢,任何线程属性和最后一个参数都像整数或结构指针一样传递。您的示例不会编译,因为您没有声明
a
b