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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:56:20+00:00 2026-05-31T02:56:20+00:00

I have created a function that creates a dynamic string array with dynamic string

  • 0

I have created a function that creates a dynamic string array with dynamic string length and then I return it to my main function. Everything works fine inside my function but when I try to print the array in main I get a segmentation fault after the 4th string – first two string don’t print out correct either. This part of the program is supposed to find out all entries in a directory and its sub-directories and store them in main memory.

Here’s the result:

Path[0]=A/New Folder. - i=0
Path[1]=A/atext - i=1
Path[2]=A/a - i=2
Path[3]=A/alink - i=3
Path[4]=A/afolder - i=4
Path[5]=A/afolder/set008.pdf - i=0
Path[6]=A/afolder/anotherfolder - i=1
Path[7]=A/afolder/anotherfolder/folderOfAnotherFolder - i=0
Path[8]=A/afolder/anotherfolder/folderOfAnotherFolder/mytext - i=0
Path[9]=A/afolder/anotherfolder/mytext - i=1
Path[10]=A/afolder/set001.pdf - i=2
Entries in directory: A
��
��
A/a
A/alink
Segmentation fault

And here’s the code:
function:

char ** getDirContents(char *dirName,char **paths ) 
{   
    DIR * tmpDir;
    struct dirent * entry;
    //char  tmpName[512];
    char * tmpName=NULL;
    struct stat node;
    int size=0;
    int i=0;
    //paths=NULL;

    if((tmpDir=opendir(dirName))==NULL){
        perror("getDirContents opendir"); 
        return NULL;
    }
    i=0;
    while ((entry=readdir(tmpDir))!=NULL) 
    {
        //if (entry->d_ino==0) continue;
        if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)//Ignore root & parent directories
            continue;but I

        tmpName =(char *)malloc(strlen(dirName)+strlen(entry->d_name)+2);
        strcpy(tmpName,dirName);
        strcat(tmpName,"/");
        strcat(tmpName,entry->d_name);
        //printf("\ntmpName[%d]:%s",count,tmpName);

        paths=(char**)realloc(paths,sizeof(char*)*(count+1));
        paths[count]=NULL;
        //paths[count]=(char*)realloc(paths[count],strlen(tmpName)+1);
        paths[count]=(char*)malloc(strlen(tmpName)+1);

        //memcpy(paths[count],tmpName,strlen(tmpName)+1);
        strcpy(paths[count],tmpName);
        printf("\nPath[%d]=%s - i=%d",count,paths[count],i);

        count++;

        if(lstat(tmpName,&node)<0)    
            { 
                printf("\ntmpName:%s",tmpName);
                perror("getDirContents Stat");
                exit(0);
            }
        if (S_ISDIR(node.st_mode))
            {
                getDirContents(tmpName,paths);//Subfolder
            }

        //printf("\n%s,iters:%d",tmpName,i);
        free(tmpName);
        tmpName=NULL;
        i++;
    }
close(tmpDir);
return(paths);
}

main:

char **A=NULL;
count=0;
A=getDirContents(dir1,NULL);
Aentries=count;
count=0;
//B=getDirContents(dir2,NULL);
printf("\nEntries in directory: %s",dir1);
for(i=0;i<Aentries;i++)
{
     printf("\n%s",A[i]);
}

count is a global variable

I just can’t figure out what is wrong I think I use the return command properly. I also tried the same code with paths as a global variable and it worked fine (main printed out the correct results).
I have a feeling it has something to do with the recursive call of my function

  • 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-31T02:56:22+00:00Added an answer on May 31, 2026 at 2:56 am

    Your code has an Undefined Behavior.

    You call the function as:

    A=getDirContents(dir1,NULL);
    

    and the function is defined as:

    char ** getDirContents(char *dirName,char **paths ) 
    

    Further you call realloc on paths.

    paths=(char**)realloc(paths,sizeof(char*)*(count+1));
    

    This causes Undefined Behavior.

    The standard mandates that the pointer being passed to realloc should exactly match the pointer which was allocated dynamic memory using a memory management function. Memory management functions specified by the standard are:
    aligned_alloc, calloc, malloc, and realloc.

    The pointer(paths) you are passing to realloc() was not returned by any of these and hence the Undefined Behavior.

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

Sidebar

Related Questions

I have created a function that takes a SQL command and produces output that
I have created a function in PHP that calls a webservice and parses through
Say I have a function func(i) that creates an object for an integer i,
I have the following function that takes a number like 5 and creates a
I have a win form that creates a site in IIS7. One function needs
Ok, I have one JavaScript that creates rows in a table like this: function
Modern browsers have multi-tab interface, but JavaScript function window.showModalDialog() creates a modal dialog that
When you have a function with a string parameter, does that create another instance
I have a dynamic class that I have created public dynamic class SiteZoneFileUploadVO {
How do I create a Ruby function that does not have an explicit number

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.