I need to create a program to do a threaded merge sort (kinda), but in the meantime I am trying to pass a struct through the the Merge method (thread runs this method), but I keep getting seg faults. I am sure its something obvious, please help me out.

Here is my code:

* FILE: hello_arg1.c
*   A "hello world" Pthreads program which demonstrates one safe way
*   to pass arguments to threads during thread creation.
* AUTHOR: Blaise Barney
* LAST REVISED: 01/29/09
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#include <string>
#define NUM_THREADS	8

using namespace std;

struct t_data
    int id;
    vector<string> v;

void *Merge(void *transmit)
   int tid;

   struct t_data *test = (struct t_data *) transmit;
   tid = test->id;
   vector<string> names;
   names = test->v;

   cout << endl << names[0];


int main(int argc, char *argv[])

struct t_data test;
test.id = 5;

for(int a = 0; a < argc; a++)

cout << endl << endl;

//The vector works fine this side
for(int a = 0; a < argc; a++)
    cout << endl << test.v[a];

int rc;

pthread_t thread1;

  rc = pthread_create(&thread1, NULL, Merge, (void *) &test);

  if (rc) {
    printf("ERROR; return code from pthread_create() is %d\n", rc);


Recommended Answers

All 2 Replies

test is actually a pointer, you are sending a an address of an address, that give memory violation exception (seg fault)

rc = pthread_create(&thread1, NULL, Merge, (void *) test);

see I removed the "&" from the container "test"

Just a suggestion since you're using C++, why not use templates instead of void pointers ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.