473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

std::vector question

I havea program which on execution gives unpredictable behaviour (it
shouldn't). In trying to track down the problem, I'm wondering if there is a
difference between these two ways of filling a std::vector with data:

Method 1:

std::vector<int> v;
int k;

for(i=0;i<n;i++){
k = i + 3;
v.push_back(k);
}

Method 2:

std::vector<int> v;

for(i=0;i<n;i++){
int k = i + 3;
v.push_back(k);
}
Jul 22 '05 #1
8 1507
"Hamish" <h.****@xtra.co.nz> wrote...
I havea program which on execution gives unpredictable behaviour (it
shouldn't). In trying to track down the problem, I'm wondering if there is
a
difference between these two ways of filling a std::vector with data:

Method 1:

std::vector<int> v;
int k;

for(i=0;i<n;i++){
k = i + 3;
v.push_back(k);
}

Method 2:

std::vector<int> v;

for(i=0;i<n;i++){
int k = i + 3;
v.push_back(k);
}


No, in this particular case there is no difference. However, something
tells me that your real code, the code that gives you trouble, is a bit
more complicated than that.

It is often important to follow certain rules in your programming. For
example, the famous "Rule of Three" (look it up). If your class has
some kind of dynamic memory management, you simply _must_ follow it.

V
Jul 22 '05 #2

"Hamish" <h.****@xtra.co.nz> wrote in message
news:oD*******************@news.xtra.co.nz...
I havea program which on execution gives unpredictable behaviour (it
shouldn't). In trying to track down the problem, I'm wondering if there is a difference between these two ways of filling a std::vector with data:

Method 1:

std::vector<int> v;
int k;

for(i=0;i<n;i++){
k = i + 3;
v.push_back(k);
}

Method 2:

std::vector<int> v;

for(i=0;i<n;i++){
int k = i + 3;
v.push_back(k);
}


The only difference with these two methods is the scope of k but this
doesn´t affect the behavior in this case. Please post your real code which
gives you trouble.

Cheers
Chris
Jul 22 '05 #3
KPB
Victor Bazarov wrote:
It is often important to follow certain rules in your programming. For
example, the famous "Rule of Three" (look it up).


I'm actually interested in what this "rule of three" says but I'm
getting lots of irrelevant hits on google. It seems that everybody has
some "rule of three".

Any hints?

Thanks,
KPB


Jul 22 '05 #4
KPB wrote:
Victor Bazarov wrote:
It is often important to follow certain rules in your programming.
For example, the famous "Rule of Three" (look it up).


I'm actually interested in what this "rule of three" says but I'm
getting lots of irrelevant hits on google. It seems that everybody has
some "rule of three".

Any hints?


Googling 101: What language are you discussing? Add that to your search
string.

Jeff
Jul 22 '05 #5
KPB
Jeff Flinn wrote:
Googling 101: What language are you discussing? Add that to your search
string.


Please don't treat me like I'm an idiot. I'm not.

Thanks,
KPB
Jul 22 '05 #6
KPB
KPB wrote:
Victor Bazarov wrote:
It is often important to follow certain rules in your programming. For
example, the famous "Rule of Three" (look it up).

I'm actually interested in what this "rule of three" says but I'm
getting lots of irrelevant hits on google. It seems that everybody has
some "rule of three".

Any hints?

Thanks,
KPB


Nevermind Victor. I found them. I alredy know them. I don't think Scott
Meyers refered to this as the "rule of three" but he did stress this
nonetheless in his Effective C++ books.

KPB
Jul 22 '05 #7
> > I havea program which on execution gives unpredictable behaviour (it
shouldn't). In trying to track down the problem, I'm wondering if there
is a
difference between these two ways of filling a std::vector with data:

Method 1:

std::vector<int> v;
int k;

for(i=0;i<n;i++){
k = i + 3;
v.push_back(k);
}

Method 2:

std::vector<int> v;

for(i=0;i<n;i++){
int k = i + 3;
v.push_back(k);
}


The only difference with these two methods is the scope of k but this
doesn´t affect the behavior in this case. Please post your real code which
gives you trouble.


I haven't been able to track down the problem yet. However, the only
difference between an older version which worked, adn this version is the
following data structures:

std::vector<PolyTypeClass*> PolyTypes;

class PolyTypeClass{
public:
PolyTypeClass();
virtual ~PolyTypeClass();

const PolyTypeClass& operator= (const PolyTypeClass& poly);
void Copy(PolyTypeClass * pCopy);

int ID;
std::vector<BasePolygonClass> Rot;
};

class BasePolygonClass{
public:
BasePolygonClass();
BasePolygonClass(int Size);
virtual ~BasePolygonClass();

const BasePolygonClass& operator= (const BasePolygonClass& poly);
void Copy(BasePolygonClass * pCopy);

std::vector<PointPropClass> Points;
double Area;
double Length;
double Height;
BOOL IsConvex;
double Angle;
BOOL XFlip;
BOOL YFlip;
};

class PointPropClass
{
public:
PointPropClass();
virtual ~PointPropClass();
const PointPropClass& operator= (const PointPropClass& poly);
void Copy(PointPropClass * pCopy);

BOOL TP;
double Angle;
int Num;
int CavNum;
int St;
int Fin;
int Type;
BOOL Neg;
BOOL IsGhosh;
double x;
double y;
};
Jul 22 '05 #8

"Hamish" <h.****@xtra.co.nz> wrote in message
news:PU*******************@news.xtra.co.nz...
I havea program which on execution gives unpredictable behaviour (it
shouldn't). In trying to track down the problem, I'm wondering if
there
is
a
difference between these two ways of filling a std::vector with data:

Method 1:

std::vector<int> v;
int k;

for(i=0;i<n;i++){
k = i + 3;
v.push_back(k);
}

Method 2:

std::vector<int> v;

for(i=0;i<n;i++){
int k = i + 3;
v.push_back(k);
}


The only difference with these two methods is the scope of k but this
doesn´t affect the behavior in this case. Please post your real code

which gives you trouble.


I haven't been able to track down the problem yet. However, the only
difference between an older version which worked, adn this version is the
following data structures:

std::vector<PolyTypeClass*> PolyTypes;

class PolyTypeClass{
public:
PolyTypeClass();
virtual ~PolyTypeClass();

const PolyTypeClass& operator= (const PolyTypeClass& poly);
void Copy(PolyTypeClass * pCopy);

int ID;
std::vector<BasePolygonClass> Rot;
};

class BasePolygonClass{
public:
BasePolygonClass();
BasePolygonClass(int Size);
virtual ~BasePolygonClass();

const BasePolygonClass& operator= (const BasePolygonClass& poly);
void Copy(BasePolygonClass * pCopy);

std::vector<PointPropClass> Points;
double Area;
double Length;
double Height;
BOOL IsConvex;
double Angle;
BOOL XFlip;
BOOL YFlip;
};

class PointPropClass
{
public:
PointPropClass();
virtual ~PointPropClass();
const PointPropClass& operator= (const PointPropClass& poly);
void Copy(PointPropClass * pCopy);

BOOL TP;
double Angle;
int Num;
int CavNum;
int St;
int Fin;
int Type;
BOOL Neg;
BOOL IsGhosh;
double x;
double y;
};


What strikes me (although in this case it should not pose a problem after a
quick glance at your code) is that you do supply a dtor and an assignment op
but not a copy ctor. If you really need a dtor & an assignment op it´s good
practice to supply a copy ctor too.

If you could please post the actual code of "Method 1" & "Method 2" which
works with your data structures and also the implementation of the Copy()
method would be interesting.

Cheers
Chris
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: bartek d | last post by:
Hello, I have a class which is used to encapsulate a RenderMan Interface variable. Generally speaking, such variable may be of integral, float, string type, or an array of those. I thought I...
5
by: bartek d | last post by:
Hello, Regarding my previous question about a class which is used to store a variable type vector. I tried to be more elaborate on the code. I'd be grateful for your suggestions. Am I going in...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
0
by: Jason Heyes | last post by:
I wrote a previous post that asked whether there was a reference-counted implementation of std::vector. Apparantly there wasn't. So my next question is, is it possible to write your own shared...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
2
by: zl2k | last post by:
hi, all I need to use gsl_vector pointer with std::vector but not sure how to free the memory when I don't need it. Here is a piece of the code. =================== std::vector<gsl_vector *...
6
by: jmsanchezdiaz | last post by:
CPP question: if i had a struct like "struct str { int a; int b };" and a vector "std::vector < str test;" and wanted to push_back a struct, would i have to define the struct, fill it, and then...
8
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
3
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.