<html><body bgcolor="#FFFFFF"><div>I confess I don't know a lot about C so I may be off base here... But it looks like your c func extendarray returns a pointer to the new extended array, but you are not capturing this pointer in your python when you call the c func. So the python code is pointing at &nbsp;the old deallocated array.</div><div><br></div><div>Does that make sense and/or point you in the right direction?<br><br>-----------------------------<div>Sent from a mobile device with a bad e-mail client.</div><div>-----------------------------</div></div><div><br>On Dec 13, 2010, at 10:27 AM, Sachin Kamboj &lt;<a href="mailto:backslash42@gmail.com">backslash42@gmail.com</a>&gt; wrote:<br><br></div><div></div><blockquote type="cite"><div>
                <div>Hi All,</div><div><br></div><div>I was trying to use the ctypes module for a project. I was creating a dynamically allocated array of "max_entries" pairs and once the array was exhausted, I was creating a new array of size (1.5 * max_entries) and copying the contents from the old array to the new array.&nbsp;</div><div><br></div><div>Unfortunately, when I try to access the contents of this new_array, I get a "NULL pointer access" exception. The corresponding C code seems to work perfectly. (See code below.)</div><div><br></div><div>I was wondering if I was missing something about the way the ctypes module works. Any help would be greatly appreciated. (Not sure if this is the appropriate mailing list for my question.)</div><div><br></div><div>/Thanks!</div><div><br></div><div>---</div><div><br></div><div><font class="Apple-style-span" face="'Courier New'">#!/usr/bin/env python</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">from ctypes import *</font></div><div><font class="Apple-style-span" face="'Courier New'">import math</font></div><div><font class="Apple-style-span" face="'Courier New'">import random</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">class PAIR(Structure):</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;_fields_ = [("a", c_long),</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;("b", c_long)]</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">class MY_ARR(Structure):</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;_fields_ = [("no_entries", c_longlong),</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;("max_entries", c_longlong),</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;("entries", POINTER(POINTER(PAIR)))</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;]</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">def extendArray(x):</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;print "Extending Array"</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;print "Before: %d/%d" % (<a href="http://x.no">x.no</a>_entries, x.max_entries)</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;old_arr = x.entries</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;# Create a new array</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;new_max_entries = int(math.ceil(1.5 * x.max_entries))</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;x.entries = (POINTER(PAIR) * new_max_entries)()</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;# Copy the entries from the old array to the new array</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;for i in range(<a href="http://x.no">x.no</a>_entries):</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;x.entries[i] = old_arr[i]</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;x.max_entries = new_max_entries</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;print "After: %d/%d" % (<a href="http://x.no">x.no</a>_entries, x.max_entries)</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;return x</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">def printPair(x):</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;print x.contents.a, x.contents.b</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">def printArray(x):</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;print "Printing %d/%d Entries" % (<a href="http://x.no">x.no</a>_entries, x.max_entries)</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;for i in range(<a href="http://x.no">x.no</a>_entries):</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;printPair(x.entries[i])</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">if __name__ == "__main__":</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;x = MY_ARR(0, 10, (POINTER(PAIR) * 10)())</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;for i in range(100):</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if <a href="http://x.no">x.no</a>_entries == x.max_entries:</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print "\n\nPrinting Before Extension"</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printArray(x)</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;extendArray(x)</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print "\n\nPrinting After Extension"</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printArray(x)</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;my_pair = PAIR(i, random.randint(0, 100))</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;x.entries[x.no_entries] = pointer(my_pair)</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<a href="http://x.no">x.no</a>_entries += 1</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;printPair(x.entries[i])</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;printArray(x)</font></div><div><br></div><div>---</div><div>Now unfortunately, when I try to run this code, I am getting a "NULL pointer access" exception:</div><div><br></div><div>$ python TestExtension.py&nbsp;</div><div>0 40</div><div>1 40</div><div>2 11</div><div>3 36</div><div>4 82</div><div>5 73</div><div>6 93</div><div>7 100</div><div>8 75</div><div>9 80</div><div><br></div><div><br></div><div>Printing Before Extension</div><div>Printing 10/10 Entries</div><div>0 40</div><div>1 40</div><div>2 11</div><div>3 36</div><div>4 82</div><div>5 73</div><div>6 93</div><div>7 100</div><div>8 75</div><div>9 80</div><div>Extending Array</div><div>Before: 10/10</div><div>After: 10/15</div><div><br></div><div><br></div><div>Printing After Extension</div><div>Printing 10/15 Entries</div><div>Traceback (most recent call last):</div><div>&nbsp;&nbsp;File "TestExtension.py", line 55, in &lt;module&gt;</div><div>&nbsp;&nbsp; &nbsp;printArray(x)</div><div>&nbsp;&nbsp;File "TestExtension.py", line 42, in printArray</div><div>&nbsp;&nbsp; &nbsp;printPair(x.entries[i])</div><div>&nbsp;&nbsp;File "TestExtension.py", line 37, in printPair</div><div>&nbsp;&nbsp; &nbsp;print x.contents.a, x.contents.b</div><div>ValueError: NULL pointer access</div><div><br></div><div>---</div><div><br></div><div>The corresponding C code works perfectly:</div><div><br></div><div><font class="Apple-style-span" face="'Courier New'">#include &lt;stdio.h&gt;</font></div><div><font class="Apple-style-span" face="'Courier New'">#include &lt;stdlib.h&gt;</font></div><div><font class="Apple-style-span" face="'Courier New'">#include &lt;math.h&gt;</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">typedef struct {</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;long a;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;long b;</font></div><div><font class="Apple-style-span" face="'Courier New'">} pair;</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">typedef struct {</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;long long no_entries;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;long long max_entries;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;pair **entries;</font></div><div><font class="Apple-style-span" face="'Courier New'">} my_arr;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">my_arr *extend_array(my_arr *x) {</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;int i;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;pair **old_entries = x-&gt;entries;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;long long new_max_entries = ceil(1.5 * x-&gt;max_entries);</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;printf("Extending Array\n");</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;printf("Before: %lld/%lld\n", x-&gt;no_entries, x-&gt;max_entries);</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;x-&gt;entries = malloc(sizeof(pair *) * new_max_entries);</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;for (i = 0; i &lt; 100; ++i) {</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;x-&gt;entries[i] = old_entries[i];</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;}</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;x-&gt;max_entries = new_max_entries;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;free(old_entries);</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;printf("After: %lld/%lld\n", x-&gt;no_entries, x-&gt;max_entries);</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;return x;</font></div><div><font class="Apple-style-span" face="'Courier New'">}</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">void print_pair(pair *p) {</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;printf("%ld\t%ld\n", p-&gt;a, p-&gt;b);</font></div><div><font class="Apple-style-span" face="'Courier New'">}</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">void print_array(my_arr *x) {</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;int i;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;printf("Printing %lld/%lld entries\n", x-&gt;no_entries, x-&gt;max_entries);</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;for (i = 0; i &lt; x-&gt;no_entries; ++i) {</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;print_pair(x-&gt;entries[i]);</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;}</font></div><div><font class="Apple-style-span" face="'Courier New'">}</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">int main(int argc, char *argv[])</font></div><div><font class="Apple-style-span" face="'Courier New'">{</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;int i;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;my_arr x = {</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;0,&nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;10,</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;malloc(sizeof(pair *) * 10)</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;};</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;for (i = 0; i &lt; 100; ++i) {</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if (<a href="http://x.no">x.no</a>_entries == x.max_entries) {</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;extend_array(&amp;x);</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;pair *my_pair = malloc(sizeof(pair));</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;my_pair-&gt;a = i;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;my_pair-&gt;b = rand() % 100;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;x.entries[x.no_entries++] = my_pair;</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;print_pair(x.entries[i]);</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;}</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;print_array(&amp;x);</font></div><div><font class="Apple-style-span" face="'Courier New'">&nbsp;&nbsp; &nbsp;return 0;</font></div><div><font class="Apple-style-span" face="'Courier New'">}</font></div><div><span id="goog_345597049"></span><span id="goog_345597050"></span><br></div><div><br></div>
    

</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Tutor maillist &nbsp;- &nbsp;<a href="mailto:Tutor@python.org"><a href="mailto:Tutor@python.org">Tutor@python.org</a></a></span><br><span>To unsubscribe or change subscription options:</span><br><span><a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a></span><br></div></blockquote></body></html>