Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8051829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:23:28+00:00 2026-06-05T07:23:28+00:00

I have done the following code. #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h>

  • 0

I have done the following code.

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

struct foo
{
   int a;
   int b;
};

void* thread_func1(void *arg)
{
   struct foo *temp = (struct foo*)malloc(sizeof(struct foo));

   temp->a = 10;
   temp->b = 20;

   pthread_exit(temp);
}

void* thread_func2(void *arg)
{
   pthread_exit((void*)100);
}

int main()
{
   pthread_t tid1, tid2;
   int err;
   struct foo *f;
   void *ret;

   err = pthread_create(&tid1, NULL, thread_func1, NULL);
   err = err | pthread_create(&tid2, NULL, thread_func2, NULL);

   if(err != 0)
   {
      perror("pthread_create()");
      exit(-1);
   }

   err = pthread_join(tid1, (void**)&f);
   if(err != 0 )
   {
      perror("pthread_join1");
      exit(-1);
   }

   printf("a = %d, b = %d\n", f->a, f->b); //Line1

   err = pthread_join(tid2, &ret);
   if(err != 0 )
   {
      perror("pthread_join2");
      exit(-1);
   }

   printf("ret = %d\n", *(int*)ret); //Line2

   return 0;

}

I get segmentation fault on Line2. What is wrong with Line2

If i modify Line2 to

printf(“ret = %d\n”, (int)ret);

there is no segmentation fault and it prints the correct value(ie, 100). I do not understand why the modification works. I believe i have the wrong concept regarding the usage of double pointers. I would like to get it corrected.

What is the reason for the Segmentation fault and why the modification works?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:23:30+00:00Added an answer on June 5, 2026 at 7:23 am

    You return one number from the thread. In the first thread, that number is a struct foo *. Therefore, if you say

    pthread_join(tid1, &ret);
    

    then ret will contain that pointer (which is not a double pointer).

    Similarly in the second case, you are returning 100 even though you are looking at it as if it’s a void *. Nevertheless, the value is still 100!

    Therefore when you write

    pthread_join(tid2, &ret);
    

    ret will contain 100, which is not a pointer, but a mere integer. That is why you should also cast it to int.

    The reason you got a segmentation fault is that you look at 100 as an int * and then try to dereference it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct
I have the following snippet of C code: #include <stdio.h> void main(){ int a
Lets say we have the following code: #include <iostream> #include <string> struct A {
Tried the following code : #include<stdio.h> int main() { int *p,*q; p = (int
I have done following code in Viewdid Load for creating multiple switch programmatically. float
I have done following code for creating multiple UISwitches programmatically and handle particular switch.
I have done the following modifications in my code: ConnectionPool.java: package com.dao; import java.sql.*;
I have just started using jQuery and although following code gets the job done,
I have the following code that cannot be compiled: #include <iostream> #include <set> #include
I have the following code #include <iostream> #include <vector> class Entity { public: virtual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.