Non-constant expression or empty matrix. This expression must be constant because its value determines the size or class of some expression.
조회 수: 56 (최근 30일)
How should I correctly pass in variable structure fields to support C/C++ code generation? My problem is simplified by the fact that the field name "str" of the following structure is dynamically changing and the exact value can only be determined at runtime
function out = testStruct()%#codegen
s = struct();
for i = 1:3
str = randGenChar();
s.(str) = i;
out = s;
function str = randGenChar()
basechar = 'abcdefghijklmnopqrstuvwxyz';
idx = randi(numel(basechar));
str = basechar(1:idx);
Then execute command line:
codegen -config:mex testStruct.m -report
Non-constant expression or empty matrix. This expression must be constant
because its value determines the size or class of some expression.
Error in ==> testStruct Line: 6 Column: 8
Code generation failed: To view the report, open('codegen/mex/testStruct/html/report.mldatx')
Walter Roberson 2023년 4월 18일
You don't. Code generation cannot handle dynamically generated field names (except perhaps some that can be somehow resolved at compile time through analysis.)
In C and C++ the only form of dynamic field name access to a struct is either by reinterpret-cast and indexing to the appropriate offset, or else by union against a type that you can index.
Once compiled, in C and C++ the only record of the correspondance between struct field name and offset relative to the beginning of the struct, is held in the debug tables. It is not even held for linkage purposes across multiple source files: in C and C++ you commonly #include header files that define the struct fields each time, and if two different source code files define the struct differently then you are dealing with Undefined Behaviour, and are lucky if the compiler happens to notice.
In C especially, everything is compile-time type checking; after that all that gets passed around is constants and pointers, and the compiled function just knows "under this circumstance access a double at offset 72 relative to the pointer that was passed in".
추가 답변 (0개)
Code Generation Fixed-Point Designer Data Type Exploration Algorithm Acceleration Algorithm Design for Acceleration Function Definition