[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Re: Segmentation fault

Rolf Magnus

10/3/2008 12:33:00 PM

Ioannis Vranos wrote:

> The following code sometimes runs OK in my system, sometimes it produces
> a segmentation fault (access to inaccessible memory). I filled it as a
> bug of GCC, but am posting it here in case I did something wrong.

I think you did.

> SomeClass::SomeClass(const SomeClass &):vec(VectorSize)
> {
> using namespace std;
>
> for(TypeVector::size_type i= 0; i< vec.size(); ++i)
> vec[i]= rand();
>
> sort(vec.begin(), vec.end());
> }

Here, the object changes the value used for operator< when copied, which is
likely to lead to problems with sorting your container of SomeClass
objects.

2 Answers

Zeppe

10/3/2008 3:09:00 PM

0

Rolf Magnus wrote:
> Ioannis Vranos wrote:
>
>> The following code sometimes runs OK in my system, sometimes it produces
>> a segmentation fault (access to inaccessible memory). I filled it as a
>> bug of GCC, but am posting it here in case I did something wrong.
>
> I think you did.

Of course he did. If I can suggest a good metric to the OP, when your
program crashes, well, *usually* it's the programmer's fault, not the
compiler's ;)

>
>> SomeClass::SomeClass(const SomeClass &):vec(VectorSize)
>> {
>> using namespace std;
>>
>> for(TypeVector::size_type i= 0; i< vec.size(); ++i)
>> vec[i]= rand();
>>
>> sort(vec.begin(), vec.end());
>> }
>
> Here, the object changes the value used for operator< when copied, which is
> likely to lead to problems with sorting your container of SomeClass
> objects.

More in general, for all the containers and all the iterators defined
over a type T, the type T has to be (among other things)
CopyConstructible, that is, given an object t1 of type T,

T t(t1);

means that t and t1 are equivalent (t == t1). If your copy constructor
doesn't satisfy this rule, the algorithms (containers, pointers, etc)
behaviour is undefined.

Best wishes,

Zeppe



James Kanze

10/4/2008 7:55:00 AM

0

On Oct 3, 5:08 pm, Zeppe <ze...@remove.all.this.long.comment.yahoo.it>
wrote:
> Rolf Magnus wrote:
> > Ioannis Vranos wrote:
>
> >> The following code sometimes runs OK in my system, sometimes it produces
> >> a segmentation fault (access to inaccessible memory). I filled it as a
> >> bug of GCC, but am posting it here in case I did something wrong.
>
> > I think you did.
>
> Of course he did. If I can suggest a good metric to the OP, when your
> program crashes, well, *usually* it's the programmer's fault, not the
> compiler's ;)
>
>
>
> >> SomeClass::SomeClass(const SomeClass &):vec(VectorSize)
> >> {
> >> using namespace std;
>
> >> for(TypeVector::size_type i= 0; i< vec.size(); ++i)
> >> vec[i]= rand();
>
> >> sort(vec.begin(), vec.end());
> >> }
>
> > Here, the object changes the value used for operator< when copied, which is
> > likely to lead to problems with sorting your container of SomeClass
> > objects.
>
> More in general, for all the containers and all the iterators defined
> over a type T, the type T has to be (among other things)
> CopyConstructible, that is, given an object t1 of type T,
>
> T t(t1);
>
> means that t and t1 are equivalent (t == t1). If your copy constructor
> doesn't satisfy this rule, the algorithms (containers, pointers, etc)
> behaviour is undefined.
>
> Best wishes,
>
> Zeppe