Thread: Can't malloc from within a function

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    101

    Can't malloc from within a function

    I have the following code that tries to malloc a pointer to a struct outside of the function. Everything works fine inside the function, but once it completes, it seems my memory is lost.

    Code:
    typedef struct {
    	char name[1024];
    } FileList;
    
    void read_files(FileList *filelist){
    	
    	long filecount = 5;
    	long i;
    
    	filelist = (FileList*)malloc(sizeof(FileList)*filecount);
    	for(i=0;i<filecount;i++){
    		sprintf(filelist[i].name,"filename%d",i);
    		fprintf(stderr,"Filename %d was %s\n",i,filelist[i].name); //works
    	}
    }
    
    int main(int argc,char **argv){
    	
    	FileList *files;
    	read_files(files);
    	fprintf(stderr,"Filename 0 was %s\n",files[0].name); //doesn't work (returns NULL)
    
    	return 0;
    }
    What can I do to fix this?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    To change a variable in a function and have the change seen outside a function you need to return it; or, pass the pointer to the variable.

    This applies to pointers, you need to pass a pointer to the pointer.

    Tim S.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that you are assigning to the pointer from within the function. Like other parameters, changing it in the function does not affect the variable in the caller because pass by value is used. Hence, if you want to do it this way, you should pass a pointer to a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    101
    Thanks. I think I figured it out:

    Code:
    typedef struct {
        char name[1024];
    } FileList;
     
    void read_files(FileList **filelist){
         
        long filecount = 5;
        long i;
     
        *filelist = (FileList*)malloc(sizeof(FileList)*filecount);
        for(i=0;i<filecount;i++){
            sprintf((*filelist)[i].name,"filename%d",i);
            fprintf(stderr,"Filename %d was %s\n",i,(*filelist)[i].name); //works
        }
    }
     
    int main(int argc,char **argv){
         
        FileList *files;
        read_files(&files);
        fprintf(stderr,"Filename 0 was %s\n",files[0].name); //works!
     
        free(files);
        return 0;
    }
    Last edited by synthetix; 11-02-2011 at 12:28 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Remember to free what you malloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    101
    Quote Originally Posted by laserlight View Post
    Remember to free what you malloc.
    Good call. I updated the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc in function
    By mugen in forum C Programming
    Replies: 13
    Last Post: 03-14-2010, 11:27 AM
  2. malloc function
    By roaan in forum C Programming
    Replies: 9
    Last Post: 08-14-2009, 04:48 AM
  3. using malloc in a function
    By cuizy in forum C Programming
    Replies: 10
    Last Post: 08-13-2009, 01:56 PM
  4. Help with Malloc() function
    By xp5 in forum C Programming
    Replies: 12
    Last Post: 09-19-2007, 03:17 PM
  5. about malloc function
    By enes in forum C Programming
    Replies: 1
    Last Post: 01-27-2002, 09:33 AM