<div dir="ltr">On Tue, Feb 11, 2014 at 8:57 AM, Christopher Jordan-Squire <span dir="ltr"><<a href="mailto:cjordan1@uw.edu" target="_blank">cjordan1@uw.edu</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Thanks for the answers! My responses are inline.</blockquote>

<div><br></div><div style>my C is a bit weak, so forgive my misunderstanding, but: </div><div style><span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:12.800000190734863px">>> {</span><br style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:12.800000190734863px">

<span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:12.800000190734863px">>>  char* ind;</span><br style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:12.800000190734863px"><span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:12.800000190734863px">>>  double val, wght;</span><br style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:12.800000190734863px">

<span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:12.800000190734863px">>> } data[] = { {"camera", 15, 2}, {"necklace", 100, 20}, {"vase", 90, 20},</span><br style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:12.800000190734863px">

<span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:12.800000190734863px">>>                  {"pictures", 60, 30}, {"tv", 40, 40}, {"video", 15, 30}};</span></div><div style>

<span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:12.800000190734863px"><br></span></div><div style>Here is my C weakness -- what does this struct look like in memory? i.e is that first element a pointer, and the actually string data is somewhere else in memory? In which case, where? and how does that memory get managed?</div>

<div style><br></div><div style>Or are the bytes at the begining of the struct with the string data in it?</div><div style><br></div><div style>in the "real" case, you say the char* is a string representation of a hex value for a memory address -- so you would know a priory exactly what the length of that sting is, so you could use a numpy struct like:<br>

</div><div style><br></div><div style>('S8,f8,f8')</div><div><br></div><div style>i.e 8 bytes to store the string at the begining of the struct.</div><div><br></div><div style>then & the_array[i] would be the address of the beggining of the string -- which may be what you want.</div>

<div style><br></div><div style>HTH,</div><div style>  -Chris</div><div style><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div class="">>><br>
>> At the C level, data is passed to the function by directly giving its<br>
>> address. (i.e. the C function takes as an argument (unsigned long)<br>
>> data, casting the data pointer to an int)<br>
><br>
><br>
> wow -- that's prone to error! but I"m still not sure which pointer you're<br>
> talking about -- a pointer to this struct?<br>
><br>
<br>
</div>I mean data. data is an array of structs, but in C that's<br>
(essentially) the same as a pointer to a struct. So data is the<br>
pointer I'm referring to.<br>
<div class=""><br>
>> I'd like to create something similar using record arrays, such as<br>
>><br>
>> np.array([("camera", 15, 2), ("necklace", 100, 20), ... ],<br>
>> dtype='object,f8,f8').<br>
><br>
><br>
><br>
>><br>
>> Unfortunately this fails because<br>
>> (1) In cython I need to determine the address of the first element and<br>
>> I can't take the address of a an input whose type I don't know (the<br>
>> exact type will vary on the application, so more or fewer fields may<br>
>> be in the C struct)<br>
><br>
><br>
> still a bit confused, but if this is types as an array in Cython, you should<br>
> be abel to do somethign like:<br>
><br>
> &the_array[i]<br>
><br>
> to get the address of the ith element.<br>
><br>
<br>
</div>To do that I need to be able to tell cython the type of the memory<br>
view I give. There are very few examples for non-primitive arrays in<br>
cython, so I'm not sure what that would look like. Or at least I<br>
*think* I need to do that, based on the cython errors I'm getting.<br>
<div class=""><br>
>> (2) I don't think a python object type is what I want--I need a char*<br>
>> representation of the string. (Unfortunately I can't test this because<br>
>> I haven't solved (1) -- how do you pass a record array around in<br>
>> cython and/or take its address?)<br>
><br>
><br>
> well, and object type will give you a pointer to a pyobject. If you know for<br>
> sure that that pyobject is a string object (probably want a bytes object --<br>
> you son't want unicode here), then you should be abel to get the address of<br>
> the underlying char array. But that would require passing something<br>
> different off to the C code that the address of that element.<br>
><br>
> You could use an unsigned long for that first field, as you are assuming<br>
> that in the C code anyway  but I don't hink there is a way in numpy to set<br>
> that to a pointer to a char allocated elsewhere -- where would it be<br>
> allocated?<br>
><br>
<br>
</div>The strings are char*, the unsigned long cast was simply to cast data<br>
to a memory address. (The specific setup of the library was passing a<br>
string with the memory location in hexadecimal. This seems weird, but<br>
it's because the C functions being called are an intermediary to a<br>
simple (third-party) virtual machine running a program compiled from<br>
another language. It doesn't deal with pointers directly, so the other<br>
language is just passed the memory address, in hex and as a string,<br>
for where the data resides. Along with the number of elements in the<br>
block of memory pointed to.)<br>
<div class=""><br>
> So I would give up on expecting to store the struct directly in numpy array,<br>
> and rather, put something reasonable (maybe what you have above) in the<br>
> numpy array, and build the C struct you need from that rather than passing a<br>
> pointer in directly.<br>
><br>
<br>
</div>That sounds reasonable. I really wanted to avoid this because, as I<br>
mentioned above, I'm just trying to generate the data in numpy and<br>
pass it to this virtual machine running a program compiled from<br>
another language. The form of the struct depends on what was done in<br>
the other language. It could easily have more or fewer fields, have<br>
the fields reordered, etc.. I wanted to avoid having to write wrappers<br>
for all such possibilities and instead use numpy record arrays as<br>
mapping exactly to C structs. But if that's really the only way to go<br>
then I guess I'll have to write struct wrappers in cython.<br>
<div class="im"><br>
> -Chris<br>
><br>
><br>
><br>
> --<br>
><br>
> Christopher Barker, Ph.D.<br>
> Oceanographer<br>
><br>
> Emergency Response Division<br>
> NOAA/NOS/OR&R            <a href="tel:%28206%29%20526-6959" value="+12065266959">(206) 526-6959</a>   voice<br>
> 7600 Sand Point Way NE   <a href="tel:%28206%29%20526-6329" value="+12065266329">(206) 526-6329</a>   fax<br>
> Seattle, WA  98115       <a href="tel:%28206%29%20526-6317" value="+12065266317">(206) 526-6317</a>   main reception<br>
><br>
> <a href="mailto:Chris.Barker@noaa.gov">Chris.Barker@noaa.gov</a><br>
><br>
</div><div class=""><div class="h5">> _______________________________________________<br>
> NumPy-Discussion mailing list<br>
> <a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
> <a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
><br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R            (206) 526-6959   voice<br>7600 Sand Point Way NE   (206) 526-6329   fax<br>

Seattle, WA  98115       (206) 526-6317   main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a>
</div></div>