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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:42:05+00:00 2026-05-23T11:42:05+00:00

I am trying to pass 2 unsigned integers to a newly created thread in

  • 0

I am trying to pass 2 unsigned integers to a newly created thread in C (using pthread_create()) but nor an array of 2 integers or a struct seems to work.

// In my socket file

struct dimension {
    unsigned int width;
    unsigned int height;
};

unsigned int width, height;

void setUpSocket(void* dimension) {

    struct dimension* dim = (struct dimension*) dimension;

    width = dim->width;
    height = dim->height;

    printf("\n\nWidth: %d, Height: %d\n\n", width, height);

}

// In main.cpp

// Pass a struct in pthread_create
struct dimension dim;
dim.width = w;
dim.height = h;

pthread_create(&ph, &attr, (void * (*)(void *)) setUpSocket, (void *) &dim);

Before calling pthread_create, dim.width and dim.height are correct. In my socket file, only width is set, height is 0, and I do not understand why.

Does anyone know what is wrong please and how to fix it?

Thank you very much.

  • 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-05-23T11:42:06+00:00Added an answer on May 23, 2026 at 11:42 am

    The way you’re passing the arguments should work fine, as long as dim is not allocated on the stack. If it’s on the stack, then it could become deallocated before the new thread has a chance to run, resulting in undefined behavior. If you’re only creating one thread, you can use a global variable, but the better alternative is to allocate it on the heap.

    Also, you should not be casting the function pointer: that is undefined behavior (and in fact, it could crash due to speculative execution on the IA64 architecture). You should declare your thread procedure to return void* and avoid a function pointer cast:

    void *setUpSocket(void* dimension) {
    
        struct dimension* dim = (struct dimension*) dimension;
    
        width = dim->width;
        height = dim->height;
        // Don't leak the memory
        free(dim);
    
        printf("\n\nWidth: %d, Height: %d\n\n", width, height);
    
        return 0;
    }
    
    // In main.cpp
    
    // Pass a struct in pthread_create (NOT on the stack)
    struct dimension *dim = malloc(sizeof(struct dimension));
    dim->width = w;
    dim->height = h;
    
    pthread_create(&ph, &attr, setUpSocket, dim);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pass an array of unsigned integers from C++ to Ada. The
I'm trying to port some code to 64-bit, but it seems that the thread
I am trying to pass a dataString to to an ajax call using JQuery.
im trying to pass new query to FlexiGrid using this code: $('#fgAllPatients').flexReload({ query: 'blah=qweqweqwe'
i dunno where is the problem exactly, but it seems when i pass 'Memory'
Im trying to pass an array that I already found by a query into
Trying to pass GetFullNamePDF() to the Filename attribute, but getting the following error: Compile
im trying to pass an array as a string $(document).ready(function(){ var args = {};
Im trying to pass to another xib some data of NSMutableArray. I created a
I'm passing an array of 76800 Booleans (could be integers or unsigned char* instead)

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.