Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stl vector: what is wrong with this code?

Reply
Thread Tools

stl vector: what is wrong with this code?

 
 
luigi
Guest
Posts: n/a
 
      10-08-2003
The following code causes crash, can someone tell me what the problem is?

#include <iostream>
#include <fstream>

#include <vector>

#include "TClass.h"

using namespace std;

int main(int argc,char **argv)
{
TClass::npts = 2;

vector<TClass> vec;

vec.resize(10);
int len=vec.size();

for(int i=0;i<len;i++)
{
for(int j=0;j<TClass::npts;j++)
{
vec[i].a[j]=j;
vec[i].b[j]=j+1;
cerr <<i<<" "<< vec[i].a[j]<< " " <<vec[i].b[j]<< endl;
}
}

return 0;
}

// TClass.h

class TClass
{
public:

static int npts;
int *a;
int *b;

public:

TClass();
~TClass();

};

// TClass.cpp

#include "TClass.h"

int TClass::npts;

TClass::TClass()
{
a=new int[npts];
b=new int[npts];
}

TClass::~TClass()
{
if(a) delete [] a;
if(b) delete [] b;
}
 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      10-08-2003
luigi,

try dereferencing TClass::a and TClass::b in the vectors when you set the
member variables

int* a;
a = some memory location
*a = (dereferencing) i.e. the contents of that memory location

vec[i].a[j]=j or in basic form vec[i].a[j]= 0 (first time through) is saying
that a = 0 and when you try and delete memory you dont own...KABOOM
it would be better to say *(vec[i].a[j]) = 0 or j giving you modification of
the contents of the memory location

Hope this helps....

Kevin


"luigi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> The following code causes crash, can someone tell me what the problem is?
>
> #include <iostream>
> #include <fstream>
>
> #include <vector>
>
> #include "TClass.h"
>
> using namespace std;
>
> int main(int argc,char **argv)
> {
> TClass::npts = 2;
>
> vector<TClass> vec;
>
> vec.resize(10);
> int len=vec.size();
>
> for(int i=0;i<len;i++)
> {
> for(int j=0;j<TClass::npts;j++)
> {
> vec[i].a[j]=j;
> vec[i].b[j]=j+1;
> cerr <<i<<" "<< vec[i].a[j]<< " " <<vec[i].b[j]<< endl;
> }
> }
>
> return 0;
> }
>
> // TClass.h
>
> class TClass
> {
> public:
>
> static int npts;
> int *a;
> int *b;
>
> public:
>
> TClass();
> ~TClass();
>
> };
>
> // TClass.cpp
>
> #include "TClass.h"
>
> int TClass::npts;
>
> TClass::TClass()
> {
> a=new int[npts];
> b=new int[npts];
> }
>
> TClass::~TClass()
> {
> if(a) delete [] a;
> if(b) delete [] b;
> }



 
Reply With Quote
 
 
 
 
Attila Feher
Guest
Posts: n/a
 
      10-08-2003
luigi wrote:
> The following code causes crash, can someone tell me what the problem
> is?

[SNIP]
> #include "TClass.h"
>
> int TClass::npts;
>
> TClass::TClass()
> {
> a=new int[npts];
> b=new int[npts];
> }
>
> TClass::~TClass()
> {
> if(a) delete [] a;
> if(b) delete [] b;
> }


You will need to define a copy constructor and an assignment operator for
TClass. Look up The Rule of Three.

WARNING! Untested example code!
WARNING! Code is not exception safe!

// TClass.h

class TClass {
private:
// Make these private

static int npts;

// You will need to know the
// size used here to be able
// to copy
int myNpts;
int *a;
int *b;

public:

void setNpts( int newNpts) {
npts = newNpts;
}
TClass();
TClass(TClass const &);
TClass const &operator=(TClass const &);
~TClass();

};


// TClass.cpp

#include "TClass.h"

int TClass::npts;

TClass::TClass() {
myNpts = npts;
a=new int[npts];
b=new int[npts];
}

TClass::TClass( TClass const &o) {
myNpts = npts;
a=new int[o.myNpts];
b=new int[o.myNpts];
// Copy them using a loop
// or memcpy or std::copy
}

TClass const &
TClass:perator =( TClass const &o) {
// Don't try to copy self
if (this == &o) return;

if (myNpts != o.myNpts) {
delete delete [] a;
delete delete [] b;
a=new int[o.myNpts];
b=new int[o.myNpts];
}
// Copy them using a loop
// or memcpy or std::copy
}

TClass::~TClass() {
// if(a) delete [] a;
// No need for the if,
// you can delete a 0
// pointer
delete [] a;
delete [] b;
}

// Do not forget to define npts
int TClass::npts = 2;
// 2 is just an arbitrary default


--
Attila aka WW


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      10-08-2003

<(E-Mail Removed)> wrote in message
news:s%Ngb.16140$%(E-Mail Removed)-kc.rr.com...
> luigi,
>
> try dereferencing TClass::a and TClass::b in the vectors when you set the
> member variables
>
> int* a;
> a = some memory location
> *a = (dereferencing) i.e. the contents of that memory location
>
> vec[i].a[j]=j or in basic form vec[i].a[j]= 0 (first time through) is

saying
> that a = 0 and when you try and delete memory you dont own...KABOOM
> it would be better to say *(vec[i].a[j]) = 0 or j giving you modification

of
> the contents of the memory location
>
> Hope this helps....
>
> Kevin
>


Kevin,
[Please post your answers after the text you're referring to (or
interspersed with it at least).]

Your answer is not correct. The OP allocated two arrays of int, not two
arrays of pointers. Your method will try to dereference the int at
vec[i].a[j], but that's not allowed, since they're not pointers. The line
"a = new int[npts]" allocates an array of ints of size npts, and the
notation a[j] refers to the int at index j in that array. Also, setting
vec.[i].a[j] = 0 will set a[j] to 0, not a. The pointer 'a 'points to
memory somewhere else, where the int's are actually stored. The int's are
*not* stored at the address of 'a', but at the address 'a' *points to*.

The OP's problem has to do with a lack of a proper copy-constructor, I
believe. (See Attila's response.)

-Howard







 
Reply With Quote
 
luigi
Guest
Posts: n/a
 
      10-08-2003
Thanks! The problem is fixed with the copy constructor. I will take
the rules more seriously from now on :->).

The resize(10) command calls default constructor once, then the copy
constructor 10 times, then destructor once. Very strange.
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      10-08-2003

"luigi" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) om...
> Thanks! The problem is fixed with the copy constructor. I will take
> the rules more seriously from now on :->).
>
> The resize(10) command calls default constructor once, then the copy
> constructor 10 times, then destructor once. Very strange.


Not strange at all. The declaration of resize is:

resize(size_type sz, T c = T());

The second argument is an element that is used to initialize (by copy
construction in your observations) the newly created elements in the
array. Vector does not require that the contained type be default
constructable. In that case, you must provide a prototype object
for it to copy over these elements.

class C {
public:
int C(int); // no default constructor
};

vector<C> v;
v.resize(10, C(1));



 
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
Segmentation Fault on stl::resize / stl::clear Steve C++ 2 11-06-2007 06:53 AM
stl questions: how can I compare 2 stl list? silverburgh.meryl@gmail.com C++ 5 04-16-2006 09:57 PM
a stl map which use stl pair as the key Allerdyce.John@gmail.com C++ 2 02-22-2006 07:25 AM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
To STL or not to STL Allan Bruce C++ 41 10-17-2003 08:21 PM



Advertisments