Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Segmentation fault...

Reply
Thread Tools

Segmentation fault...

 
 
deancoo
Guest
Posts: n/a
 
      02-14-2005
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;
}




 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      02-14-2005
"deancoo" <(E-Mail Removed)> wrote in message
news:Ns0Qd.46789$gA4.28064@edtnps89...
> 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.


--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      02-14-2005
deancoo wrote:
>
> 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.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
deancoo
Guest
Posts: n/a
 
      02-14-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> deancoo wrote:
>>
>> 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.
>
> --
> Karl Heinz Buchegger
> (E-Mail Removed)



 
Reply With Quote
 
Dave Townsend
Guest
Posts: n/a
 
      02-16-2005
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" <(E-Mail Removed)> wrote in message
news:_z8Qd.36$%y.22@clgrps12...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > deancoo wrote:
> >>
> >> 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.
> >
> > --
> > Karl Heinz Buchegger
> > (E-Mail Removed)

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SIGSEGV 11* segmentation violation manoj Java 0 06-25-2004 04:47 PM
Modelsim error code 211 : segmentation violation....What to do ??? Oleg VHDL 9 02-27-2004 01:59 PM
Image segmentation in Java gurleyboy Java 1 02-03-2004 06:47 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM
SIGSEGV 11 (*) segmentation violation on AIX 5.1 and Java Frank Java 0 08-05-2003 08:54 PM



Advertisments