What's wrong with this trivial C engine program?
古いコメントを表示
I'm trying to learn how to run Matlab from C programs, and apparently writing mex files to interface with the Matlab engine is the way to do that. This is Matlab 2009a running on Win32 with MSVC 9 as the compiler.
I made the very simple program below that just allocates an array of structures and then deallocates it, repeating this 100 times. There are ARRAY_SIZE structures in the array. Each structure has three members and each member is a 10x10 matrix. Everything works fine if ARRAY_SIZE is about 5, but slightly larger numbers such as 20 cause the program to crash every time. Anyone know what's wrong with this code? Could I possibly be running out of heap or stack space? Does it have something to do with freeing the array after it has been "put" into the engine's workspace? Thanks for any help~
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <direct.h>
#include "engine.h"
#define ARRAY_SIZE 5
int main(void)
{
Engine *ep;
ep = engOpen(NULL);
engSetVisible(ep, 1);
const char *field_names[] = {"a", "b", "c"};
mxArray *array;
mxArray *field_value;
int foo = 0;
int j, k;
while(foo < 100)
{
array = mxCreateStructMatrix(1, ARRAY_SIZE, 3, field_names);
for(j = 0; j < ARRAY_SIZE; ++j)
{
for(k = 0; k < 3; ++k)
{
field_value = mxCreateDoubleMatrix(10, 10, mxREAL);
mxSetFieldByNumber(array, j, k, field_value);
}
}
engPutVariable(ep, "array", array);
for(j = 0; j < ARRAY_SIZE; ++j)
{
for(k = 0; k < 3; ++k)
{
field_value = mxGetFieldByNumber(array, j, k);
mxDestroyArray(field_value);
}
}
mxDestroyArray(array);
++foo;
}
engClose(ep);
return 0;
}
採用された回答
その他の回答 (1 件)
カテゴリ
ヘルプ センター および File Exchange で MATLAB Compiler についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!