I'm very new to c++ and programming with pointers. I was trying to pass an argument to a thread, add one to it, and thereafter return a pointer to this result. The main thread should just print the result pointed to by the returned pointer.
#include<stdio.h>
#include<pthread.h>
#include<iostream>
using namespace std;
void* calculator(void* _n) {
int* n = (int*) _n;
int* i;
int result = *n + 1;
i = &result;
return i;
}
int main(){
int input;
pthread_t calcThread;
void* exitStatus;
int* threadResult;
cout << "Input integer: " << endl;
cin >> input;
cout << "Init thread..." << endl;
pthread_create(&calcThread, NULL, calculator, &input);
pthread_join(calcThread, &exitStatus);
// Error around here?
threadResult = (int*) exitStatus;
cout << "Returned: " << *threadResult << endl;
}
The code compiles, but I get a segmentation fault when executing. My guess is that it has something to do with the cast i'm doing, but i can't figure out what.
Any help will be greatly appreciated!