Thread: Segmentation fault problem

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    66

    Segmentation fault problem

    Hi.

    I`m struggling with the following function for almost 2 hours now and I can`t figure out what is the problem with it, maybe you can help me.

    The function lists the files and folders in a specified folder, and loads the result into a FileData array.

    Code:
    typedef struct
    {
    	char *fileName;
    	int isFile;
    } FileData;
    
    FileData **fs_listFiles(const char *path)
    {
    	DIR *dir;
    	struct dirent *ent;
    	FileData **fd;
    	int i;
    
    	dir = opendir (path);
    
    	if (dir != NULL)
    	{
    		while ((ent = readdir (dir)) != NULL)
    		{
    			fd[i] 				= (FileData **)malloc(sizeof(FileData*));
    			fd[i]->fileName = ent->d_name;
    
    			printf("%s\n",fd[i]->fileName);
    
    			i++;
    		}
    
    		closedir (dir);
    		return fd;
    	}
    
    	return NULL;
    }
    When I run this piece of code, it breaks with segmentation faul at the fd[i]->fileName = ent->d_name; part. I also tried to use realloc like this: fd = (FileData **)realloc((void *)fd, sizeof(FileData*)*(i+1)); but still no luck.

    If i print the size of the allocated memory for the fd variable it doesnt break, and the size itself seems to be ok.

    Can anybody help me?

    Thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you are going to use fd[i], then fd[i] should exist, first. Your realloc is exactly what you need for that, followed by
    Code:
    fd[i] = (FileData *)malloc(sizeof(FileData));
    Notice the proper amount of stars. Also, you shouldn't use (FileData *) there anyway: either #include <stdlib.h> like you should, or make sure your filename ends in .c and not .cpp.

    ETA: Oops: and as iMalc points out below, "fd[i]->fileName = ent->d_name;" isn't what you want. You probably want to malloc some space and then strcpy it over. Right now, what you have works-ish, except for the fact that all your pointers are pointing to the same place, so all your files have the same name. You'll need some separate storage for each filename.
    Last edited by tabstop; 07-18-2011 at 01:23 PM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You have to malloc three things for the fd array to work, and none of those should look like the one you have.
    1. An array of pointers
    2. Each FileData struct
    3. The fileName within each struct

    Don't cast malloc at all, especially considering that's the wrong cast anyway.
    Don't use realloc; the chances of you using it correctly are very slim, and it's not worth it to use it anyway.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    66
    First of all, thank you for you quick responses.

    I`m sorry, at the fd[i] = (FileData **)malloc(sizeof(FileData*)); part the cast supposed to be just (FileData *) when i copied the code here i changed the realloc to malloc, and the cast remained unchanged.

    Anyway, i tried what tabstop suggested, first realloced the array, and malloced the struct on the specified index, and now it works as it supposed to.

    Thank you again for your help.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by raczzoli View Post
    Code:
    		while ((ent = readdir (dir)) != NULL)
    		{
    			fd[i] = (FileData **)malloc(sizeof(FileData*));
    			fd[i]->fileName = ent->d_name;
    Firt you can't assign strings across the equals sign in C... all you're doing it assigning a pointer that is wiped out on the next iteration of your loop.

    You need to actually copy the filename into your struct...

    Code:
    typedef struct
    {
    	char fileName[256];
    	int isFile;
    } FileData;
    
    
    // then
    
    strcpy(fd[i]->fileName, ent->d_name);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MPI Problem gives segmentation fault
    By confused_coder in forum C Programming
    Replies: 6
    Last Post: 09-07-2009, 09:34 PM
  2. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  3. Segmentation Fault Problem: Urgent Help
    By bodydrop in forum C Programming
    Replies: 3
    Last Post: 05-05-2006, 08:02 PM
  4. problem with segmentation fault
    By cBegginer in forum C Programming
    Replies: 13
    Last Post: 05-15-2005, 11:51 AM
  5. Segmentation fault problem
    By robsmith in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 05:33 PM