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 7962449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:20:22+00:00 2026-06-04T05:20:22+00:00

When creating a new pthread and passing it an argument modified with pthread_attr_getstack it

  • 0

When creating a new pthread and passing it an argument modified with pthread_attr_getstack it seems to not using the defined stack space.

void* thread_function(void * ptr)
{
  int a;
  printf("stack var in thread %p\n",&a);
}


int main( int argc , char **argv )
{
  pthread_t thread;
  void * ptr = NULL;
  const int stack_size = 10*1024;

  void * stack = malloc(stack_size);
  printf("alloc=%p\n",&stack);

  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setstack( &attr , stack , stack_size );

  if (pthread_create(&thread, &attr, &thread_function , ptr ) ) {
    printf("failed to create thread\n");
    return 1;
  }

  pthread_attr_destroy(&attr);
  pthread_exit( 0 );
  return 0;
}

Unfortunately the output is:

alloc=0x7fff48989bc8
stack var in thread 0x7f6e6f0d2ebc

Even if stack grows backward (which i am not sure) the pointer values differ so much, that only hope that the created thread uses a different virtual memory address space. But i think this is not the case.

  • 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-04T05:20:23+00:00Added an answer on June 4, 2026 at 5:20 am

    You’re printing the wrong thing,

    printf("alloc=%p\n",&stack);
    

    Prints the address of a local stack variable, not the allocated memory. You have to do

    printf("alloc=%p\n",stack);
    

    Also, you need to check for errors:

    ERRORS

       pthread_attr_setstack() can fail with the following error:
    
       EINVAL stacksize is less than PTHREAD_STACK_MIN (16384) bytes.  On some
              systems, this error may also occur if stackaddr or
              stackaddr + stacksize is not suitably aligned.
    

    You’ve only set a stack of 10kB, so try again with a bigger stack, and check the return value of pthread_attr_setstack.

    I’d probably try to make the stack page aligned as well.

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

Sidebar

Related Questions

I'm using createuser wizard for creating new users. I'm not using any membership provider.
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I'm creating new Site Definitions using this method: http://weblogs.asp.net/paulballard/archive/2007/04/09/creating-a-custom-sharepoint-2007-portal-site-definition-using-the-portalprovisioningprovider-class.aspx and when they get created,
I'm creating new image using img = new Image(); img.src = image_url; Then I'm
When creating a new MVC Project in VS2008 Professional (SP1 installed), I do not
I'm creating new application using zend framework with several modules. Can you please advice
I'm creating new processes using System.Diagnostics.Process class from my application. I want this processes
I'm using Rails and I have a User Controller for creating new users. To
When I'm creating new project with GWT plug in it creates a skeleton project
Can I avoid third party code from creating new threads, starting new VMs, or

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.