Why I have a segmentation fautl?


Could anybody help me with this code?, because i have problemsofsegmentation faultnut i don't understand why.

Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int sq(int x)
{
  return x*x;
}

class Coordonnees
{
public:
  Coordonnees(int un_x, int un_y) : x_(un_x), y_(un_y) {}
  int x() const {
    return x_;
  }
  int y() const {
    return y_;
  }
private:
  int x_;
  int y_;
};

class Neurone
{
  /*****************************************************
   * Compléter le code à partir d'ici
   *****************************************************/

public:
  Neurone(int posx, int posy, double attenuation) 
  : position_(Coordonnees(posx, posy)), attenuation_(attenuation)
  {
    signal_ = 0.0;
  }
  
  Coordonnees const& position() const
  {
    return position_;
  }
  
  double signal() const
  {
    return signal_;
  }
  
  virtual void accumuler(double signal_recu)
  {
    signal_ = signal_recu * attenuation_;
  }
  
  void propager() const
  {
    for (auto const& n : fils)
	{
	  n->fire(signal_);
	}
  }
  
  void fire(double signal_recu)
  {
    accumuler(signal_recu);
	propager();
  }
  
  ostream& afficher(ostream& sortie) const
  {
    sortie << "Le neurone en position " <<"("<< position_.x()<< ", " << position_.y() << ")";
	
	sortie << " avec une attenuation de " << attenuation_;
	if (fils.empty())
	  sortie << " n'est connnecté à aucun neurone." << endl;
	else
	{
	  sortie << " est connecté au(x) neurone(s) suivant(s) :" << endl;
	  for (auto const& n : fils)
	  {
	    sortie << "  - neurone en position " <<"("<< n->position_.x() <<", " << n->position_.y() << ")"<< endl;
	  }
	}
	return sortie;
  }

    Neurone& operator+=(Neurone* n2)
  {
	fils.push_back(n2);
    return *this;
  }

private:
  Coordonnees position_;
  vector<Neurone*> fils;
	Neurone(Neurone const& n) = delete;
	Neurone operator=(Neurone const& n) = delete;

protected:
	double signal_;  
	double attenuation_;
};

ostream& operator<<(ostream& sortie, Neurone const& n)
{
  n.afficher(sortie);
  return sortie;
}

double distance(Coordonnees const& c1, Coordonnees const& c2)
{
  int dx = c1.x() - c2.x();
  int dy = c1.y() - c2.y();
  return sqrt(sq(dx) + sq(dy));
}

ostream& operator<<(ostream& sortie, Coordonnees const& c)
{
  sortie << "(" << c.x() << ", " << c.y() << ")";
  return sortie;
}
class NeuroneCumulatif: public Neurone
{
	public:
  
  NeuroneCumulatif(int a, int b , double c):Neurone(a, b ,c){}
  
  virtual void accumuler(double signal_recu)
  {
    signal_ = signal_+signal_recu * attenuation_;
  }	

};

class Cerveau
{
	public:
	
	Cerveau()
	{
     connectome_[0]=premier;
     connectome_[1]=second;
     connectome_[2]=third;
     connectome_[3]=fourth;
    *premier+=second;
    *premier+=third;
	*fourth+=second;
	*fourth+=third;
	}


 ostream& afficher(ostream& sortie) const
  {

	sortie<<"Un cerveau à 4 neurone(s) :"<<endl;

	  for (unsigned int i =0 ;i<4;i++)
	  {
	    sortie << connectome_[i];
	  }


   return sortie;
}    	

	
	void plante_electrode(int x, int y, double signal)
	{
		Coordonnees p(x,y);
		unsigned int i;
		i= position_vers_index(p);
		connectome_[i]->fire(signal);
	}
	 
	 
	 double eeg(Coordonnees const& p) const
	 {
	   unsigned int i =position_vers_index(p);
	   return connectome_[i]->signal();
		 }
	
	
	protected:
	
    Neurone * premier = new Neurone(0,1,0.5);    
    Neurone * second = new Neurone(1,0,1.0);
    Neurone * third = new Neurone(1,1,2.0);
    Neurone * fourth =new Neurone(0,0,0.5);        
    
    
    vector<Neurone *> connectome_ ; 
	 
	 private:
	 size_t position_vers_index(Coordonnees const& p) const {
        int i(0);
        double dist(100);
        for (const auto& connection  : connectome_) {
            double d= distance(connection->position(), p);
            if (d < dist) { dist = d; i++; };
        }
        return i;
    };


	};

ostream& operator<<(ostream& sortie, Cerveau  const& n)
{
  n.afficher(sortie);
  return sortie;
}

/*******************************************
 * Ne rien modifier après cette ligne.
 *******************************************/
int main ()
{
  // ==================================================
  // Test de la partie 1
  cout << "===== Test de la partie 1 =====" << endl << endl;

  Neurone neurone1(0, 1, 0.5);
  Neurone neurone2(1, 0, 1.0);
  Neurone neurone3(1, 1, 2.0);

  neurone1 += &neurone2;
  neurone1 += &neurone3;
  neurone2 += &neurone3;

  cout << neurone1 << endl;

  neurone1.fire(10);
  cout << "Signaux :" << endl;
  cout << neurone1.signal() << endl;
  cout << neurone2.signal() << endl;
  cout << neurone3.signal() << endl;

  // ==================================================
  // Test de la partie 2
  cout << endl << "===== Test de la partie 2 =====" << endl << endl;

  NeuroneCumulatif neurone4(0, 0, 0.75);
  cout << neurone4 << endl;
  neurone4.fire(10);
  neurone4.fire(10);
  cout << "Signal :" << endl;
  cout << neurone4.signal() << endl;

  // ==================================================
  // Test de la partie 3
  cout << endl << "===== Test de la partie 3 =====" << endl << endl;

  Cerveau my_second_favorite_organ; // Woody Allen
  cout << my_second_favorite_organ << endl;

  my_second_favorite_organ.plante_electrode(0, 1, 10);
  const Coordonnees where(0, 0);
  cout << "Le neurone en " << where << " a pour signal "
       << my_second_favorite_organ.eeg(where)
       << endl;

  return 0;
}

I tried to change the vector connectome_ because i knox that there is the problem.


So in the constructor of the class Cerveau:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Cerveau
{
	public:
	
	Cerveau()

	{
     connectome_.push_back(premier);
     //connectome_.push_back(second);
     //connectome_.push_back(third);
     //connectome_.push_back(fourth);


cout<<connectome_[0];  

    *premier+=second;
    *premier+=third;
    *second+=fourth;
    *third+=fourth;
    } 


But i havenothing in the first position , Any idea?

closed account (j3Rz8vqX)
Segmentation fault usually occurs when you're trying to access/modify memory that isn't available; possibly a loop you've got going out of bound.

Vectors are super safe, so unless your not pushing back objects, there shouldn't be any real conflict.

Check out your for-loops, and anywhere you've tried to access or modify your vectors contents.

I know whay's mean segmentation fault.
And I have a problem with my vector connectome_

I don't know how inisalise the vector ,

If i Have a class Neurone , like the code that I post

Andin my class Cerveau , i have :

Neurone * premier= new Neurone(1,1,0.5);

but only with this value , I have to inisialise my vector in the constructor of Cerveau:


Cerveau
{
connectome_.push_back(premier);


}

But if i print
cout<<connectome_[0];

I haveonly garbage , so I don't know how to asign the spaceof memory of premier to my first position in the vector connectome_, Any idea?
closed account (j3Rz8vqX)
I cannot entirely understand what you are asking, but if it's how to implement the initialization of a vector, the below is a possible example of vector declaration:
1
2
3
4
//Declaring vectors
vector<Neurone> connectome_;//vector of objects type  Neurone
//or
vector<Neurone*> connectome_;//vector of pointers to object type Neurone 


Vector initialization:
Then you can push_back() pointers or objects depending on you needs.
Last edited on
Yes , i have pointers look:

1
2
3
4
5
6
7
8
9
10
11
12
13
//Declaration
vector<Neurone*> connectome_
// Nowthe values
Neurone *n1=new Neurone(1,1,0.5);
Neurone *n2=new Neurone(1,1,1.5);


Cerveau
{
connectome_.push_back(n1);
connectome_.push_back(n2);
cout<<connectome_[0];
}


Then I have a problem with the first posotion of my vector , there is only garbage there.

I don't know how to assign those values to my vector
Last edited on
closed account (j3Rz8vqX)
http://www.cplusplus.com/doc/tutorial/pointers/
1
2
baz = foo;   // baz equal to foo (1776)
baz = *foo;  // baz equal to value pointed to by foo (25) 


In your case, you're simply trying to obtain the value at the indexed vector.

To obtain the value at your pointing location, use cout<<*connectome_[0];
Last edited on
ok thanks , that was my error . It has been solved
Topic archived. No new replies allowed.