Connecting Tech Pros Worldwide Forums | Help | Site Map

stl vector: what is wrong with this code?

luigi
Guest
 
Posts: n/a
#1: Jul 19 '05
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;
}

Guest
 
Posts: n/a
#2: Jul 19 '05

re: stl vector: what is wrong with this code?


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" <lchian@yahoo.com> wrote in message
news:27fb1738.0310072132.6a9d8518@posting.google.c om...[color=blue]
> 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;
> }[/color]


Attila Feher
Guest
 
Posts: n/a
#3: Jul 19 '05

re: stl vector: what is wrong with this code?


luigi wrote:[color=blue]
> The following code causes crash, can someone tell me what the problem
> is?[/color]
[SNIP][color=blue]
> #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;
> }[/color]

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::operator =( 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


Howard
Guest
 
Posts: n/a
#4: Jul 19 '05

re: stl vector: what is wrong with this code?



<kfallis@kc.rr.com> wrote in message
news:s%Ngb.16140$%C5.4635@twister.rdc-kc.rr.com...[color=blue]
> 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[/color]
saying[color=blue]
> 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[/color]
of[color=blue]
> the contents of the memory location
>
> Hope this helps....
>
> Kevin
>[/color]

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







luigi
Guest
 
Posts: n/a
#5: Jul 19 '05

re: stl vector: what is wrong with this code?


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.
Ron Natalie
Guest
 
Posts: n/a
#6: Jul 19 '05

re: stl vector: what is wrong with this code?



"luigi" <lchian@yahoo.com> wrote in message news:27fb1738.0310081455.79f9e906@posting.google.c om...[color=blue]
> 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.[/color]

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));



Closed Thread