Thread: segmentation error

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    10

    segmentation error

    Hi,
    I have some problems when compiling a program. I compile the program using:
    gcc -o main main.c NN_ev3.a -lm, but i get a segmentation error. I suspect that the eror lies in how i am defining global variables in my main function.

    A link to my files are given here:
    the .tar.gz file
    NN_ev3.tar_2.gz[COLOR="Silver"]
    or .zip file
    NN_ev3.zip

    Can someone help me?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Why don' you simply post relevant code in code tag here?? Use debugger /valgrind etc..

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Two dozen source and header files - you really need to learn how to debug your own code.

    A quick lesson.

    1. Compile for debugging
    gcc -g -o main main.c NN_ev3.a -lm

    Add the -g flag to all the compilations which generate your .a file as well.

    2. Load program inside the debugger
    gdb ./main

    3. Run the program
    (gdb) run

    4. When it crashes, find out where you are
    (gdb) bt
    'bt' is back-trace, it shows you all the nested function calls at the point where it crashed.
    From here you can (read the manual)
    - print variables (print)
    - traverse the stack (up, down)
    - set breakpoints for another run
    When you know where it crashed, work out how it got to that state, then track back through the code to figure out how to avoid getting into that state in the first place.

    Rinse and repeat until your program is (tolerably) bug-free.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I'm not even interested in downloading those gzipped or zipped files.

    A segmentation violation means the operating system has detected your code modifying some area of memory that is not allowed to be modified. In C code, there are many possible ways this can happen. The most common, practically, is a problem with handling of pointers or arrays - and this class of error occurs in many many forms. For example, dereferencing an uninitialised pointer, accessing the fifth element of an array that only has three elements.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    10
    I think my problem relies in the way i am defining my defining my extern variables. the extern variales are defined in one header files as:
    Code:
    #ifndef __NN_EV3_DATA_H__
    #define __NN_EV3_DATA_H__
    /*
     * NN_ev3_data.h
     *
     * Embedded MATLAB Coder code generation for function 'NN_ev3_data'
     *
     * C source code generated on: Fri Mar 11 13:21:39 2011
     *
     */
    
    
    /* Include files */
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    #include "rt_nonfinite.h"
    #include "rt_pow_snf.h"
    
    #include "rtwtypes.h"
    #include "NN_ev3_types.h"
    
    /* Type Definitions */
    
    /* Named Constants */
    
    /* Variable Declarations */
    extern real_T eta;
    extern real_T g_max;
    extern real_T x_target;
    extern real_T y_target;
    extern real_T xold;
    extern real_T yold;
    extern real_T r_old;
    extern real_T Oh_old[5];
    extern real_T DO_Whi_old[115];
    extern real_T DO_R_old[5];
    
    /* Variable Definitions */
    
    /* Function Declarations */
    #endif
    /* End of Embedded MATLAB Coder code generation (NN_ev3_data.h) */
    and this is the way i am declaring it in my main function:
    Code:
    /* Variable Definitions */
    
    real_T eta;
    real_T g_max;
    real_T x_target;
    real_T y_target;
    real_T xold;
    real_T yold;
    real_T r_old;
    real_T Oh_old[5];
    real_T DO_Whi_old[115];
    real_T DO_R_old[5];
    
    static real_T Whi[115];
    static real_T Woh[6];
    static real_T R[5];
    
    
    int main(){....
    Is what i am doing correct?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, it's correct.

    Now stop guessing and start debugging!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    10
    I followed the mentioned steps, except the following:
    Quote Originally Posted by Salem View Post
    Add the -g flag to all the compilations which generate your .a file as well.
    I couldnt apply it, coz I didn't directly generate the .a file.

    Code:
    user@user-OptiPlex-GX280:~/Desktop/NN_ev3$ gcc -g -o main main.c NN_ev3.a -lm
    user@user-OptiPlex-GX280:~/Desktop/NN_ev3$ gdb ./main
    GNU gdb (GDB) 7.2-ubuntu
    Copyright (C) 2010 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "i686-linux-gnu".
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>...
    Reading symbols from /home/user/Desktop/NN_ev3/main...done.
    (gdb) run
    Starting program: /home/user/Desktop/NN_ev3/main 
    Entering NN_ev3
    
    Program received signal SIGSEGV, Segmentation fault.
    0x08048b45 in NN_ev3 ()
    (gdb) bt
    #0  0x08048b45 in NN_ev3 ()
    #1  0x080488ab in main () at main.c:45
    (gdb)
    The fault appears in NNev3(). I added a printf command in the function NNev3() right after i finish declaring the variables. nothing got printed on the screen. What can I do next?

    Problem solved. Thank you for your help and also for pointing out that I should be debugging instead of guessing.
    Last edited by MASX; 03-12-2011 at 07:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM