Thread: Array Fundamentals: memory usage

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    62

    Array Fundamentals: memory usage

    I have a function that is called within a loop. The array in this function is 382 in length. The output of the program is usually no more than about 50 characters.

    My problem is that after the function has looped approx 21 times the app seg faults. Just as a test I increased the array to 4096 from 382. The app now seg faults after 235 loops.

    Code:
    void Func_Get_Details()
    	{
    		char temp[MAX_DIR_LENGTH] = {0x0};
    		char t[30] = {0x0};
    		char tt[2] = {0x0};
    		int hours = 0;
    		int minutes = 0;
    		int seconds = 0;
    		
    		
    		
    		file_read = popen("cat /home/text.txt","r");
    		while(fgets(temp,sizeof (temp),file_read))
    			
    			delay = atoi(temp);
    			
    		pclose(file_read);
    		
    				
    		if(delay < 15)
    		{
    			delay = 15;
    		}
    		
    		if(delay > 600)
    		{
    			delay = 600;
    		}
    		
    		file_read = popen("cat /home/text2.txt","r");
    		while(fgets(temp,sizeof temp,file_read))
    			strcat (img_main, temp);
    		pclose(file_read);
    		
    		
    		time_t s;
    		time(&s);
    		sprintf(t,"%s",ctime(&s));
    		
    		
    		strncpy(tt, t + 11, 12);
    		tt[2] = '\0';
    		hours = atoi(tt);
    				
    		
    		strncpy(tt, t + 14, 15);
    		tt[2] = '\0';
    		minutes = atoi(tt);
    		
    		
    		strncpy(tt, t + 17, 18);
    		tt[2] = '\0';
    		seconds = atoi(tt);
    		printf("Seconds %i\n", seconds);
    		
    		
    		printf("%i", hours);
    		printf("%i", minutes);
    		printf("%i\n", seconds);
    		
    		change_time = (hours * 3600) + (minutes * 60) + seconds;
    		printf("number of seconds since midnight %i", change_time);
    		printf("\nTemp variable is %s: \n",temp);
    		
    	}

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I'd say there is a good chance your seg fault is coming from this line:
    Code:
    strcat (img_main, temp);
    You need to include some sort of logic that makes sure you don't overflow the img_main buffer.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    char tt[2] = {0x0};
    ...
    tt[2] = '\0';
    This too is overstepping the end of the array.


    > file_read = popen("cat /home/text2.txt","r");
    What's wrong with
    file_read = fopen("/home/text2.txt","r");

    > strcat (img_main, temp);
    Uncontrolled appending to a string will burn you every time.
    It's also horribly inefficient for larger files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  2. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  3. Replies: 1
    Last Post: 03-30-2004, 02:57 PM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM