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 7566207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:21:15+00:00 2026-05-30T14:21:15+00:00

There is a libx.so which export 2 functions, and a struct , typedef struct

  • 0

There is a libx.so which export 2 functions, and a struct,

typedef struct Tag {
    int num;
    char *name;
}Tag;

Tag *create(int n, char *n)
{
    Tag *t = malloc(sizeof(Tag));
    t->num = n;
    t->name = n;
    return t;
}

void use(Tag *t)
{
    printf("%d, %s\n", t->num, t->name);
}

I want to call create in Python and then save the Tag *res returned by create, later I will call use and pass the Tag *res saved before to use, here is it (just to demonstrate):

>>>libx = ctypes.CDLL("./libx.so")
>>>res = libx.create(c_int(1), c_char_p("a"))
>>>libx.use(res)

The above code might be wrong, just to demonstrate what I want to do.

And my problem is that, how could I save the result returned by create? Because it returns a pointer to a user-defined struct, and I don’t want to construct struct Tag‘s counterpart in Python, would c_void_p do the trick?

UPDATE

From @David’s answer, I still don’t quite understand one thing:

the pointer (c_char_p("a")) is only valid for the duration of the
call to create. As soon as create returns then that pointer is no
longer valid.

And I assign c_char_p("a") to t->name in create, when the call to create finishes, is t->name a dangling pointer? Because according to the quoted words, that pointer is no longer valid after create. Why c_char_p("a") is no longer valid?

  • 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-30T14:21:17+00:00Added an answer on May 30, 2026 at 2:21 pm

    The C code that you present is simply not going to work. You need to be much more precise about which party allocates and is responsible for the heap memory.

    In your current example you pass c_char_p("a") to the C code. However, the pointer to that ctypes memory is only valid for the duration of the call to create. As soon as create returns then that pointer is no longer valid. But you took a copy of the pointer inside create. Thus the subsequent call to use is liable to fail.

    You are going to need to take a copy of the contents of that string and store it in the struct. If you do that then you can use libx.create.restype = c_void_p safely.

    But if you want the memory you allocated to be deallocated you will have to provide a destroy function to match the create function. With these changes the C code would look like this:

    Tag *create(int n, char *s)
    {
        Tag *t = malloc(sizeof(Tag));
        t->num = n;
        t->name = strdup(s);
        return t;
    }
    
    void destroy(Tag *t)
    {
        free(t->name);
        free(t);
    }
    

    The Python code would look like this:

    libx = ctypes.CDLL("./libx.so")
    libx.create.restype = c_void_p
    res = libx.create(c_int(1), c_char_p("a"))
    libx.use(res)
    libx.destroy(res)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are numerous Agile software development methods. Which ones have you used in practice
There are several shading languages available today like GLSL, HLSL, CG, which one to
There's a bug in FireBug: I accidentally clicked where the line numbers are, which
There is a div (form) which opens on the click of a button. There
I have libX which links with libA, and libY which also links with libA.
There is an application written in PHP which I am converting to Ruby. When
Suppose I have a library which contains a bunch of interdependent functions, this library
There are many free online services which provides you with large spaces to store
There is this SQL Statement SELECT t1.Name ,Count(t2.SubID) Totals -- I don't know how
There are few apps like Strava which records users movements using GPS. It also

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.