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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:46:34+00:00 2026-05-26T16:46:34+00:00

I am trying to create an array of size n (where n is user’s

  • 0

I am trying to create an array of size n (where n is user’s input) and when the user runs the program, the array elements should be set to 1 (each in a separate thread). Here is what I have done so far:

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <process.h>

int *x;

DWORD WINAPI init_X(LPVOID param)
{
    int index = *(int *) param;
    x[index] = 1;

    return 0;
}

int main(int argc, char *argv[])
{
    int n = atoi(argv[1]);
    int i; // counter.
    HANDLE THandles[n];

    x = malloc(n * sizeof (int));

    for(i = 0; i < n; i++)
    {
        THandles[i] = CreateThread(NULL, 0, init_X, &i, 0, NULL);
    }

    // Now wait for threads to finish
    WaitForMultipleObjects(n, THandles, TRUE, INFINITE);

    // Close the thread handle
    for(i = 0; i < n; i++)
    {
        CloseHandle(THandles[i]);
    }

    printf("After initialization x = ");
    for(i = 0; i < n; i++)
    {
        printf("%d ", x[i]);
        if(i < n - 1) printf(" ");
    }

    // ...

    return 0;
}

I run this program and I got wrong outputs:

> Test.exe 3
After initialization x = 11611536 11600064 50397186

It should be After initialization x = 1 1 1 though. I am not sure how I can I fix this, but I am sure its something related to the pointers.

P.S: I’m Java programmer so I’m not familiar with pointers.

  • 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-26T16:46:35+00:00Added an answer on May 26, 2026 at 4:46 pm

    The value you are passing as your array index will more than likely be invalid by the time the thread runs, as there is no guaranteeing that the thread is run immediately after the call to CreateThread.

    You have two solutions, either pass by value (simple & easy, but not always safe) or allocate a temporary buffer for the value that will be freed by the thread when its used.

    Minor Update:

    In fact, a better way would be to pass &x[i], then you can just do *(int*)param = 1;

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

Sidebar

Related Questions

I'm trying to create a program where the size of array index and its
I am trying to create an array of size 2^25 in c and then
I am trying to create a global 2-dimensional array who's size would depend on
I'm trying to create a array of structs, but define it's size later, like
I'm trying to create a dynamic memory. The size of the array is determine
I'm trying to create a program which takes in a set number of strings
Im trying to create an array of tm* structs, and then return them at
I am trying to create an array of class objects taking an integer argument.
I'm trying to create an array to display the last 5 products a customer
I am trying to create an array or list that could handle in theory,

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.