Segmentation fault...

D

deancoo

Ok, I've got another one for ya. The code I've pasted below compiles fine
but produces a seg fault. I've put a comment in where the seg fault gets
tripped. Can someone please explain what I've done wrong. Thanks again for
any help.

d

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

class hand {
public:
vector<int> my_ints;
};

class hand_vector {
public:
vector<hand> myhands;
};

int main(int argc, char *argv[])
{

system("PAUSE");

hand_vector *myvector;
hand v_stub;

for (int i=1; i<=10; i++) {
myvector->myhands.push_back(v_stub); // SEGMENTATION FAULT HERE
};

system("PAUSE");

vector<hand>::iterator j;

for (j=myvector->myhands.begin(); j!=myvector->myhands.end(); j++) {
j->my_ints.push_back(0);
j->my_ints.push_back(0);
j->my_ints.push_back(0);
j->my_ints.push_back(0);
j->my_ints.push_back(0);
};

system("PAUSE");

delete myvector;

system("PAUSE");
return EXIT_SUCCESS;
}
 
I

Ivan Vecerina

deancoo said:
Ok, I've got another one for ya. The code I've pasted below compiles fine
but produces a seg fault. I've put a comment in where the seg fault gets
tripped. Can someone please explain what I've done wrong. Thanks again
for any help. ....
int main(int argc, char *argv[])
{

system("PAUSE");

hand_vector *myvector; ....
for (int i=1; i<=10; i++) {
myvector->myhands.push_back(v_stub); // SEGMENTATION FAULT HERE
}; ....
delete myvector;

Don't use a pointer when you need a local variable !

Define myvector as:
hand_vector myvector;
and use it with '.':
myvector.myhands.push_back(...)
It will automatically be disposed of upon function exit.
 
K

Karl Heinz Buchegger

deancoo said:
int main(int argc, char *argv[])
{

system("PAUSE");

hand_vector *myvector;

myvector is a pointer. It points to ... nowhere in particular
hand v_stub;

for (int i=1; i<=10; i++) {
myvector->myhands.push_back(v_stub); // SEGMENTATION FAULT HERE

So where is the hand_vector object, where myvector is supposed to point to?
It was never allocated. All you have is a pointer that can point to such
an object. But having a pointer and having the object itself are different
things. It is as having a description of a Ferrari compared to having a
Ferrari itself.
 
D

deancoo

Doh! Thanks Karl, that's what happens when you work too late. Further,
after correcting this problem by creating an instance on the heap with
"new", I believe there is a memory leak of some sort. After deleting the
"hand_vector" instance, not all memory is released (only about 30%). It
appears to me as though the instances of "hand" are still hanging around.
Are they not on the heap? Shouldn't they be released along with the
"hand_vector" instance?

d

Karl Heinz Buchegger said:
deancoo said:
int main(int argc, char *argv[])
{

system("PAUSE");

hand_vector *myvector;

myvector is a pointer. It points to ... nowhere in particular
hand v_stub;

for (int i=1; i<=10; i++) {
myvector->myhands.push_back(v_stub); // SEGMENTATION FAULT HERE

So where is the hand_vector object, where myvector is supposed to point
to?
It was never allocated. All you have is a pointer that can point to such
an object. But having a pointer and having the object itself are different
things. It is as having a description of a Ferrari compared to having a
Ferrari itself.
 
D

Dave Townsend

What don't you use a debugger and put a breakpoint in the destructor
of Hand and count the number of calls to the destructor to convince
yourself you don't a leak, at least not ther.e



deancoo said:
Doh! Thanks Karl, that's what happens when you work too late. Further,
after correcting this problem by creating an instance on the heap with
"new", I believe there is a memory leak of some sort. After deleting the
"hand_vector" instance, not all memory is released (only about 30%). It
appears to me as though the instances of "hand" are still hanging around.
Are they not on the heap? Shouldn't they be released along with the
"hand_vector" instance?

d

Karl Heinz Buchegger said:
deancoo said:
int main(int argc, char *argv[])
{

system("PAUSE");

hand_vector *myvector;

myvector is a pointer. It points to ... nowhere in particular
hand v_stub;

for (int i=1; i<=10; i++) {
myvector->myhands.push_back(v_stub); // SEGMENTATION FAULT HERE

So where is the hand_vector object, where myvector is supposed to point
to?
It was never allocated. All you have is a pointer that can point to such
an object. But having a pointer and having the object itself are different
things. It is as having a description of a Ferrari compared to having a
Ferrari itself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top