Re: [Plib-users] legend update
Brought to you by:
sjbaker
|
From: Sebastian U. <ud...@ha...> - 2002-01-29 11:29:06
|
On Tue, 29 Jan 2002, st...@nt... (stathis) wrote:
> Date: Tue, 29 Jan 2002 09:21:26 -0000
> To: pli...@li...
> From: st...@nt... (stathis)
> Subject: [Plib-users] legend update
>
> Hi everyone,
>
> I am trying to setup sliders that will update their legend with the
> slider value. So as I move the slider along I can see it's value. I
> managed to do this with getStringValue and then setLegend. However on a
> floating point slider, it send back values with too many digits (.i.e
> 0.44555).
[...]
> void sliderCB( puObject *sli) {
>
> float value;
> char sliText[4];
>
> slicb->getValue(&value);
> sprintf(sliText, "%.2f", value);
> cout << sliText << endl; // this output is *exactly* what I need
>
> slicb->setLegend(sliText); // non-sense chars on the legend :(
> ...
> }
>
> No matter what I do to try and reformat the string (with sprintf), prior
> to sending it on the legend, I get on my legend some strange characters.
> What am I possibly doing wrong?
You are playing with pointers to arrays that have a limited lifetime !
The sliText[4] array is allocated when the sliderCB () routine is entered
and destroyed when it exits. After it has been destroyed, chances are high
that the appropiate memory region will be re-used for something else soon -
hence the non-sense chars.
Try declaring the array "static":
static char sliText[4] ;
This way, the array will be allocated upon application startup and
destroyed when the application quits (like a global variable). This has two
important effects:
- It's content is kept from one call of the routine to another
- It's adress does not become invalid after the function has returned
As a general rule, be *very* careful with pointers to variables or arrays
that have a limited lifetime !
- Sebastian
|