Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7585531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:11:56+00:00 2026-05-30T19:11:56+00:00

I’m trying to interop with C in Python, and I have the following function

  • 0

I’m trying to interop with C in Python, and I have the following function prototype:

int testout(unsigned char *test, int *size)

The size of the unsigned char is determined by the size parameter. So I need a way to resize the buffer in Python dynamically to adjust to the size returned from the function call. This has been very difficult for me to figure out.

From the Python ctypes manual:

“This is nice and fine, but how would one access the additional elements contained in this array? Since the type still only knows about 4 elements, we get errors accessing other elements:

>>> short_array[:]
[0, 0, 0, 0]
>>> short_array[7]
Traceback (most recent call last):
...
IndexError: invalid index
>>>

Another way to use variable-sized data types with ctypes is to use the dynamic nature of Python, and (re-)define the data type after the required size is already known, on a case by case basis.”

Great! I’m getting IndexError:invalid index, just as the example shows. And they decided not to show how to do this properly! 🙂

Does anyone know how to resize a ctype properly?

Here is my example code that works, except for the resizing:

from ctypes import *

lib = "test.so"
dll = cdll.LoadLibrary(lib)
print "Testing pointer output"
dll.testout.argtypes = [POINTER(c_ubyte), POINTER(c_int)]
sizeout = c_int(0)
mem = (c_ubyte * 20)() 
dll.testout(mem, byref(sizeout))
print "Sizeout = " + str(sizeout.value)
for i in range(0,sizeout.value):
    print "Item " + str(i) + " = " + str(mem[i])
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:11:57+00:00Added an answer on May 30, 2026 at 7:11 pm

    Here’s an example of what we were discussing. This code expects the caller to allocate the buffer.

    Test code (Windows)

    // IN: test=pre-allocated buffer.
    // IN: size=size of pre-allocated buffer.
    // RETURNS: 0 and size set to required size.
    //          1 and size set to size used.
    __declspec(dllexport) int testout(void *pdata, int *psize)
    {
        unsigned char * p = (unsigned char *)pdata;
    
        if (*psize < 5)
        {
            *psize = 5; // set size required
            return 0;  // fail
        }
    
        p[0] = 123;
        p[1] = 255;
        p[2] = 237;
        p[3] = 12;
        p[4] = 222;
        *psize = 5; // indicate size used
    
        return 1;  // pass
    }
    

    Python

    from ctypes import *
    
    test = CDLL('test')
    size = c_int(0)
    test.testout.argtypes=[c_void_p,POINTER(c_int)]
    print "Result of NULL pointer and zero size:",test.testout(None,byref(size))
    print "Returned size:",size.value
    mem = (c_ubyte * 2)()
    size = c_int(sizeof(mem))
    print "sizeof(mem):",size.value
    print "Result of small buffer:",test.testout(byref(mem),byref(size))
    print "Returned size:",size.value
    mem = (c_ubyte * size.value)()
    print "Result of exact size buffer:",test.testout(byref(mem),byref(size))
    print "Returned size:",size.value
    for i in range(size.value):
        print mem[i]
    mem = (c_ubyte * 20)()
    size = c_int(20)
    print "Result of bigger buffer:",test.testout(byref(mem),byref(size))
    print "Returned size:",size.value
    for i in range(size.value):
        print mem[i]
    

    Output

    Result of NULL pointer and zero size: 0
    Returned size: 5
    sizeof(mem): 2
    Result of small buffer: 0
    Returned size: 5
    Result of exact size buffer: 1
    Returned size: 5
    123
    255
    237
    12
    222
    Result of bigger buffer: 1
    Returned size: 5
    123
    255
    237
    12
    222
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.