Thread: memory allocation problem....help..

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    25

    memory allocation problem....help..

    Hi, I got a problem with memory allocation (that's my assumption) in my prog...
    My program purpose is to read from file and put it inside the struct array TRecord.

    this is the file format:
    Code:
    915   200   	| buy supplies for morning tea
    1030   15       | rob the bank
    1045   60       | pay the programmer
    1115   100 	| phone new client
    This is my struct:
    Code:
    struct tasks
    {
    	int  in_time; /*contain the 1st int eg: 915 from line 1*/
    	int  complete_time; /*contain the 2nd int eg: 200 from line 1*/
    	char  *description; /*the description without "|" */
    } TRecord;
    I ve sucessfully insert all the stuff inside my array, in which I proved with printing the array, HOWEVER this only work when I print from inside my function "insert_task" below, IF I try to print from main(), it give me segmentation fault (which 99% memory allocation problem). This is my main & function:

    Code:
    main (argc......,argv....)
    {
                    FILE *fp;
    	int i;
    	TRecord *trec;
    
    	workerfile = open_file( argv[0], argv[1], "r" );
    
                    insert_task( trec, workerfile );
    
    /*	i = 1;
    
    	while( trec[i] != NULL )
                   {
    		printf( "%d\n", trec[i].in_time );
    	                printf( "%d\n", trec[i].complete_time );
    		printf( "%s\n", trec[i].description );
    		
    		i++;
    	}
    */ /* this is the loop which cause the error*/
                    return 0;
    }
    
    void insert_task( TRecord *trec, FILE *fp )
    {
    	
    	char *string;
    	char *taskdescp;
    	char buffer[BUFFSIZE];
    	char *tokenized;
    	int array_subscript, i, len;
    	array_subscript = 1;
    	i = 1;
    		
    	fgets( buffer, BUFFSIZE, fp );
    	len = strlen( buffer );
    	string = ( char* ) malloc( ( len + 1 ) * sizeof( char ) );
    	strcpy( &string[i], buffer );
    	
    	while( ......)
    	{
    		....
                                    ....
             		....
    		printf( "%d\n", trec[array_subscript].in_time );
    		printf( "%d\n", trec[array_subscript].complete_time );
    		printf( "%s\n", trec[array_subscript].description );
    		
    		array_subscript++;
    		i++;
    		
    		fgets( buffer, BUFFSIZE, fp );
    		len = strlen( buffer );
    		string = ( char* ) malloc( ( len + 1 ) * sizeof( char ) );
    		strcpy( &string[i], buffer );
    
    	}
    	
    /* you see a few line above that printf from inside the functions work fine, all printed out perfectly */
    	return;
    
    }
    Anyone has the solution?
    I believe someone will give me this solution which is changing:
    TRecord *trec; into TRecord trec[ASIZE];
    I dont want to do this since it's not allowed in the proj spec...


    thanks for the time to read my code,

    Ferdinand
    Last edited by CyC|OpS; 10-18-2002 at 09:42 AM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You never allocate memory for TRec, so you're trying to access something that doesn't exist.
    Plus, you never free any memory that you allocate. This will lead to memory leaks.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    One problem I see at a quick glance is the fact that the description element of the struct is a pointer, and you point it to memory that is local to the function insert_task(). Thus when you return to main(), is points to invalid memory.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    You never allocate memory for TRec, so you're trying to access something that doesn't exist.
    Plus, you never free any memory that you allocate. This will lead to memory leaks.
    umm..I "know" that was the problem, and I tried to allocated memory for TRec, but honestly I dont know where should I do it, and I dont know the size of TRec (how many tasks are in the array)...and about the memory leak, where is it? and how to free memory?

    One problem I see at a quick glance is the fact that the description element of the struct is a pointer, and you point it to memory that is local to the function insert_task(). Thus when you return to main(), is points to invalid memory.
    um...I dont really get what are you trying to tell me...can you explain it in other word?

    thanks,

    Ferdinand

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    All those "&string[i]" are pointing to nowhere as well.
    is this mean I can't use &string[i]?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's an example of a working program. It doesn't do everything you want, t only serves to give you an example and some guidance.

    Note that it doesn't use malloc(). If you're new, and not comfortable with dynamic memory allocation, avoid it. Once you understand how to do it with arrays, then move to dynamic allocation.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_RECORDS 10
    #define MAXL_DESCRIPTION 100
    
    struct tasks 
    {
        int  in_time;
        int  complete_time;
        char  description[MAXL_DESCRIPTION];    
    };
    
    void GetData(struct tasks TasksArray[], int Count);
    
    int main(void)
    {
        struct tasks TRecord[MAX_RECORDS];
        
        GetData(TRecord, MAX_RECORDS);
        
        printf( "%d\n", TRecord[0].in_time );
        printf( "%d\n", TRecord[0].complete_time );
        printf( "%s\n", TRecord[0].description );
        
        return 0;
    }
        
    void GetData(struct tasks TasksArray[], int Count)
    {
        int i;
        
        for (i = 0; i < Count; i++)
        {
            TasksArray[i].in_time = i + 100;
            TasksArray[i].complete_time = i + 200;
            strcpy(TasksArray[i].description, "My description here\n");
        }
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    Thanks Hammer & Salem for your help,

    for Hammer : I must use dynamic memory unfortunatly.

    for Salem : ypu're right....I dont know the size of the file.. =( , is there any other way to dynamically allocated memory?

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, use dynamic memory to allocate the entire struct, but do you have to have the description element as a second dynamic call? What I mean is, define you struct like in my example, then use Salem's malloc example to create an instance of said struct. That way you only need one malloc call per struct.

    If you had to use a pointer for description (instead of an array), you'd have to first malloc the struct, then malloc more memory for the description text.

    Each call to malloc is an overhead, but it does allow you to allocate the exact amount of memory required. If you can live with wasting a few bytes on the description array, I'd do it the way I suggest here.

    Either way, remember to free() every piece of memory that you malloc().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    hei thanks to both of you,

    Hammer : yeah, maybe I just give up the description dynamic memory allocation...should be alot easier...

    Salem : I'll try to read the file first and allocating memory to the struct....I'll be back later..

    thanks heap,

    Ferdinand

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 04:45 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM