segmentation fault

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
viorel2005
Membru
Membru
Posts: 208
Joined: 24 May 2008, 09:41

segmentation fault

Post by viorel2005 » 01 Sep 2016, 09:11

Buna ziua!

Am urmatorul cod:

Code: Select all

  GNU nano 2.2.6                                                      File: cpr_mover_1.cpp

#include <termios.h>
#include <signal.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include <list>
#include "kinematics.h"
#include "ProtocolCPRCAN.h"

#include <list>
#include <chrono>
#include <thread>
using namespace std;

struct robotState
{
        float p[6];             // cart position
        float j[6];             // joint position
        int errorCode[6];
        float duration; // duration for motion; needed for actionServer
};

kinematics kin;
ProtocolCPRCAN itf;                             // The hardware interface

//******************** MAIN ************************************************
int main(int argc, char** argv)
{
        return 0;
}



Cand il compilez pe Linux am eroarea la executie Segmentation fault.
Daca declar variabila itf in interiorul functiei main eroarea dispare.

De la adresa : https://github.com/CPR-Robots/cpr_mover ... olCPRCAN.h
se observa ca avem in clasa ProtocolCPRCAN declarata variabila CANInterfacePCAN itf;

Schematic lucrurile ar sta in felul urmator:

A.h /A.cpp avem clasa A;
B.h/B.cpp avem clasa B care are o variabila membru: A m;
Fisierul cod.cpp unde avem :

B x;

void main()
{
}

gcc -std=c++11 cod.cpp B.cpp A.cpp -o cod
si am la executie eroarea segmentation fault. Cu debugger-ul am _static_initialization_and_destruction.

Cum pot declara variabila itf globala?
Codul ruleaza sub Linux.



0ptr
Membru
Membru
Posts: 71
Joined: 01 Feb 2011, 23:27
Judet: Ilfov

Re: segmentation fault

Post by 0ptr » 02 Sep 2016, 13:08

Ti-as recomanda sa nu folosesti variabile globale pe cat posibil.
Googlaleste putin dupa "why to avoid global variable in C++". Succes!

Pop Alexandra
Junior
Junior
Posts: 6
Joined: 23 Mar 2017, 22:56
Judet: Cluj
Location: Cluj-Napoca, Romania
Contact:

Re: segmentation fault

Post by Pop Alexandra » 24 Mar 2017, 00:20

Salut! Ai reusit sa rezolvi problema evitand variabilele globale?
Multumesc anticipat pentru raspuns. Ma confrunt cu o problema similara si vreau sa stiu de trebuie s-o iau de la capat :(

Post Reply