Segmentation Issue RW
Dec 12, 2011 at 3:45pm UTC
..
Last edited on Dec 16, 2011 at 10:04am UTC
Dec 12, 2011 at 4:15pm UTC
On your first lines of main, you are creating a vector of pointers to Atom objects, but you're not creating those objects:
vector<vector<Atom *> > position(duration, vector<Atom *>(particles));
So, inside the for loop, you get segfault the first time you do
position[0][i]->_x = count;
Dec 12, 2011 at 4:17pm UTC
[co
de]
"Please use code tags"
[/co
de]
1 2 3 4 5
//vector<vector<Atom *> > position(duration,vector<Atom *>(particles));
vector<vector<Atom> > position(duration,vector<Atom>(particles));
//int* Atom = new int[particles];
vector<int > Atom(particles);
Dec 12, 2011 at 6:58pm UTC
Yes you are right bbgst, I need to create objects. But i dont know how to do, please help me in this issue. I need this one
Dec 12, 2011 at 7:41pm UTC
You could either use ne555's solution and use local objects or use
Edit : this is wrong, sorry.
vector<vector<Atom *>> position(duration, vector<Atom>(particles, new Atom));
Last edited on Dec 13, 2011 at 12:06am UTC
Dec 12, 2011 at 8:32pm UTC
That is no good. vector<Atom>(particles, new Atom)
is using the copy constructor, so all the pointers point to the same address.
Dec 13, 2011 at 12:06am UTC
Oops, that's what you get when you quick-reply.
Dec 13, 2011 at 1:51pm UTC
So what can i do now, my homework needs to use vectors and dynamic memory management (new and delete operators) and i dont know how to proceed with, please help me in this issue
Dec 13, 2011 at 4:12pm UTC
First of all, code tags and indentation.
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
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <vector>
using namespace std;
int DefineDuration()
{
int durations;
cout<<"Enter the Duration:" ;
cin>>durations;
return durations;
}
int DefineParticles()
{
int particle;
cout<<"Enter the Particles:" ;
cin>>particle;
return particle;
}
class Atom
{
public :
int _x;
int _y;
};
int main()
{
int duration = DefineDuration();
int particles= DefineParticles();
vector<vector<Atom *> > position(duration,vector<Atom *>(particles));
int * Atom = new int [particles];
int count=1;
for (int i=0; i<particles; i++)
{
position[0][i]->_x=count;
position[0][i]->_y=count;
count++;
}
srand(time(0));
int displacement=1;
for (int t=1; t<duration; t++)
{
for (int j=0; j<particles; j++)
{
switch (rand()%4)
{
case 0:
position[t][j]->_x = position[t-1][j]->_x + displacement;
position[t][j]->_y = position[t-1][j+1]->_y + displacement;
break ;
case 1:
position[t][j]->_x = position[t-1][j]->_x - displacement;
position[t][j]->_y = position[t-1][j+1]->_y - displacement;
break ;
case 2:
position[t][j]->_x = position[t-1][j]->_x + displacement;
position[t][j]->_y = position[t-1][j+1]->_y + displacement;
break ;
case 3:
position[t][j]->_x = position[t-1][j]->_x - displacement;
position[t][j]->_y = position[t-1][j+1]->_y - displacement;
break ;
}
}
}
ofstream myfile;
myfile.open ("random.txt" );
for (int t=0; t<duration; t++)
{
for (unsigned j=0; j<position[t].size(); ++j)
myfile << position[t][j]->_x << "," << position[t][j]->_y << " " ;
myfile << endl;
}
myfile.close();
delete [] Atom;
return 0;
}
Dec 13, 2011 at 4:36pm UTC
..
Last edited on Dec 16, 2011 at 10:04am UTC
Dec 13, 2011 at 4:39pm UTC
Line 34
vector<vector<Atom *> > position(duration,vector<Atom *>(particles, (Atom *) 0));
Line 36
int * Atom = new int [particles]; // why is this necessary?
Line 38
1 2 3
for (int t=0; t<duration; t++)
for (int j=0; j<particles; j++)
position[t][j] = new Atom;
Line 86
1 2 3 4 5 6
myfile.close();
delete [] Atom;
for (int t=0; t<duration; t++)
for (int j=0; j<particles; j++)
delete position[t][j];
But that won't work because:
1) you created a
int * Atom
, which obscures the class declared outside.
2) on your loop, you are accessing the [j + 1] element on all elements, including the last, which results in a index outside your vector (lines 56, 61, 66, 71).
And more importantly, if you don't know how to do it, you should learn, not ask for the code. Showing you the code doesn't help.
Dec 13, 2011 at 4:42pm UTC
What I mean is if you surround your code with code tags ([ code][/code] or the first button on the left when you are posting), it keeps the formatting, making it A LOT easier to read.
Topic archived. No new replies allowed.