C++和MATLAB混編的問題集錦

是皮皮攀呀,發表於2018-11-01

抱著感恩的心終於把所有的問題都除錯通過了,

最後跑程式的時候,還沒開始一會,程式就卡死在定義mwArray型別的變數上了。

 

下面先來講講mwArray

 mwArray :C++用它向MATLAB傳遞輸 i/o 引數。MATLAB中的引數都是矩陣表示,即使是1*1的矩陣。

1)建構函式

  mwArray(mwSize num_dims, const mwSize* dims, mxClassID mxID,mxComplexity cmplx = mxREAL)

  num_rows:行數; num_cols:列數;mxID:The data type type of the matrix.: cmplx:The complexity of the matrix (numeric types only).

   mwArray a(2, 2, mxDOUBLE_CLASS);

   mwArray b(3, 3, mxSINGLE_CLASS, mxCOMPLEX);

   mwArray c(2, 3, mxCELL_CLASS); 

2)例子

     #include "mclcppclass.h"

      double data[4] = {1.0, 2.0, 3.0, 4.0};

      double x;

       mwArray a(2, 2, mxDOUBLE_CLASS);

      a.SetData(data, 4);

      x = a.Get(1,1); // x = 1.0

       x = a.Get(2, 1, 2); // x = 3.0

       x = a.Get(2, 2, 2); // x = 4.0

基本上需要用到的就是以上兩點了。

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

發現我在cc檔案中定義mwArray變數的時候,沒有進行整個程式的初始化,加入初始化是否成功的判斷之後,程式碼執行得遠一些了,雖然報了更多的錯誤,但是,至少能提示錯誤出現在了,什麼型別的錯誤啦。感恩的心,繼續找錯誤。

錯誤太多,選擇性貼上一些:

*** Error in ‘XX路徑': free(): invalid pointer: 0x00007ffd32e1b3c8 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fc8500d67e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7fc8500df37a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fc8500e353c]
/usr/local/MATLAB/R2017b/bin/glnxa64/libmwi18n.so(+0xb5493)[0x7fc7d3283493]
/usr/local/MATLAB/R2017b/bin/glnxa64/libmwi18n.so(_ZN2fl10filesystem6tmpnamERKSbIDsSt11char_traitsIDsESaIDsEES6_S6_+0x3f)[0x7fc7d326c5ff]
``````

======= Memory map: ========
00400000-0042b000 r-xp 00000000 08:21 164327                            

XX路徑
0062a000-0062b000 r--p 0002a000 08:21 164327                            

XX路徑
0062b000-0062c000 rw-p 0002b000 08:21 164327                            

XX路徑
0062c000-0062d000 rw-p 00000000 00:00 0 
01e00000-12d40000 rw-p 00000000 00:00 0                                  

[heap]
7f4ba3910000-7f4ba3920000 r-xp 00000000 08:15 2256809      

``````

------------------------------------------------------------------------
              abort() detected at Thu Nov  1 09:31:26 2018
------------------------------------------------------------------------

Configuration:
  Crash Decoding      : Disabled - No sandbox or build area path
  Crash Mode          : continue (default)
  Default Encoding    : UTF-8
  GNU C Library       : 2.23 stable
  Host Name           : zp
  MATLAB Architecture : glnxa64
  MATLAB Root         : /usr/local/MATLAB/R2017b
  MATLAB Version      : 9.3.0.713579 (R2017b)
  Operating System    : Linux 4.15.0-36-generic #39~16.04.1-Ubuntu SMP Tue Sep 25 08:59:23 UTC 2018 x86_64
  Processor ID        : x86 Family 6 Model 158 Stepping 9, GenuineIntel

Fault Count: 1
Abnormal termination:
abort()

Register State (from fault):
  RAX = 0000000000000000  RBX = 000000000000008c
  RCX = 00007f4c3e67b428  RDX = 0000000000000006
  RSP = 00007ffc139be718  RBP = 00007ffc139beab0
  RSI = 0000000000006b74  RDI = 0000000000006b74

   R8 = 000000000000002f   R9 = 0000000000000000
  R10 = 0000000000000008  R11 = 0000000000000206
  R12 = 000000000000008c  R13 = 00007ffc139be8c8
  R14 = 00007ffc139be8c8  R15 = 0000000000000002

  RIP = 00007f4c3e67b428  EFL = 0000000000000206

   CS = 0033   FS = 0000   GS = 0000
頭大ing~

 

 

 

 

 

 

 

 

 

 

相關文章