Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning a value from an Array to an another Array

Status
Not open for further replies.

Raju05

Programmer
May 2, 2008
2
GB
Hi,


BACKGROUND
I am creating a simple application.

It an Excel file which connects to a C++ DLL.
I am building the C++ DLL in Bloodshed Dev-C++.

I can connect an Excel file to the DLL successfully.


PROBLEM
The problem I have is a assigning a value to an array in the C++ code.

The excel passes a Variant ByRef which is a 2D Array (23 x 23)
as it passes the Variant it passes address information as well making the Variant passed in 23x46.

So I try to use the Pointer Array (Varinat passed in) to strip the data I want into another array (23x23)

Code:
const int nNumRows = 23;  
const int nNumCols = 23; 
double Billday2[nNumRows][nNumCols] = { 0 };
double tempVal;
    
for (int j = 0; j<48; j++)
{
   j++;
   int xx;
   if (j==1)
  { xx = j; }
  else
  { xx = (j-1)/2; }
  for (int i = 0; i<24; i++)
  {
         tempVal = new double Billday[i][j];
         myfile << tempVal << ", ";
         Billday2[i][xx] = tempVal;
         myfile <<  i << "x" << xx << ", ";
  }
   myfile << "\n";
}

The line that crashes the DLL is Billday2[xx] = tempVal;
Hence it cannot assign a value to the array.

If I comment out this line the code works fine and prints a txt file with all the data I want.

Can somebody help. ??
 
You have a HORRID memory leak in that code. Don't use new double with tempVal, since you've already declared it as a double.

What xx does the crash occur at?

Also, you have an array declared as Billday2, then access a value in Billday. I take it you just haven't displayed the declaration for that, or is it an error?

I hope your Billday array has 23 * 47 elements in it, too, or you're going to be accessing memory that is unknown.

Lee
 
Hi

I have removed the new double.

The crash occurs when i=5 and xx=2

Billday2 is a double Billday2[23][23] = { 0 };

Billday is a double Billday2[][48];

It only when I introduce the Billday2[xx] = tempVal; line does the program crash

New Coding below, the return value is a dumby until I have the function working.
Code:
extern "C" DLLIMPORT __stdcall double GetTRValue(     double* VolLPut, 
                                                      double* VolSCall, 
                                                      double* VolLCall, 
                                                      double* ForwardCurve, 
                                                      double* Correlation, 
                                                      double* IR, 
                                                      double* T2, 
                                                      double* Tavg, 
                                                      double LPut, 
                                                      double SCall, 
                                                      double LCall, 
                                                      double LputQty,
                                                      double Billday [][48])
{
    ofstream myfile;
    myfile.open ("Debug.txt");
    myfile << "Billday" << "\n";
    const int nNumRows = 23;  
    const int nNumCols = 23; 
    double Billday2[nNumRows][nNumCols] = { 0 };
    double tempVal;
    for (int j = 0; j<48; j++)
    {
      j++;
      int xx;
      if (j==1)
      { xx = 0; }
      else
      { xx = (j-1)/2; }
      for (int i = 0; i<24; i++)
      {
         tempVal = Billday[i][j];
         myfile << tempVal << ", ";
         Billday2[i][xx] = tempVal;
         myfile <<  i << "x" << xx << ", ";
     }
      myfile << "\n";
    }
    myfile << "\n\n\n\n\n";
    
    myfile.close(); 
                                      
    return 12345.67890;
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top