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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:11:51+00:00 2026-06-13T13:11:51+00:00

back with another segfault. Not sure why, as this readdir line is the same

  • 0

back with another segfault. Not sure why, as this readdir line is the same one i’ve been using in other file copy assignments. Below is the code I crafted in lieu of the assignment shared here. I have commented where the segfault occurs in hopes of aiding better minds find my flaw!

This is copyDirs.cpp

//cmd: ./a.out [#ofConsumerThreads] [src directory] [dest directory]

#include "copyDirs.h"

int main(int ac,char* av[])
{
pthread_mutex_init(&buflock,NULL);
pthread_t prodT;

if(ac == 4)
{
    int consNum = atoi(av[1]);

    pthread_t thread[consNum];

    strcpy(f1,av[2]);
    strcpy(f2,av[3]);

    directory = opendir(f1);
    pthread_create(&prodT,NULL,producer,NULL); //segfault happens in producer function

        pthread_join(prodT, NULL);

    for(int i=0;i<consNum && buffer.size() > 0;i++)
    {
        pthread_create(&thread[i],NULL,consumer,NULL);
            pthread_join(thread[i],NULL);
    }

    closedir(directory);
}
else cout<<"Try that again ;)"<<endl;

pthread_mutex_destroy(&buflock);
pthread_exit(NULL);

return 0;
}

and the relevant header file;

#ifndef COPYDIRS_H
#define COPYDIRS_H 

#include <iostream>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <dirent.h>
#include <pthread.h>
#include <time.h>
#include <stack>

using namespace std;

struct FD
{
public:
char* i ;
char* o;

FD(){}

FD(char* input, char* output)
{
    i=input;
    o=output;
}
};

char f1[PATH_MAX];
char f2[PATH_MAX];

struct dirent *curDir;
DIR* directory = NULL;

pthread_mutex_t buflock;
stack <FD> buffer;

void* producer(void*)
{
cout<<"SegTest"<<endl;
    //shows

while (curDir = readdir(directory)) //segfault on this line
{
    cout<<"SegTest"<<endl;
        //doesn't show

    char* file = curDir -> d_name;
    char* i = new char[256];
    char* o = new char[256];

    strcpy(i,f1);
    strcpy(o,f2);
    strcat(i,file);
    strcat(o,file);
    FD prodFD(i,o);

    cout<<"Pushing "<<file<<" to buffer!"<<endl;
    pthread_mutex_lock(&buflock);
        buffer.push(prodFD);
    pthread_mutex_unlock(&buflock);

    i = NULL;
    o = NULL;
}

pthread_exit(NULL);
}

void* consumer(void*)
{
FD consFD;
char c;

consFD = buffer.top();
buffer.pop();
    //ERROR: "statement cannot resolve address of overloaded function

cout << "Copying file: "<<consFD.i<<endl;
pthread_mutex_lock(&buflock);
    FILE * consIF = fopen(consFD.i,"r");
    FILE * consOF = fopen(consFD.o,"w");
pthread_mutex_unlock(&buflock);

pthread_exit(NULL);
}
#endif 
  • 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-13T13:11:53+00:00Added an answer on June 13, 2026 at 1:11 pm

    Try checking that the value of directory is not NULL before calling readdir. You have this line

     directory = opendir(f1);
    

    but are not checking if the return value is NULL which could be the cause of the segfault. At the very least this will prevent a segfault in the event that you pass in an invalid command line argument for the directories.

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

Sidebar

Related Questions

Because I need to fadeIn() another element of the same size back in, is
Hi I'm back with another question,I have started using EF and I'm having problem
I'm back with another mysql query question. This time, given a time range, I
I have two DIVs (one in back and another on font of it). Where
Hey folks... I am back with another issue... I am using jquery Autocomplete on
I'm back with another Flex/Flash security question. I've already received some help from the
Back-end: I have a model (User) that has_many of another model (ContactPreference). Front-end: An
I am starting another activity by calling startActivityForResult() and after pressing back button my
I've got 10 GB of files to back up daily to another site. The
Back in my days as a BeOS programmer, I read this article by Benoit

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.