SDK Question Access violation, oapiWriteScenario

Bj

Addon Developer
Addon Developer
Donator
Joined
Oct 16, 2007
Messages
1,886
Reaction score
11
Points
0
Location
USA-WA
Website
www.orbiter-forum.com
Here is the problem;

Access violation reading location

This is it (shortened)

Code:
    string name = "hi";
        char * buff;
        name.push_back(NULL); //append 0 to string so string is 0 terminiated
        //name now == 'hi[null]'  //just to be sure- 
        strcpy(buff,name.c_str());
        oapiWriteScenario_string(scn,"NAMED",buff);

Before anyone asks, it still doesn't work when you remove the push_back.

Something like this works;

oapiWriteScenario_string(scn,"NAMED","babalu");

No other errors, any ideas?
 

martins

Orbiter Founder
Orbiter Founder
Joined
Mar 31, 2008
Messages
2,448
Reaction score
462
Points
83
Website
orbit.medphys.ucl.ac.uk
You never allocate space for buff.
Why do you want to copy the string in the first place, instead of using name.c_str() directly as the 3rd argument in oapiWriteScenario_string?
 

Bj

Addon Developer
Addon Developer
Donator
Joined
Oct 16, 2007
Messages
1,886
Reaction score
11
Points
0
Location
USA-WA
Website
www.orbiter-forum.com
You never allocate space for buff.
Why do you want to copy the string in the first place, instead of using name.c_str() directly as the 3rd argument in oapiWriteScenario_string?


oop

The more obvious they are...

anyway, its because of compiler error;

'oapiWriteScenario_string' : cannot convert parameter 3 from 'const char *' to 'char *'


This works, Thanks :tiphat:

string name = "hi";
char * buff = new char[50];
strcpy(buff,name.c_str());

FILEHANDLE scn = oapiOpenFile ("TEST",FILE_APP,ROOT);

//oapiWriteScenario_string(scn,"NAMED",buff);
oapiWriteScenario_string(scn,"NAMED",name.c_str());
 

dbeachy1

O-F Administrator
Administrator
Orbiter Contributor
Addon Developer
Donator
Beta Tester
Joined
Jan 14, 2008
Messages
9,217
Reaction score
1,563
Points
203
Location
VA
Website
alteaaerospace.com
Preferred Pronouns
he/him
You could also just do this:

Code:
string name = "hi";
oapiWriteScenario_string(scn,"NAMED", (char *)name.c_str());

...to cast away the constness there. In this case it is safe to do because oapiWriteScenario_string does not modify the string passed in.

In any case, if you use the "new->copy->write" sequence anyway, don't forget to free up buff when you're done with it. But all that is a lot less elegant than just typecasting it IMO. :)
 

Hielor

Defender of Truth
Donator
Beta Tester
Joined
May 30, 2008
Messages
5,580
Reaction score
2
Points
0
You could also just do this:

Code:
string name = "hi";
oapiWriteScenario_string(scn,"NAMED", (char *)name.c_str());

...to cast away the constness there. In this case it is safe to do because oapiWriteScenario_string does not modify the string passed in.

In any case, if you use the "new->copy->write" sequence anyway, don't forget to free up buff when you're done with it. But all that is a lot less elegant than just typecasting it IMO. :)
I'd recommend using const_cast so it's explicitly obvious what you're doing:
Code:
string name = "hi";
oapiWriteScenario_string(scn,"NAMED", const_cast<char *>(name.c_str()));
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,616
Reaction score
2,336
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
Much better would be if Orbiters API would use correct "const" attributes for the call by reference parameters... but well, there are many good fixes for it. :lol:
 

Enjo

Mostly harmless
Addon Developer
Tutorial Publisher
Donator
Joined
Nov 25, 2007
Messages
1,665
Reaction score
13
Points
38
Location
Germany
Website
www.enderspace.de
Preferred Pronouns
Can't you smell my T levels?
Yup, const correctness never hurts ...
I'd ask for this as a feature request.
 
Top