A very weird bug......

X

xz

I have defined a class Floor with its constructor as follows:

class Floor{

private:
//...
public:
Floor(double x0, double y0, double xM, double yM, double r = 1.0, int
meshOrder = 1);
//...
};

I have tried both of the following:

Floor floor(-5.0, -5.0, 10.0, 10.0, 1.0);
Floor floor(-5.0, -5.0, 10.0, 10.0);

In both cases, after floor is constructed, in the very first member
function of Floor that is called, r has changed to r= 2.37161e-310 and
meshOrder has changed to 0.

I really cannot understand what happens after the construction of
floor and before the first member function of Floor is called.
 
R

red floyd

xz said:
I have defined a class Floor with its constructor as follows:

class Floor{

private:
//...
public:
Floor(double x0, double y0, double xM, double yM, double r = 1.0, int
meshOrder = 1);
//...
};

I have tried both of the following:

Floor floor(-5.0, -5.0, 10.0, 10.0, 1.0);
Floor floor(-5.0, -5.0, 10.0, 10.0);

In both cases, after floor is constructed, in the very first member
function of Floor that is called, r has changed to r= 2.37161e-310 and
meshOrder has changed to 0.

I really cannot understand what happens after the construction of
floor and before the first member function of Floor is called.

You have an error on line 42.

We're not psychic. Post the constructor code, not just the prototype.
 
X

xz

Floor.h:

class Floor{
public :

private:
int m; //# of rows of vertices
int n; //# of columns of vertices
double r;
int meshOrder;
std::vector< std::vector<Vertex*> > floorVertices;

public:
Floor(double x0, double y0, double xM, double yM, double r = 1.0, int
meshOrder = 1);

//...

void describe();
};

in Floor.cpp:

Constructor:

Floor::Floor(double x0, double y0, double xM, double yM, double r, int
meshOrder) {
m = int ((yM - y0)/r + 1);
n = int ((xM - x0)/r + 1);

floorVertices = *(new vector< vector<Vertex*> >(m, vector<Vertex*>
(n) ) );

floorVertices[0][0] = new Vertex(x0, y0);

for(int j = 1; j < n; j++) {
floorVertices[0][j] = new Vertex(j+x0, y0);
floorVertices[0][j]->addAdjacency(floorVertices[0][j-1]);
}

for(int i = 1; i < m; i++) {
floorVertices[0] = new Vertex(x0, i+y0);
floorVertices[0]->addAdjacency(floorVertices[i-1][0]);
}

for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
floorVertices[j] = new Vertex(j+x0, i+y0);
floorVertices[j]->addAdjacency(floorVertices[i-1][j]);
floorVertices[j]->addAdjacency(floorVertices[j-1]);
}
}
}

Floor has a self-describe function as follows:

void Floor::describe() {
cout << "The floor is represented by the mesh as follows: \n"
"(r = " << r << ", meshOrder = " << meshOrder << "\n";
for(int i = 0; i < floorVertices.size(); i++) {
for(int j = 0; j < floorVertices.size(); j++) {
floorVertices[j]->describe();
}
}
}


In a test file I have lines like:

Floor keck(-5.0, -5.0, 10.0, 10.0, 1.0);
//Floor keck(-5.0, -5.0, 10.0, 10.0);
keck.describe();


In the information printed by keck.describe(), I got:

(r = 2.33637e-310, meshOrder = 0)


With DDD I checked the value of "r" inside the constructor, it is *1*
all the
way to the end of the constructor.

I have also tried changing r to public (for debugging), and printed it
out right after the constructor, I found it being 2.36305e-310 there.
Seems like the value is changed at the end of the constructor.
 
R

Richard Herring

In message
xz said:
Floor.h:

class Floor{
public :

private:
int m; //# of rows of vertices
int n; //# of columns of vertices
double r;

That's a member variable called "r". I'd recommend choosing a naming
convention that makes it clear when something is a member.
int meshOrder;
std::vector< std::vector<Vertex*> > floorVertices;

public:
Floor(double x0, double y0, double xM, double yM, double r = 1.0, int
meshOrder = 1);

//...

void describe();

Probably ought to be const.
};

in Floor.cpp:

Constructor:

Floor::Floor(double x0, double y0, double xM, double yM, double r,

That's a parameter called "r", local to the implementation of the
constructor. Although it happens to have the same name, it is completely
unrelated to the member variable called "r". The compiler cannot read
your mind.
int
meshOrder) {
m = int ((yM - y0)/r + 1);
n = int ((xM - x0)/r + 1);

You probably need to learn about initialization lists.
floorVertices = *(new vector< vector<Vertex*> >(m, vector<Vertex*>
(n) ) );

floorVertices[0][0] = new Vertex(x0, y0);

for(int j = 1; j < n; j++) {
floorVertices[0][j] = new Vertex(j+x0, y0);
floorVertices[0][j]->addAdjacency(floorVertices[0][j-1]);
}

for(int i = 1; i < m; i++) {
floorVertices[0] = new Vertex(x0, i+y0);
floorVertices[0]->addAdjacency(floorVertices[i-1][0]);
}

for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
floorVertices[j] = new Vertex(j+x0, i+y0);
floorVertices[j]->addAdjacency(floorVertices[i-1][j]);
floorVertices[j]->addAdjacency(floorVertices[j-1]);
}
}
}


None of the above initializes the member variable "r", or assigns
anything to it. Its value is therefore undefined.
Floor has a self-describe function as follows:

void Floor::describe() {
cout << "The floor is represented by the mesh as follows: \n"
"(r = " << r << ", meshOrder = " << meshOrder << "\n";

That reports the member variable r, which has never been initialized.
for(int i = 0; i < floorVertices.size(); i++) {
for(int j = 0; j < floorVertices.size(); j++) {
floorVertices[j]->describe();
}
}
}


In a test file I have lines like:

Floor keck(-5.0, -5.0, 10.0, 10.0, 1.0);
//Floor keck(-5.0, -5.0, 10.0, 10.0);
keck.describe();


In the information printed by keck.describe(), I got:

(r = 2.33637e-310, meshOrder = 0)


With DDD I checked the value of "r" inside the constructor, it is *1*
all the
way to the end of the constructor.


That's the constructor parameter "r". It's not the member variable "r".
I have also tried changing r to public (for debugging), and printed it
out right after the constructor, I found it being 2.36305e-310 there.
Seems like the value is changed at the end of the constructor.

No, the value of the member variable is never set.

And the same applies equally to the two variables called "meshOrder".
 
L

Lionel B

On Thu, 13 Mar 2008 20:47:39 -0700, xz wrote:

Nothing weird about it...
class Floor{
public :

private:
int m; //# of rows of vertices
int n; //# of columns of vertices
double r;
int meshOrder;
std::vector< std::vector<Vertex*> > floorVertices;

public:
Floor(double x0, double y0, double xM, double yM, double r = 1.0, int
meshOrder = 1);

//...

void describe();
};

Floor::Floor(double x0, double y0, double xM, double yM, double r, int
meshOrder) {
m = int ((yM - y0)/r + 1);
n = int ((xM - x0)/r + 1);

Note that the *parameters* r and meshOrder to the constructor are *not*
the same as the *members* r and meshOrder, which you are not setting
anywhere - hence the members r and meshOrder remain uninitialised, which
explains your results.

It is generally considered good practice in C++ to initialise variables
in an initialiser list, so I would do this as follows (this is a stripped
down, compilable version of your code):

#include <iostream>

class Floor{
private:
double r;
int meshOrder;
int m; //# of rows of vertices
int n; //# of columns of vertices

// note: m,n have to be initialised after r and members are
// initialised in order of declaration.

public:
Floor(double x0, double y0, double xM, double yM, double r_ = 1.0, int meshOrder_ = 1);
// Changed parameter names to r_, meshOrder_ to distinguish them
// from member variables

void describe();
};

Floor::Floor(double x0, double y0, double xM, double yM, double r_, int meshOrder_) :
r(r_), meshOrder(meshOrder_), m(int((yM - y0)/r + 1)), n(int((xM - x0)/r + 1))
{
}

void Floor::describe()
{
std::cout << "The floor is represented by the mesh as follows: \n(r = " << r << ", meshOrder = " << meshOrder << ")\n";
}

int main()
{
Floor keck(-5.0, -5.0, 10.0, 10.0, 1.0);
keck.describe();
}

Output:

The floor is represented by the mesh as follows:
(r = 1, meshOrder = 1)
 
J

Jim Langston

Richard said:
In message
Floor.h:

class Floor{
public :

private:
int m; //# of rows of vertices
int n; //# of columns of vertices
double r;

That's a member variable called "r". I'd recommend choosing a naming
convention that makes it clear when something is a member.
int meshOrder;
std::vector< std::vector<Vertex*> > floorVertices;

public:
Floor(double x0, double y0, double xM, double yM, double r = 1.0, int
meshOrder = 1);

//...

void describe();

Probably ought to be const.
};

in Floor.cpp:

Constructor:

Floor::Floor(double x0, double y0, double xM, double yM, double r,

That's a parameter called "r", local to the implementation of the
constructor. Although it happens to have the same name, it is
completely unrelated to the member variable called "r". The compiler
cannot read your mind.
int
meshOrder) {
m = int ((yM - y0)/r + 1);
n = int ((xM - x0)/r + 1);

You probably need to learn about initialization lists.
floorVertices = *(new vector< vector<Vertex*> >(m, vector<Vertex*>
(n) ) );

floorVertices[0][0] = new Vertex(x0, y0);

for(int j = 1; j < n; j++) {
floorVertices[0][j] = new Vertex(j+x0, y0);
floorVertices[0][j]->addAdjacency(floorVertices[0][j-1]);
}

for(int i = 1; i < m; i++) {
floorVertices[0] = new Vertex(x0, i+y0);
floorVertices[0]->addAdjacency(floorVertices[i-1][0]);
}

for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
floorVertices[j] = new Vertex(j+x0, i+y0);
floorVertices[j]->addAdjacency(floorVertices[i-1][j]);
floorVertices[j]->addAdjacency(floorVertices[j-1]);
}
}
}


None of the above initializes the member variable "r", or assigns
anything to it. Its value is therefore undefined.
Floor has a self-describe function as follows:

void Floor::describe() {
cout << "The floor is represented by the mesh as follows: \n"
"(r = " << r << ", meshOrder = " << meshOrder << "\n";

That reports the member variable r, which has never been initialized.
for(int i = 0; i < floorVertices.size(); i++) {
for(int j = 0; j < floorVertices.size(); j++) {
floorVertices[j]->describe();
}
}
}


In a test file I have lines like:

Floor keck(-5.0, -5.0, 10.0, 10.0, 1.0);
//Floor keck(-5.0, -5.0, 10.0, 10.0);
keck.describe();


In the information printed by keck.describe(), I got:

(r = 2.33637e-310, meshOrder = 0)


With DDD I checked the value of "r" inside the constructor, it is *1*
all the
way to the end of the constructor.


That's the constructor parameter "r". It's not the member variable
"r".
I have also tried changing r to public (for debugging), and printed
it out right after the constructor, I found it being 2.36305e-310
there. Seems like the value is changed at the end of the constructor.

No, the value of the member variable is never set.

And the same applies equally to the two variables called "meshOrder".


Also, one way to fix it is with what is called an "initialization list" in
the constructor.

Floor(double x0, double y0, double xM, double yM, double r = 1.0,
int meshOrder = 1): r(r), meshOrder(meshOrder)

The initializaiton list is one place where you can use variables of the same
name and the compiler will know which one you are talking about. In this
case, r(r) means to initialize the class variable r with the parameter r,
meshOrder(meshOrder) means to initialize the class variable meshOrder with
the parameter meshOrder.

This is one reason I personally distinuish my local class variable names
from parameters. I personally use an underscore at the end of the variable,
some people use capital letters or all small, etc.. So my varialbes would
be:

private:
int m_; //# of rows of vertices
int n_; //# of columns of vertices
double r_;
int meshOrder_;
std::vector< std::vector<Vertex*> > floorVertices_;

and my constructor would look like:

Floor(double x0, double y0, double xM, double yM, double r = 1.0,
int meshOrder = 1): r_(r), meshOrder_(meshOrder)
 
A

Andy Champ

Snipping out some interesting lines that others haven't mentioned (yet)
xz said:
class Floor{
public :

private:
int m; //# of rows of vertices
int n; //# of columns of vertices
double r;
int meshOrder;
std::vector< std::vector<Vertex*> > floorVertices;

This is a vector of vectors of pointers to Vertex objects

floorVertices = *(new vector< vector<Vertex*> >(m, vector<Vertex*>(n) ) );

.... and this builds a vector of n pointers, then copies it m times into
a new heap object, then copies the contents of the heap object into your
data object. I think. Then leaks the original.

Perhaps you ought to resize() the vectors, rather than do that weird
assignment. I'm also nervous about you storing all those pointers, this
is a recipe for leaks.

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top