SDK Question oapiGetNavDescr

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
611
Reaction score
52
Points
43
Location
Happy Wherever
Got another elusive query (for me):blush::

Can't find anything in threads on oapiGetNavDescr.

Why shouldn't this work?

global
Code:
char *descr2;

Code:
bool InputTarget(void *id, char *bstr, void *user_data)
{
.......................
VESSEL3 *vessel = (VESSEL3*)oapiGetFocusInterface();
int maxlen = 256;
NAVHANDLE n = vessel->GetNavSource(0);


oapiGetNavDescr (n, descr2, maxlen);
		sprintf(oapiDebugString(),"NAV ILS  %s ",descr2);

If I make int maxlen global and give it NULL, prints as "NULL".
If compile as code above, it ctd.

What am I doing wrong please?
 

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0
The descr2 is just a pointer to char, and Orbiter doesn't allocate the buffer for you, but you must provide an address to already allocated writable memory as 2nd argument of oapiGetNavDescr, i.e. you need to either allocate the memory for character buffer pointed by descr2 pointer, or change it to be a character array and not a pointer.

So either:
  1. Code:
    descr2 = new char [maxlen];
    oapiGetNavDescr (n, descr2, maxlen);
    sprintf (oapiDebugString (), "NAV ILS  %s ", descr2);
    delete [] descr2;
  2. or
    Code:
    char descr2 [256];
    oapiGetNavDescr (n, descr2, sizeof (descr2));
    sprintf (oapiDebugString (), "NAV ILS  %s ", descr2);
 

storm

New member
Joined
Jun 8, 2012
Messages
17
Reaction score
0
Points
0
I would go with the second option so you don't have to deallocate the memory yourself.
 
Top