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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:03:06+00:00 2026-06-17T18:03:06+00:00

My C knowledge is young, so forgive me :) Two questions really, How do

  • 0

My C knowledge is young, so forgive me 🙂 Two questions really,

  1. How do I turn the following code into something that correctly makes use of pthread_t *threadsArray as opposed to threadsArray[MAXCON]?

  2. Is there a good reason to? I’ve heard I should avoid using explicit arrays and try to use pointer defined arrays when I can.

pthread_t threadsArray[MAXCON];

int k;
for (k = 0; k < MAXCON; k++) {
    fprintf(stderr, "Make %d\n", k);

    int *connfd = malloc(sizeof(int));
    *connfd = accept(fd, (struct sockaddr *) &cliaddr, &cliaddrlen);
    if (*connfd == -1) {
        perror("Unable to accept connection");
        return 1;
    }

    fprintf(stderr, "Waited\n");

    pthread_t thread;
    pthread_create(&thread, NULL, readWriteToClient, connfd);
    threadsArray[k] = thread;
}

for(k = 0; k < MAXCON; k++){
    fprintf(stderr,"Join %d\n",k);
    pthread_t thread = threadsArray[k];
    pthread_join(thread, NULL);
}
  • 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-17T18:03:08+00:00Added an answer on June 17, 2026 at 6:03 pm

    To answer your second question first: it sounds like you’re talking about the different between stack and heap allocation.

    Declaring the array in the function body like you’ve done will take up space on the stack, which can be quite limited (a few kilobytes to a few megabytes).

    An alternative to this is to allocate the array on the “heap”, which is limited only by the available memory in your computer. To allocate memory on the heap, you use malloc().

    So changing the first line to:

    pthread_t *threadsArray = malloc(MAXCON * sizeof(pthread_t));
    

    (and possibly checking for NULL if your system’s malloc can fail)

    is all you need to do to allocate the array on the heap.

    You’ll also need to do free(threadsArray) afterwards to release the memory.

    But if MAXCON is small, allocating the array on the stack as you have done should be fine.

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

Sidebar

Related Questions

I have little working knowledge of python. I know that there is something called
With my only ORM knowledge being L2S/EF, I was surprised when the following code
My knowledge of Lambda expressions is a bit shaky, while I can write code
A little knowledge can be a dangerous thing. Now that I've had a run
My knowledge of Java is rather rusty, but I've been forced to use it
Without pro-knowledge of HTML/CSS, what can I use to create a web 2.0-like website
It is common knowledge that source filters are bad and should not be used
My limited SQL knowledge prompted this post! I have a stored procedure that runs
This just for general knowledge: If I have two, let's say, List , and
My knowledge of MVC and Razor is quite basic so I'm hoping its something

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.