How to use a struct pointer returned to the in the main from a thread function?What should main() return in C and C++?How do function pointers in C work?Is it possible to iterate over arguments in variadic macros?Program run in child process doesn't loopIn a c program, does the whole process terminates when the main thread terminate?pointer to array of structmalloc and pointer in a structHow to drain the java thread stack memory area?C Pass arguments as void-pointer-list to imported function from LoadLibrary()how to create a struct and pass to a new thread via pthread_create()
The economics of a "no deal" Brexit
How to retract the pitched idea from employer?
siunitx error: Invalid numerical input
How bad would a partial hash leak be, realistically?
What's the correct term for a waitress in the Middle Ages?
Secure offsite backup, even in the case of hacker root access
When writing an error prompt, should we end the sentence with a exclamation mark or a dot?
About the expansion of seq_set_split
How to skip replacing first occurrence of a character in each line?
How were concentration and extermination camp guards recruited?
Did Darth Vader wear the same suit for 20+ years?
Disclosing Spiritual Experiences
What LISP compilers and interpreters were available for 8-bit machines?
Does the "6 seconds per round" rule apply to speaking/roleplaying during combat situations?
After the loss of Challenger, why weren’t Galileo and Ulysses launched by Centaurs on expendable boosters?
Is it recommended against to open-source the code of a webapp?
Why does the Schrödinger equation work so well for the Hydrogen atom despite the relativistic boundary at the nucleus?
How to make a setting relevant?
Java guess the number
Why does Kathryn say this in 12 Monkeys?
Select items in a list that contain criteria #2
Bent spoke design wheels — feasible?
How did students remember what to practise between lessons without any sheet music?
Select items in a list that contain criteria
How to use a struct pointer returned to the in the main from a thread function?
What should main() return in C and C++?How do function pointers in C work?Is it possible to iterate over arguments in variadic macros?Program run in child process doesn't loopIn a c program, does the whole process terminates when the main thread terminate?pointer to array of structmalloc and pointer in a structHow to drain the java thread stack memory area?C Pass arguments as void-pointer-list to imported function from LoadLibrary()how to create a struct and pass to a new thread via pthread_create()
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am writing a program that takes integers as command line arguments. For each of these numbers I have to create a thread which calculates Fibonacci series up to that number. That function returns a struct pointer to the main where the data is printed.
Now, I have correctly done the fib calculations and checked them by printing the series within the function.
The problem arises when I try to return the struct pointer created within the thread function and use it to print the data in the main.
typedef struct thread_func_param
int *fib;
int size;
thread_func_param;
//===================================================
void *fibGen(void *parameters)
int num = atoi(parameters);
struct thread_func_param *p;
p = malloc (sizeof (thread_func_param));
p->size = fibSize(num);
p->fib = malloc(sizeof(int)* p->size);
//Fibonacci Calculations
//..
//.
return (void *) p;
//pthread_exit((void *) p);
//===================================================
int main(int argc, char* argv[])
void* thread_result;
thread_func_param* p = malloc( sizeof(thread_func_param));
assert(argc > 1);
int noOfThreads = argc - 1;
printf("No of Thread = %dn", noOfThreads);
pthread_t *threadID = malloc (sizeof (pthread_t) * noOfThreads);
pthread_attr_t attributes;
pthread_attr_init(&attributes);
int i, j;
for(i = 0; i < noOfThreads; i++)
pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);
pthread_join(threadID[i], thread_result);
//HOW TO USE THE RETURNED DATA?
for (j = 0; j< ((thread_func_param*)thread_result->size)-1; j++)
printf(" %d ", (thread_func_param*)thread_result->fib[j]);
return 0;
The solution that I use, in the end, to print the data gives error of dereferencing a void pointer (I am new with C). How can I correct it?
c pthreads pthread-join
add a comment |
I am writing a program that takes integers as command line arguments. For each of these numbers I have to create a thread which calculates Fibonacci series up to that number. That function returns a struct pointer to the main where the data is printed.
Now, I have correctly done the fib calculations and checked them by printing the series within the function.
The problem arises when I try to return the struct pointer created within the thread function and use it to print the data in the main.
typedef struct thread_func_param
int *fib;
int size;
thread_func_param;
//===================================================
void *fibGen(void *parameters)
int num = atoi(parameters);
struct thread_func_param *p;
p = malloc (sizeof (thread_func_param));
p->size = fibSize(num);
p->fib = malloc(sizeof(int)* p->size);
//Fibonacci Calculations
//..
//.
return (void *) p;
//pthread_exit((void *) p);
//===================================================
int main(int argc, char* argv[])
void* thread_result;
thread_func_param* p = malloc( sizeof(thread_func_param));
assert(argc > 1);
int noOfThreads = argc - 1;
printf("No of Thread = %dn", noOfThreads);
pthread_t *threadID = malloc (sizeof (pthread_t) * noOfThreads);
pthread_attr_t attributes;
pthread_attr_init(&attributes);
int i, j;
for(i = 0; i < noOfThreads; i++)
pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);
pthread_join(threadID[i], thread_result);
//HOW TO USE THE RETURNED DATA?
for (j = 0; j< ((thread_func_param*)thread_result->size)-1; j++)
printf(" %d ", (thread_func_param*)thread_result->fib[j]);
return 0;
The solution that I use, in the end, to print the data gives error of dereferencing a void pointer (I am new with C). How can I correct it?
c pthreads pthread-join
add a comment |
I am writing a program that takes integers as command line arguments. For each of these numbers I have to create a thread which calculates Fibonacci series up to that number. That function returns a struct pointer to the main where the data is printed.
Now, I have correctly done the fib calculations and checked them by printing the series within the function.
The problem arises when I try to return the struct pointer created within the thread function and use it to print the data in the main.
typedef struct thread_func_param
int *fib;
int size;
thread_func_param;
//===================================================
void *fibGen(void *parameters)
int num = atoi(parameters);
struct thread_func_param *p;
p = malloc (sizeof (thread_func_param));
p->size = fibSize(num);
p->fib = malloc(sizeof(int)* p->size);
//Fibonacci Calculations
//..
//.
return (void *) p;
//pthread_exit((void *) p);
//===================================================
int main(int argc, char* argv[])
void* thread_result;
thread_func_param* p = malloc( sizeof(thread_func_param));
assert(argc > 1);
int noOfThreads = argc - 1;
printf("No of Thread = %dn", noOfThreads);
pthread_t *threadID = malloc (sizeof (pthread_t) * noOfThreads);
pthread_attr_t attributes;
pthread_attr_init(&attributes);
int i, j;
for(i = 0; i < noOfThreads; i++)
pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);
pthread_join(threadID[i], thread_result);
//HOW TO USE THE RETURNED DATA?
for (j = 0; j< ((thread_func_param*)thread_result->size)-1; j++)
printf(" %d ", (thread_func_param*)thread_result->fib[j]);
return 0;
The solution that I use, in the end, to print the data gives error of dereferencing a void pointer (I am new with C). How can I correct it?
c pthreads pthread-join
I am writing a program that takes integers as command line arguments. For each of these numbers I have to create a thread which calculates Fibonacci series up to that number. That function returns a struct pointer to the main where the data is printed.
Now, I have correctly done the fib calculations and checked them by printing the series within the function.
The problem arises when I try to return the struct pointer created within the thread function and use it to print the data in the main.
typedef struct thread_func_param
int *fib;
int size;
thread_func_param;
//===================================================
void *fibGen(void *parameters)
int num = atoi(parameters);
struct thread_func_param *p;
p = malloc (sizeof (thread_func_param));
p->size = fibSize(num);
p->fib = malloc(sizeof(int)* p->size);
//Fibonacci Calculations
//..
//.
return (void *) p;
//pthread_exit((void *) p);
//===================================================
int main(int argc, char* argv[])
void* thread_result;
thread_func_param* p = malloc( sizeof(thread_func_param));
assert(argc > 1);
int noOfThreads = argc - 1;
printf("No of Thread = %dn", noOfThreads);
pthread_t *threadID = malloc (sizeof (pthread_t) * noOfThreads);
pthread_attr_t attributes;
pthread_attr_init(&attributes);
int i, j;
for(i = 0; i < noOfThreads; i++)
pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);
pthread_join(threadID[i], thread_result);
//HOW TO USE THE RETURNED DATA?
for (j = 0; j< ((thread_func_param*)thread_result->size)-1; j++)
printf(" %d ", (thread_func_param*)thread_result->fib[j]);
return 0;
The solution that I use, in the end, to print the data gives error of dereferencing a void pointer (I am new with C). How can I correct it?
c pthreads pthread-join
c pthreads pthread-join
edited Mar 24 at 16:17
Robin Goodfellow
asked Mar 24 at 15:18
Robin GoodfellowRobin Goodfellow
218
218
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Two issues here:
pthread_join()
takes avoid**
as 2nd parameter. The code passes avoid*
only.To cast a pointer wrap it into parenthesis. Here the cast
(thread_func_param*)thread_result->size
refers to
size
not tothread_result
. So what you want is((thread_func_param*)thread_result)->size
However a nice and clean solution would only use a void
pointer interimswise. It could look like this:
int main(int argc, char* argv[])
{
thread_func_param* thread_result;
...
...
pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);
void * pv;
pthread_join(threadID[i], &pv);
thread_result = pv;
if (NULL != thread_result) /* perform some sanity checking. */
for (j = 0; j < thread_result->size - 1; j++)
printf(" %d ", thread_result->fib[j]);
...
Corrected the code. Now, it gives a segmentation fault.
– Robin Goodfellow
Mar 24 at 15:38
In which line does the code end with a segmentation violation?
– alk
Mar 24 at 15:41
In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.
– Robin Goodfellow
Mar 24 at 15:48
Thanks a lot! It finally worked.
– Robin Goodfellow
Mar 24 at 15:58
1
Of course I was going to do it; I was making some changes in my code.
– Robin Goodfellow
Mar 24 at 16:12
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325278%2fhow-to-use-a-struct-pointer-returned-to-the-in-the-main-from-a-thread-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Two issues here:
pthread_join()
takes avoid**
as 2nd parameter. The code passes avoid*
only.To cast a pointer wrap it into parenthesis. Here the cast
(thread_func_param*)thread_result->size
refers to
size
not tothread_result
. So what you want is((thread_func_param*)thread_result)->size
However a nice and clean solution would only use a void
pointer interimswise. It could look like this:
int main(int argc, char* argv[])
{
thread_func_param* thread_result;
...
...
pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);
void * pv;
pthread_join(threadID[i], &pv);
thread_result = pv;
if (NULL != thread_result) /* perform some sanity checking. */
for (j = 0; j < thread_result->size - 1; j++)
printf(" %d ", thread_result->fib[j]);
...
Corrected the code. Now, it gives a segmentation fault.
– Robin Goodfellow
Mar 24 at 15:38
In which line does the code end with a segmentation violation?
– alk
Mar 24 at 15:41
In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.
– Robin Goodfellow
Mar 24 at 15:48
Thanks a lot! It finally worked.
– Robin Goodfellow
Mar 24 at 15:58
1
Of course I was going to do it; I was making some changes in my code.
– Robin Goodfellow
Mar 24 at 16:12
|
show 3 more comments
Two issues here:
pthread_join()
takes avoid**
as 2nd parameter. The code passes avoid*
only.To cast a pointer wrap it into parenthesis. Here the cast
(thread_func_param*)thread_result->size
refers to
size
not tothread_result
. So what you want is((thread_func_param*)thread_result)->size
However a nice and clean solution would only use a void
pointer interimswise. It could look like this:
int main(int argc, char* argv[])
{
thread_func_param* thread_result;
...
...
pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);
void * pv;
pthread_join(threadID[i], &pv);
thread_result = pv;
if (NULL != thread_result) /* perform some sanity checking. */
for (j = 0; j < thread_result->size - 1; j++)
printf(" %d ", thread_result->fib[j]);
...
Corrected the code. Now, it gives a segmentation fault.
– Robin Goodfellow
Mar 24 at 15:38
In which line does the code end with a segmentation violation?
– alk
Mar 24 at 15:41
In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.
– Robin Goodfellow
Mar 24 at 15:48
Thanks a lot! It finally worked.
– Robin Goodfellow
Mar 24 at 15:58
1
Of course I was going to do it; I was making some changes in my code.
– Robin Goodfellow
Mar 24 at 16:12
|
show 3 more comments
Two issues here:
pthread_join()
takes avoid**
as 2nd parameter. The code passes avoid*
only.To cast a pointer wrap it into parenthesis. Here the cast
(thread_func_param*)thread_result->size
refers to
size
not tothread_result
. So what you want is((thread_func_param*)thread_result)->size
However a nice and clean solution would only use a void
pointer interimswise. It could look like this:
int main(int argc, char* argv[])
{
thread_func_param* thread_result;
...
...
pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);
void * pv;
pthread_join(threadID[i], &pv);
thread_result = pv;
if (NULL != thread_result) /* perform some sanity checking. */
for (j = 0; j < thread_result->size - 1; j++)
printf(" %d ", thread_result->fib[j]);
...
Two issues here:
pthread_join()
takes avoid**
as 2nd parameter. The code passes avoid*
only.To cast a pointer wrap it into parenthesis. Here the cast
(thread_func_param*)thread_result->size
refers to
size
not tothread_result
. So what you want is((thread_func_param*)thread_result)->size
However a nice and clean solution would only use a void
pointer interimswise. It could look like this:
int main(int argc, char* argv[])
{
thread_func_param* thread_result;
...
...
pthread_create(&threadID[i], &attributes, fibGen, argv[i+1]);
void * pv;
pthread_join(threadID[i], &pv);
thread_result = pv;
if (NULL != thread_result) /* perform some sanity checking. */
for (j = 0; j < thread_result->size - 1; j++)
printf(" %d ", thread_result->fib[j]);
...
edited Mar 24 at 16:04
answered Mar 24 at 15:31
alkalk
60.1k868180
60.1k868180
Corrected the code. Now, it gives a segmentation fault.
– Robin Goodfellow
Mar 24 at 15:38
In which line does the code end with a segmentation violation?
– alk
Mar 24 at 15:41
In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.
– Robin Goodfellow
Mar 24 at 15:48
Thanks a lot! It finally worked.
– Robin Goodfellow
Mar 24 at 15:58
1
Of course I was going to do it; I was making some changes in my code.
– Robin Goodfellow
Mar 24 at 16:12
|
show 3 more comments
Corrected the code. Now, it gives a segmentation fault.
– Robin Goodfellow
Mar 24 at 15:38
In which line does the code end with a segmentation violation?
– alk
Mar 24 at 15:41
In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.
– Robin Goodfellow
Mar 24 at 15:48
Thanks a lot! It finally worked.
– Robin Goodfellow
Mar 24 at 15:58
1
Of course I was going to do it; I was making some changes in my code.
– Robin Goodfellow
Mar 24 at 16:12
Corrected the code. Now, it gives a segmentation fault.
– Robin Goodfellow
Mar 24 at 15:38
Corrected the code. Now, it gives a segmentation fault.
– Robin Goodfellow
Mar 24 at 15:38
In which line does the code end with a segmentation violation?
– alk
Mar 24 at 15:41
In which line does the code end with a segmentation violation?
– alk
Mar 24 at 15:41
In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.
– Robin Goodfellow
Mar 24 at 15:48
In gdb, it is on the line where I declare the for loop. It cannot match the condition for i.
– Robin Goodfellow
Mar 24 at 15:48
Thanks a lot! It finally worked.
– Robin Goodfellow
Mar 24 at 15:58
Thanks a lot! It finally worked.
– Robin Goodfellow
Mar 24 at 15:58
1
1
Of course I was going to do it; I was making some changes in my code.
– Robin Goodfellow
Mar 24 at 16:12
Of course I was going to do it; I was making some changes in my code.
– Robin Goodfellow
Mar 24 at 16:12
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325278%2fhow-to-use-a-struct-pointer-returned-to-the-in-the-main-from-a-thread-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown