April 18, 2024, 06:09:42 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


trouble with pointers, etc.

Started by John S, December 31, 2006, 02:32:32 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

John S

Paul,

I had this code working for a while, but now it only prints out 0.0 values for the elements.



/* -------------------------------------------------------
   vector.src  -  Testing Matrix Functions
   by John M. Siino, P.E.
   12/28/06
*/

#include "vector.inc"

sub main()
{
string text;
text = "Testing Basic Vector Library Functions";
writeln ("\n\n"+text+"\n\n");

writeln ("initializing vectors\n\n");
DVector vectorA;

int i, rows = 6;
unsigned int flag;

flag = vectorA.SetSize(rows);

for(i = 0; i < rows; i++)
   {
     vectorA.Set(i, 10.0*i);
   }

print( "\nVector A\n");
for(i = 0; i < rows; i++)
   {
     print( "A", i, " = ", vectorA.Get(i), "\n");
   }

do{}until getkey() <> "";
return;
}


/* vector.inc  -  Matrix Functions
   by John M. Siino, P.E.
   12/28/06

   VCpy(A, B)  : Copy data from A to B  B = A
   VAdd(A, B, C)     : Vector Addition        c = A + B
   VMlt(A, B, s)     : Vector Multiplication  s = A x B
   VDot(A, B, s)     : Dot Product            s = A dot B

Notes:
One dimensional matrices (vectors) are essentially a n row x 1 column vector as follows:
    |0.0|    // row 0 column 0   (1st row)
    |1.0|    // row 1 column 0
    |2.0|    // row 2 column 0
    ...
    |n-1.0|  // row n-1 column 0 (nth row)

Two dimensional matrices are referenced as n rows x m columns (n,m) as follows:
    |   0.0,   0.1,   0.2, ... ,   0.m-1|   // row 0    columns 0 to m-1
    |   1.0,   1.1,   1.2, ... ,   1.m-1|
    |   2.0,   2.1,   2.2, ... ,   2.m-1|
    ...
    | n-1.0, n-1.1, n-1.2, ... , n-1.m-1|   // row n-1  columns 0 to m-1

  or as:

    |   0,    1,    2, ... ,   m-1|
    | m+0,  m+1,  m+2, ... ,  2m-1|
    |2m+0, 2m+1, 2m+2, ... ,  3m-1|
    |3m+0, 3m+1, 3m+2, ... ,  3m-1|
    ...
    | (n-1)m+0, (n-1)m+1, (n-1)m+2, ... , (n-1)m-1|

Within this library, all vectors and matrices are set up as dynamic arrays of a single dimension
with size = nrows*ncolumns = n * m elements.

A vector is actually a column vector, but stored as a continuous series of memories locations.
A matrix is stored by rows, one after another.  This will allow the multiplication of a matrix
(very common practice) by a vector easier to code.

The column vector is stored as:
{ 0.0, 1.0, 2.0, ..., n-1.0 }

The matrix is stored as:
{ 0.0, 0.1, 0.2, ..., 0.m-1, 1.0, 1.1, 1.2, ..., 1.m-1, ..., n-1.0, n-1.1, n-1.2, ..., n-1.m-1,  }
or
{ 0, 1, 2, ..., m-1, m, m+1, m+2, ..., 2m-1, ..., (n-1)m, (n-1)+1, (n-1)m+2, ..., (n-1)+m-1,  }

*/

#autodefine "off"

class DVector           // Floating Point Column Vector
{
declare  DVector();    // class constructor
declare _DVector();    // class destructor

//member variables
int nrows, size, pos;
double *pDVectorA;

//member functions
declare virtual SetSize(int nrows), unsigned int;        // Set size of Vector
declare virtual GetSize(), int;                          // Get size of Vector
declare virtual Set(int pos, double value);              // Set value at specified position
declare virtual Get(int pos), double;                    // Get value at specified position
declare virtual Add(double value), unsigned int;         // Add value at end
declare virtual Ins(int pos, double value);              // Insert value at specified position
declare virtual Del(int pos);                            // Delete value at specified position (& shrink)
declare virtual ZeroAll();                               // Set all elements to zero (0.0)
}


DVector :: DVector()
{
size = 1;
if SetSize(size) < 1
   {
writeln("insufficient memory\n");
   }
pos = 0;
return;
}

DVector :: _DVector()
{
//clean up our object
if(size)
   {
     Delete pDVectorA;
     size  = NULL;
}
pos=0;
return;
}


DVector :: SetSize(int nrows), unsigned int
{
  unsigned int flag;
  if(nrows > size)
   {
double *pDtemp;
pDtemp = new( double, nrows );

   if ( pDtemp = null )
     {
      flag = 0;
     }
   else
     {
  //zero the data
    flag = 1;
      for(int i = 0; i <= nrows-1; i++)
       {
        *pDtemp[i] = 0.0;
       }
    pDVectorA = pDtemp;
   }
    }
  delete pDtemp;
  return flag;
}


DVector :: GetSize(), int
{
  return size;
}


DVector :: Set(int pos, double value)
{
  pDVectorA[pos] = value;               // set value at pos
  return;
}


DVector :: Get(int pos), double
{
  return pDVectorA[pos];                // get value at pos
}


DVector :: Add(double value), unsigned int
{
  unsigned int flag;
double *pDtemp;
  pDtemp = new( double, size + 1 );

  if ( pDtemp = null )
     {
      flag = 0;
     }
  else
     {
  // copy data
    flag = 1;
      for(int i = 0; i <= size-1; i++)
       {
         pDtemp[i] = pDVectorA[i];
       }

    pDtemp[size] = value;
    size = size + 1;
      pDVectorA = pDtemp;
     }

delete pDtemp;
  return flag;
}


DVector :: Ins(int pos, double value), unsigned int
{
  unsigned int flag;
double *pDtemp;
  pDtemp = new( double, size + 1 );

  if ( pDtemp = null )
     {
      flag = 0;
     }
  else
     {
   // copy data
   flag = 1;
      for(int i = 0; i <= pos-1; i++)
       {
        pDtemp[i] = pDVectorA[i];
       }
   pDtemp[pos] = value;                        // insert value at pos
 
   for(i = pos+1; i <= size; i++)          // copy rest of pDVectorA to pDtemp
       {
        pDtemp[i] = pDVectorA[i-1];
       }
    size = size + 1;
      pDVectorA = pDtemp;
     }
  delete pDtemp;
  return flag;
}


DVector :: Del(int pos), unsigned int
{
  unsigned int flag;
double *pDtemp;
  pDtemp = new( double, size - 1 );

  if ( pDtemp = null )
     {
      flag = 0;
     }
  else
     {
    // copy data
    flag = 1;
      for(int i = 0; i <= pos-1; i++)
       {
        pDtemp[i] = pDVectorA[i];
       }
 
    for(i = pos; i <= size-2; i++)         // copy rest of pDVectorA to pDtemp
       {
        pDtemp[i] = pDVectorA[1+i];
       }
    size = size - 1;
      pDVectorA = pDtemp;
     }
  delete pDtemp;
  return flag;
}


DVector :: ZeroAll()
{
double zero = 0;
for(int i = 0; i <= size-1; i++)
  {
   pDVectorA[i] = zero;
  }
return;
}

// End of Class Methods
John Siino, Advanced Engineering Services and Software

Ionic Wind Support Team

A lot of problems there. 

For example in your class constructor you call SetSize. When you assign a pointer:

pDVectorA = pDtemp;

You are not making a copy of the memory, just assigning the address of that memory to another pointer.  Deleting the pDTemp pointer invalidates the memory making pDVectorA point to invalid memory.  In that routine pDTemp is really not needed anyway. 

In your 'Add' method you have this:

  // copy data
    flag = 1;
      for(int i = 0; i <= size-1; i++)
       {
         pDtemp[i] = pDVectorA[i];
       }

    pDtemp[size] = value;
    size = size + 1;
      pDVectorA = pDtemp;
     }

delete pDtemp;
  return flag;


Which should be:


  // copy data
    flag = 1;
      for(int i = 0; i <= size-1; i++)
       {
         *pDtemp[i] = *pDVectorA[i];
       }

    *pDtemp[size] = value;
    size = size + 1;
          delete pDVectorA; // DELETE the ORIGINAL, NOT THE NEW
      pDVectorA = pDtemp;
     }

  return flag;


See my example of a dynamic array, included with the archive named "oop1.src" .  Build off of that if you can.

Paul.
Ionic Wind Support Team

John S

John Siino, Advanced Engineering Services and Software

John S

I have posted elsewhere the example oop1.src reworked for an array of doubles (instead of int) with a couple corrections.  I was trying to get by without the Win API calls but couldn't manage it.  I will be using this as a foundation for the vector and matrix math (linear algebra) library.  The double library will proceed first followed by equivalent integer functions.

for the post, go to:
http://www.ionicwind.com/forums/index.php/topic,1319.0.html#new
John Siino, Advanced Engineering Services and Software