Malloc should work in xPC Target s-functions. In fact, I have
a few drivers that are part of the product that use it.
You should allocate memory in your mdlStart routine, save the
pointer in a P work variable, use it in your mdlOutputs routine
and then free it in mdlTerminate.
What is the incorrect behavior you're seeing?
Gordon Weast
xPC Target Development
The MathWorks
John Reilly wrote:
> what happens when you write a C s-function that uses malloc,
> and then you include that s-function in a model that you
> build for xPC Target?
>
> well, i did it. there were no build errors. it loads and
> runs on the Targetbox. but doesn't produce the right output.
>
> i'm looking at the build output in the command window and
> can't figure out what the linker resolves the malloc call to.
>
> i realize the docs say dynamic memory isn't supported for
> the xPC Target. i'm just experimenting here.
>
> thanks.
>
> john.
>
explicitly says that dynamic memory allocation is not
supported for xPC.
Anyway, I was ginning up a simple test to see if malloc()
worked. So I used an s-function builder block. In the
mdlOutput function, I declared a static pointer, and
allocated memory on the first invocation (initializing it to
1). My function simply multiplied the single input by a
gain and by the value in the dynamic memory. It returned
that value as the only output and stored that value in the
dynamic memory.
My results were odd. Since the input and gain were positive
constant values, the value in dynamic memory and the output
should always have been a positive, increasing value. I was
getting numbers that were positive and negative, increasing
and decreasing.
Also, neither the target nor host scopes ever showed any
output from my s-function builder block. Oddly, the file
scope did produce results. Those results were correct when
dynamic memory was not used.
I am using R2007b, and VC++ Express 2005 with SP1 and the
Win2k3 R2 Platform SDK.
I'll try the experiment again today. If you say it should
work, further investigation is needed on my part. I have
been around long enough to know not to claim my experiment
wasn't badly flawed.
Thanks. I'll be interested to hear your comments regarding
the documentation referenced above.
john.
Gordon Weast <gweast@mathworks.com> wrote in message
<fvvo0q$su1$1@fred.mathworks.com>...
> John,
>
> Malloc should work in xPC Target s-functions. In fact, I have
> a few drivers that are part of the product that use it.
>
> You should allocate memory in your mdlStart routine, save the
> pointer in a P work variable, use it in your mdlOutputs
routine
> and then free it in mdlTerminate.
>
> What is the incorrect behavior you're seeing?
>
> Gordon Weast
> xPC Target Development
> The MathWorks
>
> John Reilly wrote:
> > what happens when you write a C s-function that uses malloc,
> > and then you include that s-function in a model that you
> > build for xPC Target?
> >
> > well, i did it. there were no build errors. it loads and
> > runs on the Targetbox. but doesn't produce the right
output.
> >
> > i'm looking at the build output in the command window and
> > can't figure out what the linker resolves the malloc
call to.
> >
> > i realize the docs say dynamic memory isn't supported for
> > the xPC Target. i'm just experimenting here.
> >
> > thanks.
> >
> > john.
> >
To be perfectly honest, I'm not sure why there isn't an x in
the dynamic memory allocation row for xPC Target. It works fine.
There may be other implications of an x there that aren't obvious
to me though. I have to do some research about what that implies
for all of the target types.
Gordon Weast
xPC Target Development
The MathWorks
John Reilly wrote:
> Hi Gordon,
>
> I'm a little confused. The documentation
>
> http://www.mathworks.com/access/helpdesk/help/toolbox/rtw/ug/f1027373.html
>
> explicitly says that dynamic memory allocation is not
> supported for xPC.
>
> Anyway, I was ginning up a simple test to see if malloc()
> worked. So I used an s-function builder block. In the
> mdlOutput function, I declared a static pointer, and
> allocated memory on the first invocation (initializing it to
> 1). My function simply multiplied the single input by a
> gain and by the value in the dynamic memory. It returned
> that value as the only output and stored that value in the
> dynamic memory.
>
> My results were odd. Since the input and gain were positive
> constant values, the value in dynamic memory and the output
> should always have been a positive, increasing value. I was
> getting numbers that were positive and negative, increasing
> and decreasing.
>
> Also, neither the target nor host scopes ever showed any
> output from my s-function builder block. Oddly, the file
> scope did produce results. Those results were correct when
> dynamic memory was not used.
>
> I am using R2007b, and VC++ Express 2005 with SP1 and the
> Win2k3 R2 Platform SDK.
>
> I'll try the experiment again today. If you say it should
> work, further investigation is needed on my part. I have
> been around long enough to know not to claim my experiment
> wasn't badly flawed.
>
> Thanks. I'll be interested to hear your comments regarding
> the documentation referenced above.
>
> john.
>
>
>
> Gordon Weast <gweast@mathworks.com> wrote in message
> <fvvo0q$su1$1@fred.mathworks.com>...
>> John,
>>
>> Malloc should work in xPC Target s-functions. In fact, I have
>> a few drivers that are part of the product that use it.
>>
>> You should allocate memory in your mdlStart routine, save the
>> pointer in a P work variable, use it in your mdlOutputs
> routine
>> and then free it in mdlTerminate.
>>
>> What is the incorrect behavior you're seeing?
>>
>> Gordon Weast
>> xPC Target Development
>> The MathWorks
>>
>> John Reilly wrote:
>>> what happens when you write a C s-function that uses malloc,
>>> and then you include that s-function in a model that you
>>> build for xPC Target?
>>>
>>> well, i did it. there were no build errors. it loads and
>>> runs on the Targetbox. but doesn't produce the right
> output.
>>> i'm looking at the build output in the command window and
>>> can't figure out what the linker resolves the malloc
> call to.
>>> i realize the docs say dynamic memory isn't supported for
>>> the xPC Target. i'm just experimenting here.
>>>
>>> thanks.
>>>
>>> john.
>>>
>
I finally got around to trying out malloc on xPC Target 3.3.
As you said, it works fine.
If you come across any "implications", please let us know.
And it wouldn't hurt to send an email and get the docs updated.
Thanks for your help.
john.
Gordon Weast <gweast@mathworks.com> wrote in message
<g01j28$res$1@fred.mathworks.com>...
> John,
>
> To be perfectly honest, I'm not sure why there isn't an x in
> the dynamic memory allocation row for xPC Target. It
works fine.
>
> There may be other implications of an x there that aren't
obvious
> to me though. I have to do some research about what that
implies
> for all of the target types.
>
> Gordon Weast
> xPC Target Development
> The MathWorks
Tags for this Thread
Add a New Tag:
Separated by commas
Ex.: root locus, bode
What are tags?
A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.
Anyone can tag a thread. Tags are public and visible to everyone.
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central. Read the complete Disclaimer prior to use.