String.ToCString() issues

About Monkey 2 Forums Monkey 2 Programming Help String.ToCString() issues

This topic contains 2 replies, has 3 voices, and was last updated by  Mark Sibly 3 years ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #1665


    First, I’ve found a bug in the implementation in modules/monkey/types.monkey2 line 217.  ToCString should take two parameters, not one – as defined in the native .cpp file (I already submitted the bug report and fixed it/rebuild modules here locally)


    However, I still can’t get it to work as expected and I get hit with a memory access violation.

    Here is what I am trying to do:

    I’m using an extern to a .c source file, which contains:

    This is externed in my .monkey2 file as:

    Then, I try to use it and it bombs out:

    any ideas?  Has anyone else used ToCString() yet?



    Should it be?


    Docs say monkey.types.CString is the mapping for “char *”


    Mark Sibly

    > char *dev_name   = “/dev/video0”;

    Literal c/c++ strings like this are stored in ‘read only’ memory and shouldn’t be written to.

    Try this instead…

    char dev_name[256];

    dev_name will intially be ’empty’ but that’s OK because you’re using ToCString to initialize it before you use it (I hope).

    The alternative is to pass the dev_name directly to native code, eg:

    This will automagically convert name to a const char * for you when you call open_dev. However, you should NOT store ‘name’ anywhere inside open_dev for later use as the const char * is temporary and will disappear after open_dev returns.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.