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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:38:45+00:00 2026-06-16T15:38:45+00:00

I am trying to do an assignment in which I am to use multiple

  • 0

I am trying to do an assignment in which I am to use multiple threads to sort input from a file, but when I try to use the array of structs to store the information I need to recover after the threads, I get a segmentation fault. I am not sure why it is causing the fault, as according to the sources I have.

This is the main file, Threads.c The seg fault is in the for loop, and the causing line is desginated by a comment. Sort Method is another function I did not in

#include "threads.h"
Threads* threadArray[4];
int linesPerThread;
int extraLines;

main(int argc, char *argv[]){
 int n;

 if( argc != 4){
  printf("Wrong Number of Arguements!\n");
  return;
}
  n = atoi(argv[1]);
 char *inName = argv[2];



*threadArray = (Threads*) (*threadArray, n*sizeof(Threads));  




FILE* file = fopen(inName, "r");
 if(!file){
printf("invalid file Name \n");
return;}

int lines = 0;
char xyz[5000]; //makes fgets happy
while(fgets(xyz, 5000, file) != NULL){
  lines = lines+1;
}
fclose(file);
linesPerThread = lines / n;


 extraLines = lines - linesPerThread;

 int i =0;
 int methodCounter =1;


 printf("Right before Loop \n \n");

 for(i; i < n; i++){

   printf("first part of loop \n");
 \\The ling below here Seg Faults.
   (*threadArray + i)->id = i;

   printf("right after first ThreadArray access \n");
   if(methodCounter < 3){
 printf("method counter 1\n");
(*threadArray+i)->methodID = methodCounter;
 methodCounter++;
   }else{
 printf("method counter condition 2 \n");
(*threadArray + i)->methodID = 3;
   methodCounter = 1;}
   if(extraLines > 0){
 printf("extra Lines condition 1 \n");
(*threadArray+i)->lines = linesPerThread +1;
 extraLines= extraLines -1;
   }else{
 printf("extraLines condition 2 \n");
 (*threadArray+i)->lines = linesPerThread;
   }
   printf("Right before Thread Creation \n \n");
   pthread_t tID;
   pthread_create(&tID, NULL, sortMethod, (void*) &((*threadArray+i)->id));
   (*threadArray+i)->threadID = tID;
   printf("right after thread creation \n \n");
 }
 printf("right after loop \n \n");
 int c=0;

 printf("before thread joining \n");
 for(c; c< n; c++){
   pthread_join( (*threadArray+ c)->threadID, NULL);
 }


 }

And this is the header file, Threads.h

#include <sys/time.h>
#include <stdio.h> 
#include <pthread.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
  int id;
  int lines;
  pthread_t threadID;
  int methodID;
}Threads;

void* sortMethod(void*ptr);
int main(int argc, char *argv[]);

Any help you can offer would be greatly appreciated.

  • 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-16T15:38:46+00:00Added an answer on June 16, 2026 at 3:38 pm

    In the line

    *threadArray = (Threads*) (*threadArray, n*sizeof(Threads));  
    

    you are setting threadArray[0] to (Threads*)(n*sizeof(Threads). You probably want a realloc or a calloc in that line.

    As it stands,

    (*threadArray, n*sizeof(Threads))
    

    is a comma-expression, and the value of that comma-expression is cast to Threads*.

    Since you never allocated memory to any of the elements of threadArray,

    (*threadArray + i)->id = i;
    

    dereferences invalid pointers. Since the array is static, the pointers are originally initialised to null pointers, you only set threadArray[0] to a different value (but that doesn’t point to valid memory either, in all probability).

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

Sidebar

Related Questions

I am trying to learn Java's threads in order to do an assignment, but
I am trying to build an NLP system for an assignment, for which I
I'm trying to build a simple interface for an assignment, in which multi-line word-wrapped
I'm new to C++, and I'm trying to use inheritance but I have an
I'm trying to use sscanf in C to get some values from a string.
How to set entire HTML in MSHTML? I am trying using this assignment: (Document
I'm trying to complete my assignment and this is the last thing to do
I'm trying to propagate an assignment to the data parameter of a sub-component through
Hi I am am trying to complete an assignment, where it is ok to
I am getting the error Assignment to readonly property when trying to assign a

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.