473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

1
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 "segmentation fault.
Could you help me to resolve the prblem..?

My script program is :
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>   /* Standard input/output definitions */
  2. #include <string.h>  /* String function definitions */
  3. #include <unistd.h>  /* UNIX standard function definitions */
  4. #include <fcntl.h>   /* File control definitions */
  5. #include <string.h>
  6. #include <mysql/mysql.h>
  7. #include <stdlib.h>       // malloc, free, rand, for exit()
  8.  
  9. void tokenizer(char *str,char *lat,char *lon)
  10.   int i = 0;
  11.   while(str[i]!='|') {
  12.      lat[i] = str[i];
  13.      i++;
  14.   }      
  15.   lat[i] = '\0';
  16.   i++;
  17.  
  18.   int j = 0;
  19.   while(str[i]!='\0') {
  20.      lon[j] = str[i];
  21.      i++; 
  22.      j++; 
  23.   }    
  24.   lon[j] = '\0';  
  25. }
  26.  
  27. void tokenizer_acm(char *str,char *nilai_x,char *nilai_y,char *nilai_z,char *teg)
  28.   int i = 0;
  29.   while(str[i]!='|'){
  30.      nilai_x[i] = str[i];
  31.      i++;
  32.   }      
  33.   nilai_x[i] = '\0';
  34.   i++;
  35.  
  36.   int j = 0;
  37.   while(str[i]!='|'){
  38.      nilai_y[j] = str[i];
  39.      i++; 
  40.      j++; 
  41.   }    
  42.   nilai_y[j] = '\0';
  43.   i++; 
  44.   j++;
  45.  
  46.   int k = 0;
  47.   while(str[i]!='|') {
  48.      nilai_z[k] = str[i];
  49.      i++; 
  50.      j++; 
  51.      k++;
  52.   }    
  53.   nilai_z[k] = '\0';
  54.   i++; 
  55.   j++; 
  56.   k++;
  57.  
  58.   int l = 0;
  59.   while(str[i]!='\0') {
  60.      teg[l] = str[i];
  61.      i++; 
  62.      j++;
  63.      k++;
  64.      l++; 
  65.   }    
  66.   teg[l] = '\0';
  67.  
  68. }
  69.  
  70.  
  71. int main(void) {
  72.  
  73.     int fd;
  74.     fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
  75.     if (fd == -1) {
  76.         perror("open_port: Unable to open port ");
  77.     } else {
  78.         fcntl(fd, F_SETFL, 0);
  79.     }
  80.  
  81.     char a[] = "a";
  82.     char b[] = "b";
  83.  
  84.     int n,m,cnt;
  85.     char in[30];
  86.     //char in2[50];
  87.     //char *in;
  88.     //char in2[100];
  89.     //in = (char*) malloc(i+1);
  90.     //if (in == NULL) exit(1);
  91.  
  92.  
  93.     MYSQL *conn;
  94.  
  95.     const char *localhost   = "127.0.0.1";
  96.     const char *user     = "root";
  97.     const char *password = "";
  98.     const char *database = "arduino1";
  99.  
  100.     conn = mysql_init(NULL);
  101.  
  102.     // Connect to database 
  103.     if (!mysql_real_connect(conn, localhost, user, password, database, 0, NULL, 0))
  104.     {
  105.           fprintf(stderr, "%s\n", mysql_error(conn));
  106.           exit(1);
  107.     } 
  108.  
  109.  
  110.     for(cnt=0; cnt<5; cnt++) 
  111.     {
  112.         if (cnt < 1) {
  113.             sleep(2);
  114.             n = write(fd, a, sizeof(a));
  115.             printf("Send : %s \n", a);
  116.  
  117.             //terima data gps dari port
  118.             sleep(1);
  119.             n = read(fd, in, 100);
  120.             if (n < 0)
  121.             {
  122.                 perror("read");
  123.                 break;
  124.             }
  125.  
  126.             //query gps            
  127.             char c_lat[50],c_lon[50];
  128.             tokenizer(in,c_lat,c_lon);
  129.  
  130.  
  131.             //Isi nilai gps ke database
  132.  
  133.             char query[255];
  134.  
  135.             strcat(query,"INSERT INTO gps (latitude, longitude) VALUES (");
  136.                 strcat(query,c_lat);
  137.                 strcat(query,",");
  138.                 strcat(query,c_lon);
  139.                 strcat(query,")");
  140.  
  141.             if (mysql_query(conn, query));
  142.             {
  143.               printf("%s\n", query);
  144.             }
  145.  
  146.  
  147.      } else {
  148.             m = write(fd, b, sizeof(b));
  149.             printf("Send : %s \n", b);
  150.  
  151.             //terima data accelerometer dari port
  152.             sleep(1);
  153.             m = read(fd, in, 100);
  154.  
  155.             in[m] = '\0';    
  156.  
  157.             char str[255];
  158.  
  159.             //query    accelerometer    
  160.             char c_nilai_x[10],c_nilai_y[10],c_nilai_z[10],c_teg[6];
  161.             tokenizer_acm(in,c_nilai_x,c_nilai_y,c_nilai_z,c_teg);
  162.  
  163.  
  164.             //Isi nilai accelerometer ke database
  165.  
  166.  
  167.  
  168.             strcat(str,"INSERT INTO highcharts_php (x_axis, y_axis, z_axis, tegangan) VALUES (");
  169.                 strcat(str,c_nilai_x);
  170.                 strcat(str,",");
  171.                 strcat(str,c_nilai_y);
  172.                 strcat(str,",");
  173.                 strcat(str,c_nilai_z);
  174.                 strcat(str,",");
  175.                 strcat(str,c_teg);
  176.                 strcat(str,")");
  177.  
  178.             if (mysql_query(conn, str));
  179.             {
  180.               printf("%s\n", str);
  181.             }
  182.  
  183.         }
  184.  
  185.  
  186.         // Close database connection
  187.         mysql_close(conn);
  188.  
  189.     }
  190.  
  191.     return 0;
  192. }
May 21 '13 #1
1 2906
weaknessforcats
9,208 Expert Mod 8TB
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.
May 21 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: N.S. du Toit | last post by:
Just having a bit of trouble programming with C under FreeBSD 5.1 using the gcc compiler. I'm a bit new to C so my apologies if the answer to my question appear obvious :) Basically I've...
22
by: Mike Polinske | last post by:
I am new to the C programming language but have been programming in Cobol for over 10 years. When I compile the following code, it compiles clean but I get an application error both under Windows...
0
by: dboileau | last post by:
Can anyone help me out with this error, I am trying to compile mysql with gcc 3.4.6 (Compiled from source) using CC=gcc CFLAGS="-O3 -mcpu=v8 -Wa,-xarch=v8plusa" \ CXX=gcc CXXFLAGS="-O3...
29
by: DanielJohnson | last post by:
I wrote this small program to reverse each word in the string. For example: "I love You" should print as "I evoL uoY". I get Segmentation Fault (core dumped) error upon running the program. It...
2
by: Verdana | last post by:
We're using Python 2.5 on our production and testing servers, which both run SunOS 5.8 and Oracle 10g. The script we're working on is supposed to process wddx packets, enter some info in the database...
4
by: Willy Wijaya | last post by:
I have wrote this code.. But when i try to compile it using gcc in PCLinuxOS 2007, i got this message "Help me about segmentation fault (core dump)" when I run the client script.. //CLIENT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.