title: Malloc error in excel called .exe library
body: <p>I'm using office 365 in 64 bit, with Windows 10 OS.</p>
  
  <p>I compile my .exe file with Visual Studio 2017 Community.</p>
  
  <p>Each time I met <code>malloc()</code> function, it report a violation of memory access.</p>
  
  <p>It is nearly impossible for me to stop using <code>malloc</code> since there are some data base API also used <code>malloc</code>.</p>
  
  <p>The following are codes from Excel (VBA):</p>
  
  <pre class="lang-vb prettyprint-override"><code>Option Explicit
  Public Declare PtrSafe Function ExcelCalc Lib "E:\AEONLIFE\CalcMProj\ActThread2\x64\Debug\ActThread2.exe" ( _
                  ByVal PlanCode As String, _
                  ByVal chnl As Long, _
                  ByVal sensi As Long, _
                  ByRef a As Double, _
                  ByRef b As Double) As Long
  
  Dim a(0 To 1273) As Double
  Dim b(0 To 1273) As Double
  
  
  Sub test()
      Dim c As Long
      Dim PlanCode As String
  
  
      c = ExcelCalc("502001", 1, 0, a(1), b(1))
  End Sub
  </code></pre>
  
  <p>The following is C code:</p>
  
  <pre><code>extern __declspec(dllexport) int WINAPI ExcelCalc(char *PlanCode, int chnl, int scen, double *a, double *b)
  {
  #pragma region 初始化
      //**********  本地变量  **********
      double     mfac = (double)1 / (double)YEARSTEP;
      char       TempPID[PlanIDLEN];
      int        Ind, PlanCodeInt;
      DB         dbconn;
      pfdrv      ProfDrv;
      tData      Data;
      temp       Temp;
      //********** 申请堆空间 **********
      cashflow  *CashFlow     = malloc(sizeof(cashflow));
      sensi     *Sensi        = malloc(sizeof(sensi));
      calcconst *CalcConst    = malloc(sizeof(calcconst)*MAXCHNL);
      cf_calc   *CF_Calc_Base = malloc(sizeof(cf_calc));
      rate_calc *RateCalc     = malloc(sizeof(rate_calc));
      decrate   *DecRate      = malloc(MAXCHNL * sizeof(decrate));
      //********** 链接数据库 **********
      if (dbconnect(&amp;dbconn) == FAIL) { return 0; };
  #pragma endregion
      strcpy_s(&amp;TempPID[0], sizeof(TempPID), PlanCode);
      TempPID[4]  = 0;
      PlanCodeInt = atoi(&amp;TempPID[0]);
  
      LoadAsump(PlanCode, &amp;dbconn, DecRate, CashFlow, Sensi, CalcConst, &amp;ProfDrv, mfac);
      Ind = LoadTemp(&amp;Data, &amp;Temp, DecRate, &amp;CashFlow-&gt;GCV[0], &amp;CashFlow-&gt;CFOutgo[chnl], mfac);
  
      Calc_CashFlow(&amp;Temp, CF_Calc_Base, &amp;CashFlow-&gt;CFOutgo[chnl], &amp;CalcConst[chnl], PlanCodeInt, mfac);
      CalcDec(&amp;Temp, &amp;DecRate[chnl], RateCalc, &amp;Sensi-&gt;SensiRT[scen][0], &amp;CalcConst[chnl], mfac);
  
  
  #pragma region 计算后处理
      //********** 释放堆空间 **********
      free(DecRate);
      free(CashFlow);
      free(Sensi);
      free(CalcConst);
      //**********  断开链接  **********
      dbcleares(&amp;dbconn);
      PQfinish(dbconn.conn);
  #pragma endregion
  
      a[0] = 1;
      a[1] = 3.5;
      b[0] = 1;
      b[1] = 7;
      return 1;
  }
  </code></pre>
  
  <p>Little bit complicated but the error appears in line 12, the first malloc(), as the following picture:</p>
  
  <p><a href="https://i.stack.imgur.com/iTStM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iTStM.png" alt="enter image description here"></a></p>
  
  <p>It's Chinese and says memory access violation in address 0x181FC1A.</p>
  
  <p>I did assembly debugging, it seems that I didn't load the library that containing malloc. I'll try to load the relating library and try again.</p>
  
  <p>Thanks!!</p>