Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi,

Looks sane to me, but there's a portability issue in that your code requires a C99 compliant compiler.

It therefore won't build for me with any of my Microsoft compilers - though I don't have a recent MS compiler to test with.
(The most recent MS compiler I have is  Microsoft (R) C/C++ Optimizing Compiler Version 14.00.40310.41 for AMD64.)

Even if I pre-declare everything and rewrite your XSub as:
int testing(int channel, SV* byte_ref, int len){ SV ** elem; AV * bytes = (AV*)SvRV(byte_ref); int num_bytes = av_len(bytes) + 1; unsigned char buf[num_bytes]; int i; int x; if (channel != 0 && channel != 1){ croak("channel param must be 0 or 1\n"); } if (! SvROK(byte_ref) || SvTYPE(SvRV(byte_ref)) != SVt_PVAV){ croak("not an aref\n"); } num_bytes = av_len(bytes) + 1; if (len != num_bytes){ croak("$len param != elem count\n"); } for (i=0; i<len; i++){ elem = av_fetch(bytes, i, 0); buf[i] = (int)SvNV(*elem); } /* * here, I'll be passing the char buffer and len * to an external C function. For display, I'll * just print the elements * * if ((spiDataRW(channel, buf, len) < 0){ * croak("failed to write to the SPI bus\n"); * } */ for (x=0; x<len; x++){ printf("%d\n", buf[x]); } }
it still errors out with:
try_pl_1bcb6.xs(10) : error C2057: expected constant expression try_pl_1bcb6.xs(10) : error C2466: cannot allocate an array of constan +t size 0 try_pl_1bcb6.xs(10) : error C2133: 'buf' : unknown size NMAKE : fatal error U1077: 'cl' : return code '0x2' Stop.
I would work around that by dynamically allocating buf (Newxz/Safefree) but perhaps there's another way.

Anyway - it's only an issue when (if) your code meets a compiler that's not C99 compliant.

Cheers,
Rob

In reply to Re: Is this a sane/safe way to pass an aref into a C function? by syphilis
in thread Is this a sane/safe way to pass an aref into a C function? by stevieb

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-18 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found