receive error when run program "Segmentation fault (core dumped)"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Folkes
    New Member
    • May 2013
    • 1

    receive error when run program "Segmentation fault (core dumped)"

    Hallo every body.

    I need help.,
    I have a program to read gps and accelerometer data from port. The programm work like this : when I send 'a' pros will receive gps data and send to database, when I send 'b', prog will send accelerometer data and save to database. But when I runn the proggram, gps data success to received and send to databse but accelerometer data canot receive and error is "segmentati on fault.
    Could you help me to resolve the prblem..?

    My script program is :
    Code:
    #include <stdio.h>   /* Standard input/output definitions */
    #include <string.h>  /* String function definitions */
    #include <unistd.h>  /* UNIX standard function definitions */
    #include <fcntl.h>   /* File control definitions */
    #include <string.h>
    #include <mysql/mysql.h>
    #include <stdlib.h>       // malloc, free, rand, for exit()
    
    void tokenizer(char *str,char *lat,char *lon)
    { 
      int i = 0;
      while(str[i]!='|') {
         lat[i] = str[i];
         i++;
      }	  
      lat[i] = '\0';
      i++;
      
      int j = 0;
      while(str[i]!='\0') {
    	 lon[j] = str[i];
    	 i++; 
    	 j++; 
      }	
      lon[j] = '\0';  
    }
    
    void tokenizer_acm(char *str,char *nilai_x,char *nilai_y,char *nilai_z,char *teg)
    { 
      int i = 0;
      while(str[i]!='|'){
         nilai_x[i] = str[i];
         i++;
      }	  
      nilai_x[i] = '\0';
      i++;
      
      int j = 0;
      while(str[i]!='|'){
    	 nilai_y[j] = str[i];
    	 i++; 
    	 j++; 
      }	
      nilai_y[j] = '\0';
      i++; 
      j++;
      
      int k = 0;
      while(str[i]!='|') {
    	 nilai_z[k] = str[i];
    	 i++; 
    	 j++; 
    	 k++;
      }	
      nilai_z[k] = '\0';
      i++; 
      j++; 
      k++;
      
      int l = 0;
      while(str[i]!='\0') {
    	 teg[l] = str[i];
    	 i++; 
    	 j++;
    	 k++;
    	 l++; 
      }	
      teg[l] = '\0';
      
    }
    
    
    int main(void) {
    	
        int fd;
        fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
        if (fd == -1) {
            perror("open_port: Unable to open port ");
    	} else {
            fcntl(fd, F_SETFL, 0);
        }
        
        char a[] = "a";
        char b[] = "b";
    
    	int n,m,cnt;
    	char in[30];
    	//char in2[50];
    	//char *in;
    	//char in2[100];
    	//in = (char*) malloc(i+1);
    	//if (in == NULL) exit(1);
    	
    	
    	MYSQL *conn;
    	
    	const char *localhost   = "127.0.0.1";
    	const char *user     = "root";
    	const char *password = "";
    	const char *database = "arduino1";
    	
    	conn = mysql_init(NULL);
    	
    	// Connect to database 
    	if (!mysql_real_connect(conn, localhost, user, password, database, 0, NULL, 0))
    	{
    		  fprintf(stderr, "%s\n", mysql_error(conn));
    		  exit(1);
    	} 
    			
    	
    	for(cnt=0; cnt<5; cnt++) 
    	{
    		if (cnt < 1) {
    			sleep(2);
    			n = write(fd, a, sizeof(a));
    			printf("Send : %s \n", a);
    			
    			//terima data gps dari port
    		    sleep(1);
    		    n = read(fd, in, 100);
    			if (n < 0)
    			{
    				perror("read");
    				break;
    			}
    			
    		    //query gps			
    			char c_lat[50],c_lon[50];
    			tokenizer(in,c_lat,c_lon);
    			
    			
    			//Isi nilai gps ke database
    			
    			char query[255];
    		    
    		    strcat(query,"INSERT INTO gps (latitude, longitude) VALUES (");
    				strcat(query,c_lat);
    				strcat(query,",");
    				strcat(query,c_lon);
    				strcat(query,")");
    		    
    			if (mysql_query(conn, query));
    			{
    			  printf("%s\n", query);
    			}
    			
    													
    	 } else {
    			m = write(fd, b, sizeof(b));
    			printf("Send : %s \n", b);
    			
    			//terima data accelerometer dari port
    			sleep(1);
    		    m = read(fd, in, 100);
    			
    			in[m] = '\0';	
    			
    			char str[255];
    			
    			//query	accelerometer	
    			char c_nilai_x[10],c_nilai_y[10],c_nilai_z[10],c_teg[6];
    			tokenizer_acm(in,c_nilai_x,c_nilai_y,c_nilai_z,c_teg);
    		    		
    			
    			//Isi nilai accelerometer ke database
    			
    			
    		    
    		    strcat(str,"INSERT INTO highcharts_php (x_axis, y_axis, z_axis, tegangan) VALUES (");
    				strcat(str,c_nilai_x);
    				strcat(str,",");
    				strcat(str,c_nilai_y);
    				strcat(str,",");
    				strcat(str,c_nilai_z);
    				strcat(str,",");
    				strcat(str,c_teg);
    				strcat(str,")");
    				
    			if (mysql_query(conn, str));
    			{
    			  printf("%s\n", str);
    			}
    			
    		}
    		
    		
    		// Close database connection
    		mysql_close(conn);
    	
    	}
    	
        return 0;
    }
    Last edited by Rabbit; May 21 '13, 02:19 PM. Reason: Please use code tags when posting code.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The problem appears to be a design weakness where functions receive pointers to arrays but have no idea how big the array is.

    The array appears to be created on the stack. Not good. If this array is overrun you will get the segmentation fault.

    I suggest redesigning your functions so there is an "number of elements" argument the function can use to be sure to not exceed the array bounds.

    You might also consider a design where functions that receive data only receive a small amount, like 10 or 20 byte using an argument from the calling function. They return the amount of data received. The calling function can then check to see if the return is the same as the amount requested. If it is the same the calling function knows there is more so it appends the received data to a dynamic array and calls the function again to receive the next increment. When the called function returns less than the amount requested, the calling function knows all data has been received. This design does not depend on any pre-defined array size.

    Comment

    Working...