Jump to content
  • Advertisement
Sign in to follow this  

Using const char*

This topic is 2147 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a lot of functions that using const char* as argument type. Can i bind these functions? Or i necessarily have to using std::string?

What i would like to do:
void print(const char *str){


Thanks in advance. Edited by Cordan

Share this post

Link to post
Share on other sites
It is recommended to use wrappers for functions that take or return const char *. Pointers like these are simply not safe. You would have little control over when strings are created or destroyed, and will likely end up with loose pointers that can cause any kind of havok in your application.

That said, if you are in full control over who writes the scripts and accept the same potential problems that occur in C++ you can register the const char * as a type like this:

engine->RegisterObjectType("charptr", sizeof(const char *), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_PRIMITIVE);

Then use this type to register the functions that take or return the const char*, i.e.

engine->RegisterGlobalFunction("void print(charptr)", asFUNCTION(print), asCALL_CDECL);


Share this post

Link to post
Share on other sites
I have problems with this on 64bit mac. When i try to build the script i get errors:

Don't support passing type 'charptr' by value to application in native calling convention on this platform

How can i fix this problem?
Thanks in advance.

Share this post

Link to post
Share on other sites
A couple of changes in the library is needed:

as_callfunc.cpp, function PrepareSystemFunction(), around line 325:

// It's not safe to pass objects by value because different registers
// will be used depending on the memory layout of the object
// Ref: http://www.x86-64.org/documentation/abi.pdf
// Ref: http://www.agner.org/optimize/calling_conventions.pdf
!(func->parameterTypes[n].GetObjectType()->flags & COMPLEX_MASK) &&
func->parameterTypes[n].GetSizeInMemoryDWords() < AS_LARGE_OBJ_MIN_SIZE &&
- !(func->parameterTypes[n].GetObjectType()->flags & (asOBJ_APP_CLASS_ALLINTS | asOBJ_APP_CLASS_ALLFLOATS)) )
+ !(func->parameterTypes[n].GetObjectType()->flags & (asOBJ_APP_PRIMITIVE | asOBJ_APP_FLOAT | asOBJ_APP_CLASS_ALLINTS | asOBJ_APP_CLASS_ALLFLOATS)) )
engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, func->GetDeclarationStr().AddressOf());
asCString str;
str.Format(TXT_DONT_SUPPORT_TYPE_s_BY_VAL, func->parameterTypes[n].GetObjectType()->name.AddressOf());
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
engine->ConfigError(asINVALID_CONFIGURATION, 0, 0, 0);

and in

as_callfunc_x64_gcc.cpp, function CallSystemFunctionNative(), around line 290:

// An object is being passed by value
if( (descr->parameterTypes[a].GetObjectType()->flags & COMPLEX_MASK) ||
descr->parameterTypes[a].GetSizeInMemoryDWords() > 4 )
// Copy the address of the object
argsType[argIndex] = x64INTARG;
memcpy(paramBuffer + argIndex, stack_pointer, sizeof(asQWORD));
- else if( descr->parameterTypes[a].GetObjectType()->flags & asOBJ_APP_CLASS_ALLINTS )
+ else if( (descr->parameterTypes[a].GetObjectType()->flags & asOBJ_APP_CLASS_ALLINTS) || (descr->parameterTypes[a].GetObjectType()->flags & asOBJ_APP_PRIMITIVE) )
// Copy the value of the object

I'll have these changes checked in as soon as possible.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!