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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:18:27+00:00 2026-06-03T21:18:27+00:00

#include <stdio.h> #include <dirent.h> #include <sys/types.h> #include <sys/param.h> #include <sys/stat.h> #include <unistd.h> #include <string.h>

  • 0
#include <stdio.h>
#include <dirent.h> 
#include <sys/types.h> 
#include <sys/param.h> 
#include <sys/stat.h> 
#include <unistd.h> 
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <vector>

using namespace std;

enum ElementType { SIMPLEFILE, DIRECTORY, SYMBOLICLINK };

class Element{
public:
    Element(){};

    Element(char *name_, char *full_path_name_, ElementType element_type_, long element_size_)
      : name(NULL), full_path_name(NULL), element_size(0)
    {
        memcpy (name,name_,strlen(name_)+1);
        memcpy (full_path_name,full_path_name_,strlen(full_path_name_)+1);
        element_type=element_type_;
        element_size=element_size_;
    };

    char *name;
    char *full_path_name;
    ElementType element_type;
    long element_size;
};

int inspect(const char *input_path, std::vector<Element>& result_element_array, long *dir_size,const char *full_path ) {
    std::vector<Element> result_element_array_temp;
    DIR           *d;
    struct dirent *dir;
    struct stat buf;
    char *mynamebuf=(char *)malloc(0);
    int c=0;
    size_t base_len = strlen(full_path);

    long dir_size_temp=0;
    char *full_path_temp=(char *)malloc(0);
    char *full_path_dummy=(char *)malloc(0);

    result_element_array_temp.reserve(1000);

    d = opendir( full_path);

    if( d == NULL ) {
        return 1;
    }
    while( ( dir = readdir( d ) )) {
        if( strcmp( dir->d_name, "." ) == 0 || strcmp( dir->d_name, ".." ) == 0 ) {
            continue;
        }

        memcpy (mynamebuf,full_path,strlen(full_path)+1);
        strcat(mynamebuf,(full_path[base_len - 1] == '/' ? "" : "/"));
        strcat(mynamebuf,dir->d_name);
        if (stat(mynamebuf, &buf) != 0) {
            perror(mynamebuf);
            continue;
        }

        if( S_ISDIR(buf.st_mode) ) {//if dir
            chdir( dir->d_name );
            memcpy (full_path_temp,full_path,strlen(full_path)+1);
            strcat(full_path_temp,"/");
            strcat(full_path_temp,dir->d_name);
            (dir_size_temp)=0;

            inspect( ".", result_element_array_temp, &dir_size_temp, full_path_temp  );

            chdir( ".." );
            memcpy (full_path_dummy,full_path_temp,strlen(full_path_temp)+1);
            strcat(full_path_dummy,"/");
            strcat(full_path_dummy,dir->d_name);
            Element element;
            element.name=dir->d_name;
            element.full_path_name=full_path_dummy;
            element.element_type=DIRECTORY;
            element.element_size=dir_size_temp;
            result_element_array.push_back(element);
            result_element_array.insert( result_element_array.end(), result_element_array_temp.begin(), result_element_array_temp.end() );
            (*dir_size)+=(dir_size_temp);
        } else if( S_ISREG(buf.st_mode)) {//if file
            memcpy (full_path_dummy,full_path,strlen(full_path)+1);
            strcat(full_path_dummy,"/");
            strcat(full_path_dummy,dir->d_name);
            Element element;
            element.name=dir->d_name;
            element.full_path_name=full_path_dummy;
            element.element_type=SIMPLEFILE;
            element.element_size=buf.st_size;
            result_element_array.push_back(element);
            (*dir_size)+=buf.st_size;
        } else if( S_ISLNK(buf.st_mode) ) {//if link
            memcpy (full_path_dummy,full_path,strlen(full_path)+1);
            strcat(full_path_dummy,"/");
            strcat(full_path_dummy,dir->d_name);

            Element element;
            element.name=dir->d_name;
            element.full_path_name=full_path_dummy;
            element.element_type=SIMPLEFILE;
            element.element_size=buf.st_size;
            result_element_array.push_back(element);
        } else {
            continue;
        }
    }
    closedir(d);
    return 0;
}

int main(){
    std::vector<Element> result_element_array;
    result_element_array.reserve(3000); 
    long dir_size;
    const char *full_path="/";
    inspect("/", result_element_array, &dir_size,full_path  );

    std::vector <Element>::iterator It;

    for(It = result_element_array.begin(); It != result_element_array.end(); ++It){
        printf("%s\n",(*It).full_path_name);
    }
    return 0;
}

The code is above. I aim to write directory explorer recursively.

I GDBed the code and it reaches return 0 when inspect method called recursively, but return 0 can not be executed. Might it be about stackoverflow caused by some other lines? Any idea would be 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-03T21:18:28+00:00Added an answer on June 3, 2026 at 9:18 pm

    On your Element constructor, you’re memcpy-ing char arrays without allocating the destination char*. You should consider using std::string:

    class Element{
    public:
        Element(){};
      Element(char *name_,
      char *full_path_name_,
      ElementType element_type_,
      long element_size_)
          :name(name),
          full_path_name(full_path_name_),
          element_size(0)
      {
          element_type=element_type_;
          element_size=element_size_;
      };
      std::string name;
      std::string full_path_name;
      ElementType element_type;
      long element_size;
    
    };
    

    You are also performing a malloc of 0 bytes, and then use this pointer as if it was valid:

    char *mynamebuf=(char *)malloc(0);
    

    You should also consider using std::string, or at least fix it to allocate a valid amount of bytes.

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

Sidebar

Related Questions

#include <stdio.h> #include <dirent.h> #include <sys/types.h> #include <sys/param.h> #include <sys/stat.h> #include <unistd.h> #include <string.h>
Here is my code: #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <string.h> #include <sys/stat.h>
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> static char *dup_str(const char *s) {
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <dirent.h> pthread_mutex_t mut1 = PTHREAD_MUTEX_INITIALIZER, mut2 =
I got a segfault with this code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include
#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]);
#include<stdio.h> #include<signal.h> #include<stdlib.h> void handler(int signo) { printf(First statement); system(date); exit(EXIT_SUCCESS); } int main()
#include stdafx.h #include string.h #include stdio.h void CharReadWrite(FILE *fin); FILE *fptr2; int _tmain(int argc,
could someone explain this phenomenon. #include stdio.h #include stdlib.h int main() { char foo[]=foo;

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.