When using Legacy Code Tool, why would using pointers outside of a function call cause a segmentation violation?

When using the Legacy Code Tool I have found that the following code will compile and generate a Simulink block. However when I use the block in a simulation it causes a segmentation violation. This only happens when variables a, b, and output2 are pointers. When I remove the pointers and just use variables everything functions the way you would expect.
Does anyone know why using pointers would cause the violation and if there is something I can do fix the code so I can use the pointers?
The pointers are important because I have different legacy code, which uses pointers to access global variables, that would be used instead of MyInternalFunction().
// MyFunction.c
#include "MyFunction.h"
FLT *a;
FLT *b;
FLT *output2;
void MyFunction_init(const FLT param, FLT *P1)
{
*P1 = param;
}
void MyFunction(FLT input, FLT *output, FLT *output2, FLT *P1)
{
*output = input+*P1;
*a = input;
*b = *P1;
MyInternalFunction();
}
void MyInternalFunction(void)
{
*output2 = *a+*b;
}
// MyFunction.h
#include "your_types.h" // this h file is from legacy code tool demos
extern void MyFunction_init(const FLT param, FLT *P1);
extern void MyFunction(FLT input, FLT *output, FLT *output2, FLT *P1);
extern void MyInternalFunction(void);
%%Matlab Scrip to create s function block
def = legacy_code('initialize');
def.SFunctionName = 'sfun_MyFunction';
def.InitializeConditionsFcnSpec = 'MyFunction_init(single p1, single work1[1])';
def.OutputFcnSpec = 'MyFunction(single u1, single y1[1], single y2[1], single work1[1])';
def.HeaderFiles = {'MyFunction.h'};
def.SourceFiles = {'MyFunction.c'};
legacy_code('generate_for_sim', def);
legacy_code('slblock_generate', def);

1 Comment

Who allocates memory for the pointers 'a' and 'b'? I don't see it being done in the code above. Also, how is the scope for 'output2' resolved in MyInternalFunction()? I'm guessing that it assigns to the (global) un-allocated pointer variable 'output2', and not the output pointer passed into MyFunction()? Please also make sure that sizeof(FLT) == sizeof(single).

Sign in to comment.

Answers (0)

Categories

Find more on Simulink Compiler in Help Center and File Exchange

Products

Asked:

on 26 Feb 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!