I thought about doing that, the problem is that I don't know how would that affect memory handling. I mean, If I'd had a function like this in C++:

string bleh(void);

I could easily have a C wrapper that would do:

const char* blehC(void) { return bleh(void).c_str(); }

But... this has a big problem: you are returning a pointer to a local variable (the returned string inside blehC is local to blehC).

So, what I would need is to tell XS/perl/whatever to give me a piece of memory that it considers a scalar where I can write to (wether it is a scalar being returned or a reference to a scalar which I receive). I guess that I don't have an alternative to write something in the CODE: section, but I really wouldn't know how to do that nor how the C/C++ function should look like. I mean, in C I don't have references, so I would be taking a char*, but... how do I know how much space do I have to write? When I write to a std::string I'm indirectly allocating memory and returning to something a user gave me. So, at C level, how does XS/perl/whatever knows how to free the memory I return? assuming that I do something like this:

char* respondC(const char* input, const char* username) { char* ret = NULL; if (respond(input, username, cpp_ret)) { string cpp_ret; ret = new char[cpp_ret.length()+1]; strncpy(ret, cpp_ret.c_str(), cpp_ret.length()); } return ret; }
(ignoring possible fencepost errors)

So, that is what is haunting me... Maybe there's a way to not even write at CODE: but just write a C function that takes a scalar reference (SV* or something like that) where I can copy (as a string) the result of the C++ function. Anyway, that would be ideal, not sure if possible.


In reply to Re^2: C++ strings, references and XS (char*) by v01d
in thread C++ strings, references and XS by v01d

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.