简体   繁体   中英

passing pointer char argument to function in thread

When I execute this code, I'm receiving a "segmentation error (core dumbed)".

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

void function(char *oz){

    char *y;
    y = (char*)oz;
    **y="asd";


    return NULL;
}

int main(){
    char *oz="oz\n";

    pthread_t thread1;

    if(pthread_create(&thread1,NULL,function,(void *)oz)){
        fprintf(stderr, "Error creating thread\n");
        return 1;
    }

    if(pthread_join(thread1,NULL)){
        fprintf(stderr, "Error joining thread\n");
        return 2;
    }
    printf("%s",oz);
    return 0;

}

First you need to decide, how you want to manage the memory: is the memory allocated by caller, or inside the thread function.

If the memory is allocated by caller, then the thread function will look like:

void *function(void *arg)
{
    char *p = arg;
    strcpy(p, "abc"); // p points to memory area allocated by thread creator
    return NULL;
}

Usage:

char data[10] = "oz"; // allocate 10 bytes and initialize them with 'oz'
...
pthread_create(&thread1,NULL,function,data);

If the memory is allocated inside the thread function, then you need to pass pointer-to-pointer:

void *function(void *arg)
{
    char **p = (char**)arg;
    *p = strdup("abc"); // equivalent of malloc + strcpy
    return NULL;
}

Usage:

char *data = "oz"; // data can point even to read-only area
...
pthread_create(&thread1,NULL,function,&data); // pass pointer to variable
...
free(data); // after data is not needed - free memory-

Your compiler might be warning you about some of these problems. Don't ignore compiler warnings! Warnings are the way your compiler tells you " Hold on! I don't quite understand this bit of code... Please put it in a language I understand! ". Which book are you reading? I see other problems, which I won't mention in this question, arising from this approach. It's not safe to learn C by "trial and error", or by "misguided example" (eg. copying/pasting and modifying). You should be reading a book written by a professor who teaches programming for a living, and has a bloody good reputation for doing so. Please answer this question before reading any further.

pthread_create expects a function pointer that indicates a function with a signature like this:

void *function(void *argument);

The function pointer you're giving it will be converted to a function pointer that assumes that signature. pthread_create will then try to use that function pointer, the only way it knows how... This is undefined behaviour. ... and besides, as indicated by @AdamRosenfield, you can't return values from a function that returns void . This ought to be causing your compiler to spew errors and warnings at you. Your function should look like:

void *function(void *oz) {
    /* ... */
}

In the block comment is another series of lies in your question. Your code doesn't segfault, because it doesn't compile. y is a char * ... *y is a char ... **y is a compiler error. I would assume that you only meant to dereference it once, but then assigning "asd" to a char makes no sense, either, so I must assume you meant to declare y as char **y; . You seem to be using typecasts superfluously. Learn about them before you use them, so that you don't misuse them or use them in places where you don't need to (such as trying to convert a char * to a char * ... wtf?).

void *function(void *oz) {
    char **y = oz;
    *y = "asd";
}

Hence, you should probably be passing pthread_open a char ** value, too. It makes no sense to pass a char * value and then treat it as though it's a char ** value, right? There's no need to cast any pointer to object to a void * ; That conversion is one of the automatic ones.

if (pthread_create(&thread1, NULL, function, &oz)){
    fprintf(stderr, "Error creating thread\n");
    return 1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM