sk0102 sk0102 - 3 months ago 20
C Question

pthread argment address is different than before

I create 2 threads, and main() will pass argc and argv to thread.

so i use struct arg that contain argc and argv.

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct arg {
int argc;
char id[9];
char **argv;
};

int test1(struct arg *a) {
int argc;
char **argv;
argc = a->argc;
argv = a->argv;

if (argc > 3) {
printf("arg 1 is %s, arg 2 is %s arg 3 is %s\n\n", *(argv+1), *(argv+2),a->id);
}
else {
exit(0);
}


return 0;
}
int main(int argc, const char *argv[]) {
int i;
void *ret;
struct arg *a = (struct arg *) malloc(sizeof(struct arg *) * atoi(argv[3]));
pthread_t *thread = (pthread_t *) malloc(sizeof(*thread) * atoi(argv[3]));

for(i = 0; i < atoi(argv[3]); ++i) {
a[i].argc = argc;
a[i].argv = argv;
sprintf(a[i].id,"%d",i);
pthread_create( &thread[i], NULL , test1 ,(struct arg *)&(a[i]));
}

for(i = 0; i < atoi(argv[3]); ++i) {
pthread_join( thread[i], &ret);
}

return 0;
}


I exec my progam
./test 1 2 2

output look like :

arg 1 is 1, arg 2 is 2 arg 3 is 0

arg 1 is , arg 2 is arg 3 is 1


In thread 1 the argv is correct

but in thread 2 the argv address is different with before.

i use gdb to print a->argv address

in main argv address is 0x7fffffffdee8

in thread 1 argv address is 0x7fffffffdee8

in thread 2 argv address is 0x7ffff6fef700

[Switching to Thread 0x7ffff6fef700 (LWP 19472)]

argv address is the same as thread address

i check arg[1]->argv before pthread_create

the address is 0x7fffffffdee8.

pthread_create will change address??

Is that something i missing??

thank you.

Answer

In this line, you are allocating too little space:

struct arg *a = (struct arg *) malloc(sizeof(struct arg *) * atoi(argv[3]));

It should be more like:

struct arg *a = (struct arg *) malloc(sizeof(*a) * atoi(argv[3]));

You shouldn't tamper with the definition of main(); the const is not part of its type (see What should main() return in C and C++, which also covers the arguments as well as the return value).

You should also make your thread function conform to void *test1(void *vp); that's the type of a thread function. For example:

static void *test1(void *vp)
{
    struct arg *a = vp;
    int argc = a->argc;
    char **argv = a->argv;
    …

The cast in the pthread_create() call is unwarranted.