分配结构堆与堆栈:Pthread\u create

分配结构堆与堆栈:Pthread\u create,c,multithreading,pthreads,stack,heap,C,Multithreading,Pthreads,Stack,Heap,我正在尝试为arg_结构动态分配内存。如果我只是在堆栈上分配它,它会正常工作,但不会动态工作。它动态地在主函数中打印字符串,但当它被传递到线程函数时,它不起作用。你知道为什么它不起作用吗 #include <stdio.h> #include <stdlib.h> #include <pthread.h> struct arg_struct { int *arg1; char *str; }; void *print_the_argume

我正在尝试为arg_结构动态分配内存。如果我只是在堆栈上分配它,它会正常工作,但不会动态工作。它动态地在主函数中打印字符串,但当它被传递到线程函数时,它不起作用。你知道为什么它不起作用吗

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>


struct arg_struct {
    int *arg1;
    char *str;
};

void *print_the_arguments(void *arguments)
{
    struct arg_struct *args = (struct arg_struct *)arguments;
    printf("The result is : %s\n",args->str);
    pthread_exit(NULL);
    return NULL;
}

int main()
{
    pthread_t some_thread;
    struct arg_struct *args = malloc(sizeof(struct arg_struct));
    //struct arg_struct args; // uncommenting this and args.str WORKS (prints to thread function)
    args->str = malloc(sizeof(char)*6);
    args->str = "hello";
    //args.str = "hello";
    printf("printing from main: %s\n", args->str); // Prints here but not in other function

    if (pthread_create(&some_thread, NULL, &print_the_arguments, (void *)&args) != 0) {
        printf("Uh-oh!\n");
        return -1;
    }

    return pthread_join(some_thread, NULL); /* Wait until thread is finished */

}
#包括
#包括
#包括
结构arg_结构{
int*arg1;
char*str;
};
void*打印参数(void*参数)
{
struct arg_struct*args=(struct arg_struct*)参数;
printf(“结果是:%s\n”,args->str);
pthread_exit(NULL);
返回NULL;
}
int main()
{
pthread\u t一些线程;
struct arg_struct*args=malloc(sizeof(struct arg_struct));
//struct arg_struct args;//取消对此的注释,args.str起作用(打印到线程函数)
args->str=malloc(sizeof(char)*6);
args->str=“你好”;
//args.str=“你好”;
printf(“从main打印:%s\n”,args->str);//在此处打印,但不在其他函数中打印
if(pthread_create(&some_thread,NULL,&print_)参数,(void*)&args)!=0){
printf(“Uh-oh!\n”);
返回-1;
}
返回pthread_join(some_thread,NULL);/*等待线程完成*/
}
这个

应该是

29     if (pthread_create(&some_thread, NULL, &print_the_arguments, (void *)args) != 0) {
动态分配参数时。还有,我建议

 25     args->str = strdup("hello");
并跳过
malloc

此代码:

 24     args->str = malloc(sizeof(char)*5);
 25     args->str = "hello";
导致内存泄漏,来自malloc的内存泄漏。也许你的意思是

 24     args->str = malloc(sizeof(char)*5);
 25     strcpy(args->str, "hello");
但这也是不正确的。5应该是6,以说明空字符 在字符串的末尾
strdup()
malloc
strcpy
的一个很好的快捷方式

应该是

29     if (pthread_create(&some_thread, NULL, &print_the_arguments, (void *)args) != 0) {
动态分配参数时。还有,我建议

 25     args->str = strdup("hello");
并跳过
malloc

此代码:

 24     args->str = malloc(sizeof(char)*5);
 25     args->str = "hello";
导致内存泄漏,来自malloc的内存泄漏。也许你的意思是

 24     args->str = malloc(sizeof(char)*5);
 25     strcpy(args->str, "hello");
但这也是不正确的。5应该是6,以说明空字符
在字符串的末尾
strdup()
malloc
的一个很好的快捷方式,后面跟着
strcpy
args->str=malloc(sizeof(char)*5);args->str=“你好”
分配五个字节(当您似乎想要6个字节时),然后当您将其他内容分配给
args->str
时,会立即丢失该内存。以后,请在发布之前删除这些行号。这是:
args->str=malloc(sizeof(char)*5);args->str=“你好”
分配五个字节(当您似乎想要6个字节时),然后当您将其他内容分配给
args->str
时,就会立即丢失该内存。以后,请在发布之前删除这些行号。+1、转换为
void*
、在
前面的
打印参数和
大小(char)
在OP的代码中也是不必要的。可能不需要,但不必要?该代码来自另一个stackoverflow页面;我只是稍微修改了一下。此外,该页面的强制转换为void*。该站点还使用sizeof(char):+1,强制转换为
void*
前面的
&
打印参数以及
大小(char)
在OP的代码中也不需要。可能不需要,但不需要?该代码来自另一个stackoverflow页面;我只是稍微修改了一下。此外,该页面的强制转换为void*。该站点还使用sizeof(char):