segmentation fault in my c code -


i getting segmentation fault in code reason that.
think because of while loop in cur = readdir(dr); not entering while loop when trying give wrong entry.

int main(char *source)   {     dir *dr;     struct dirent *cur;     struct stat fi;     long int total_size = 0;     dr = opendir(source);     char *name=source;     size_t sourcelen = strlen(source);     printf("\n\n source %s\n\n\n", source);     printf("before *path \n");     char *path = malloc(100);     strcpy(path,source);     printf("before while \n");     while ((cur = readdir(dr)) != null)     {        if(cur->d_name[0] != '.')             {                     free(path);                     path = malloc(sourcelen + strlen(cur->d_name) + 2);                     strcat(path,source);                     strcat(path,"/");                     strcat(path,cur->d_name);                     if(stat(path, &fi) == -1)                    {                             printf("error \n\n");                     }                     else                     {                      printf("%s  ",cur->d_name);                     printf("%ld  ",fi.st_blocks);                     total_size = total_size + fi.st_blocks;                     }             }     }     printf("\n\ntotal_size = %ld \n", total_size);     printf("\n\n\n"); 

}

you have:

int main(char *source) 

this totally unorthodox , unsupported definition of main(). see what should main() return in c , c++ full story of , not allowed.

it cause of segmentation fault too. have:

dir *dr; … dr = opendir(source); 

you have got (small non-zero) int being treated (part of?) char *, going lead trouble.

you need:

int main(int argc, char **argv) {     if (argc != 2)         …report error , not continue…     dir *dr = diropen(argv[1]);     …error check dr , continue if not null… 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -