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

  • SEARCH
  • Home
  • 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 9018037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:25:08+00:00 2026-06-16T04:25:08+00:00

The first argument of pthread_create is a pthread_t pointer. In the hello program below,

  • 0

The first argument of pthread_create is a pthread_t pointer. In the hello program below, if the first argument is a pointer to pthread_t (pthread_t*) instead of a pthread_t (pthread_t) the program ends with Segmentation fault…why?

I don’t remember seeing pthread_t* as the declared type of the first argument of pthread_create.
And chapter 2 of Butenhof’s book Programming with POSIX Threads says:

To create a thread, you must declare a variable of type pthread_t [not pthread_t*].

But according to the specification the first argument of pthread_create is a pointer to pthread_t, so why the segmentation fault?


Segmentation fault

pthread_t* thr;
pthread_create(thr, NULL, &hello, NULL);


Runs OK

pthread_t thr;
pthread_t* pntr = &thr;
pthread_create(pntr, NULL, &hello, NULL);


hello program:

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

void * 
hello(void *arg){
  printf("Hello\n");
  pthread_exit(NULL);
}

int 
main(int argc, char **argv){
  pthread_t thr = 1;
  pthread_create(&thr, NULL, &hello, NULL);



  pthread_join(thr, NULL);

  return 0;
}

pthread_create prototype:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void*), void *arg);
  • 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-16T04:25:09+00:00Added an answer on June 16, 2026 at 4:25 am
    pthread_t* thr;
    pthread_create(thr, NULL, &hello, NULL);
    

    declares a pointer to a pthread_t without allocating storage for it. When you call pthread_create, it’ll try writing to *thr. This is at an undefined location and will almost certainly fail.

    pthread_t thr;
    pthread_t* pntr = &thr;
    pthread_create(pntr, NULL, &hello, NULL);
    

    works because you’ve declare storage (thr on the stack) for a pthread_t.

    Note that the second, working, version can be simplified to what is used in your hello program

    pthread_t thr;
    pthread_create(&thr, NULL, &hello, NULL);
    

    …which declares a pthread_t on the stack then passes a pointer to it into pthread_create.

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

Sidebar

Related Questions

The first program compiled properly.second gave error saying too few argument for foo... is
What is the significance of the first argument (containing '' in the example below)
I'm solving the following kata: Write a program that takes as its first argument
The read function has it's first argument as pointer to an array to store
this program hangs after taking first argument:- #include <stdio.h> #include <conio.h> void ellip(char*,...); int
I know the program name is passed as the first argument, and next simple
How can i skip first argument without giving any value in function call,so that
Pep8 advises to always use cls as the first argument of a class method
I'm trying to write a function that would use its first argument, then send
I have created a file argument.pl which takes several arguments first of which should

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.