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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:15:07+00:00 2026-06-18T16:15:07+00:00

I made a simple program in pthreads that passed multiple parameters to the called

  • 0

I made a simple program in pthreads that passed multiple parameters to the called function via a struct. Consider these two programs:
Program 1 :

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

struct args{
    long long val1;
    int val2;
};

void *hello(void* threadid){
    struct args *tid;
    tid=(struct args*)threadid;
    printf("thread %lld\n",tid->val1);
    pthread_exit(NULL);
}

int main(){
    pthread_t threads[20];
    int i;
    for(i=0;i<20;i++){
        // ***** specific memory given to the struct *****
        struct args* a1=(struct args*)malloc(sizeof(struct args));    
        a1->val1=i;
        a1->val2=i+1;
        int ret=pthread_create(&threads[i],NULL,hello,(void*)a1);
        if(ret){
            printf("error code %d for %d\n",ret,i);
        }
    }
    pthread_exit(NULL);
}

which prints output as expected, some permutation of 0..19

On the other hand, consider
Program p2

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

struct args{
    long long val1;
    int val2;
};

void *hello(void* threadid){
    struct args *tid;
    tid=(struct args*)threadid;
    printf("thread %lld\n",tid->val1);
    pthread_exit(NULL);
}

int main(){
    pthread_t threads[20];
    int i;
    for(i=0;i<20;i++){
        // ***** struct made like normal declarations *****
        struct args a1;
        a1.val1=i;
        a1.val2=i+1;
        int ret=pthread_create(&threads[i],NULL,hello,(void*)&a1);
        if(ret){
            printf("error code %d for %d\n",ret,i);
        }
    }
    pthread_exit(NULL);
}

This program has repeating and incomplete entries, like

thread 3
thread 5
thread 3
thread 4
thread 3
thread 6
thread 7
thread 8
thread 9
thread 10
thread 11
thread 12
thread 13
thread 15
thread 15
thread 16
thread 17
thread 18
thread 19
thread 19

Why is instantiation of struct directly causing overlap of this kind? Shouldn’t C provide a new memory block for each time in the loop?

  • 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-18T16:15:08+00:00Added an answer on June 18, 2026 at 4:15 pm

    In your second example, a1 is declared with automatic-storage duration in a for loop. This means that at the end of every iteration, that storage location may then be re-used for the next iteration. Therefore:

    int ret=pthread_create(&threads[i],NULL,hello,(void*)&a1);
    

    …you may be passing an address (&a1) to a memory location that will be modified in the subsequent iteration. malloc on the other hand, will allocate a pointer to a different memory location at every iteration.

    Since there’s no guarantee whether or not the thread will execute before or after the next iteration begins, you may actually get different results each time you run the above code.

    Furthermore, using malloc without storing the pointers anywhere or free them in the thread will result in memory being leaked. Your struct may also go out of scope before all your threads finish, the result being them accessing a dangling pointer. Lastly, since you’re not joining any of your threads, there’s no telling how many will actually execute completely before the program exits.

    Welcome to the fantastic world of multi-threading.

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

Sidebar

Related Questions

For an assignment I've made a simple C++ program that uses a superclass (Student)
I recently made a very simple practice program in Python, that takes user input
I've made a simple program to store locally an array using two for loops,
I made a simple java program that sends bytes to the parallel port, which
So I made a very simple program that counts down from 99 (sings 99
I'm writing a simple program that makes multiple connections to different servers for status
I've made a simple program (closely resting on this example) that visualises my problem.
I made a simple program that uses a shared object, opening it with dlopen()
i've made a simple program that change a string from lowercase to uppercase and
I have made a simple program that show directory listing in QTreeView using QFileSystemModel.

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.