Problem with double matrix in vc++ Express edition

N

Nangi

Hi, I've got a problem with this code.

I've got a double Matrix A and a vector b, initialized with

DoubleMatr A = new double*[3];
DoubleVect b = new double[3];

where DoubleMatr and DoubleVect are defined as

typedef double* DoubleVect;
typedef double** DoubleMatr;

then i pass the matrix and the vector to a function

void CreateSystemFromPolygon(Polygon *p, double *X, double *Y,
DoubleMatr A, DoubleVect b)
....
for ( int i = 0 ; i < 3 ; i++)
{
for (int j = 0 ; j < 3; j++)
{
A[j] = 0;
}

b = 0.0;
}

A[0][0] = 1.0;
A[1][1] = 1.0;

b[0] = X[0];
b[1] = Y[0];
....

at runtime i watch the values of A[0][0] A[0][1] etc..
I see that the istruction A[0][0] = 1.0 modify A[0][0] but A[1][0] and
A[2][0] too.
Where is the problem?
 
D

David Wilkinson

Nangi said:
Hi, I've got a problem with this code.

I've got a double Matrix A and a vector b, initialized with

DoubleMatr A = new double*[3];
DoubleVect b = new double[3];

where DoubleMatr and DoubleVect are defined as

typedef double* DoubleVect;
typedef double** DoubleMatr;

then i pass the matrix and the vector to a function

void CreateSystemFromPolygon(Polygon *p, double *X, double *Y,
DoubleMatr A, DoubleVect b)
...
for ( int i = 0 ; i < 3 ; i++)
{
for (int j = 0 ; j < 3; j++)
{
A[j] = 0;
}

b = 0.0;
}

A[0][0] = 1.0;
A[1][1] = 1.0;

b[0] = X[0];
b[1] = Y[0];
...

at runtime i watch the values of A[0][0] A[0][1] etc..
I see that the istruction A[0][0] = 1.0 modify A[0][0] but A[1][0] and
A[2][0] too.
Where is the problem?


Nanji:

You have not defined a two-dimensional double array. You have just
defined an array of 3 double pointers. A[0], for example is a pointer to
double, but it is not pointing at anything. You need to do

A[0] = new double[3];
A[1] = new double[3];
A[2] = new double[3];
 
N

Nangi

Nangi said:
Hi, I've got a problem with this code.
I've got a double Matrix A and a vector b, initialized with
   DoubleMatr A = new double*[3];
   DoubleVect b = new double[3];
where DoubleMatr and DoubleVect are defined as
typedef double* DoubleVect;
typedef double** DoubleMatr;
then i pass the matrix and the vector to a function
void CreateSystemFromPolygon(Polygon *p, double *X, double *Y,
DoubleMatr A, DoubleVect b)
...
   for ( int i = 0 ; i < 3 ; i++)
   {
           for (int j = 0 ; j < 3; j++)
           {
                   A[j] = 0;
           }

           b = 0.0;
   }

   A[0][0] = 1.0;
   A[1][1] = 1.0;
   b[0] = X[0];
   b[1] = Y[0];
...
at runtime i watch the values of A[0][0] A[0][1] etc..
I see that the istruction A[0][0] = 1.0 modify A[0][0] but A[1][0] and
A[2][0] too.
Where is the problem?

Nanji:

You have not defined a two-dimensional double array. You have just
defined an array of 3 double pointers. A[0], for example is a pointer to
double, but it is not pointing at anything. You need to do

A[0] = new double[3];
A[1] = new double[3];
A[2] = new double[3];

--
David Wilkinson
Visual C++ MVP- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -


Sorry, I forgot to post this part of code..

for ( int i = 0 ; i < 3 ; i++ ) A = new double[3];
 
N

Nangi

Nangi said:
Hi, I've got a problem with this code.
I've got a double Matrix A and a vector b, initialized with
   DoubleMatr A = new double*[3];
   DoubleVect b = new double[3];
where DoubleMatr and DoubleVect are defined as
typedef double* DoubleVect;
typedef double** DoubleMatr;
then i pass the matrix and the vector to a function
void CreateSystemFromPolygon(Polygon *p, double *X, double *Y,
DoubleMatr A, DoubleVect b)
...
   for ( int i = 0 ; i < 3 ; i++)
   {
           for (int j = 0 ; j < 3; j++)
           {
                   A[j] = 0;
           }

           b = 0.0;
   }

   A[0][0] = 1.0;
   A[1][1] = 1.0;
   b[0] = X[0];
   b[1] = Y[0];
...
at runtime i watch the values of A[0][0] A[0][1] etc..
I see that the istruction A[0][0] = 1.0 modify A[0][0] but A[1][0] and
A[2][0] too.
Where is the problem?

Nanji:

You have not defined a two-dimensional double array. You have just
defined an array of 3 double pointers. A[0], for example is a pointer to
double, but it is not pointing at anything. You need to do

A[0] = new double[3];
A[1] = new double[3];
A[2] = new double[3];

--
David Wilkinson
Visual C++ MVP- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -


I've tried to run same code in Borland C++ Builder 2006.
THERE IS NO ERROR!

IS THIS A VC++ 2008 BUG?
 
D

David Wilkinson

Nangi said:
Nangi said:
Hi, I've got a problem with this code.
I've got a double Matrix A and a vector b, initialized with
� �DoubleMatr A = new double*[3];
� �DoubleVect b = new double[3];
where DoubleMatr and DoubleVect are defined as
typedef double* DoubleVect;
typedef double** DoubleMatr;
then i pass the matrix and the vector to a function
void CreateSystemFromPolygon(Polygon *p, double *X, double *Y,
DoubleMatr A, DoubleVect b)
...
� �for ( int i = 0 ; i < 3 ; i++)
� �{
� � � � � �for (int j = 0 ; j < 3; j++)
� � � � � �{
� � � � � � � � � �A[j] = 0;
� � � � � �}
� � � � � �b = 0.0;
� �}
� �A[0][0] = 1.0;
� �A[1][1] = 1.0;
� �b[0] = X[0];
� �b[1] = Y[0];
...
at runtime i watch the values of A[0][0] A[0][1] etc..
I see that the istruction A[0][0] = 1.0 modify A[0][0] but A[1][0] and
A[2][0] too.
Where is the problem?

Nanji:

You have not defined a two-dimensional double array. You have just
defined an array of 3 double pointers. A[0], for example is a pointer to
double, but it is not pointing at anything. You need to do

A[0] = new double[3];
A[1] = new double[3];
A[2] = new double[3];

--
David Wilkinson
Visual C++ MVP- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -


I've tried to run same code in Borland C++ Builder 2006.
THERE IS NO ERROR!

IS THIS A VC++ 2008 BUG?


Nanji:

Maybe the bug (if any) is in the debugger, not in the code generator.
 

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

Top