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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:41:04+00:00 2026-06-13T20:41:04+00:00

In a C extension to my Python program, I am trying to improve performance

  • 0

In a C extension to my Python program, I am trying to improve performance by setting two of the inputs to the main C function (which is called millions of times) as global variables, as they don’t change often, so that I don’t have to keep supplying them from Python using a lambda wrapper to the C function (which wastes a nontrivial amount of time). My code looks like this. First I declare the global variables at the top of the file:

unsigned char* splitArray;
PyObject* wordCmp;

Then the Python API function to set them:

static PyObject* py_set_globals(PyObject *self, PyObject *args)
{
    free(wordCmp);
    free(splitArray);

    char* splitChars;
    PyObject* wordC;

    if (!PyArg_ParseTuple(args, "sO", &splitChars, &wordC))
        return NULL;

    wordCmp = (PyObject*)malloc(sizeof(PyObject));
    memcpy(wordCmp, wordC, sizeof(PyObject));
    splitArray = splitchars_to_mapping(splitChars);

    return Py_BuildValue("");
}

In this case, splitArray is assigned to a 128-character array which is malloc’ed in the function splitchars_to_mapping, and wordCmp is a Python function object that is passed to C. Anyway, as far as I can tell, the char* splitArray works fine as a global variable, but when I later try to call wordCmp using PyEval_CallObject, Python/C crashes. So I have two questions:

  1. Why doesn’t C crash immediately when I try to free the uninitialized pointers wordCmp and splitArray at the beginning of the function?
  2. Why am I unable to call wordCmp later when I have properly stored it on the heap and saved a reference to it as a global?
  • 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-06-13T20:41:05+00:00Added an answer on June 13, 2026 at 8:41 pm

    As to the first question, why it doesn’t crash when free’ing uninitialized global variables, that’s because global (and static) variables are initialized to zero when the program is loaded (guaranteed by the standard) and when you call free() with NULL (or zero) it does nothing.

    From man free(1):

    free(ptr) has already been called before, undefined behavior occurs.
    If ptr is NULL, no operation is performed.

    Edit:
    Your second question is related to fact that you’re trying to copy a PyObject and you shouldn’t do that, as the PyObject structure could contain pointers and you can’t do deep copying because you don’t have access to that structure, you should however increment the reference count and keep the reference around for later use, note that when you use O the reference count is not incremented, from the docs:

    O (object) [PyObject *]
    Store a Python object (without any conversion) in a C object pointer. The C program thus receives the actual object that was
    passed. The object’s reference count is not increased.

    So you should increment the reference yourself, see this example

    static PyObject *my_callback = NULL;
    
    static PyObject* my_set_callback(PyObject *dummy, PyObject *args)
    {
        PyObject *result = NULL;    
        if (PyArg_ParseTuple(args, "O:set_callback", &my_callback)) {
            if (!PyCallable_Check(my_callback)) {
                PyErr_SetString(PyExc_TypeError, "parameter must be callable");
                return NULL;
            }
            Py_XINCREF(my_callback); /* Add a reference to new callback */           
            Py_INCREF(Py_None);      /* Boilerplate to return "None" */
            result = Py_None;
        }
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a little Python extension in C/C++, and I've got a function like
Im attempting to use a C++ extension for Python called PySndObj. and getting an
What I am trying to do in Python, is make a program where you
I have a python program I wrote that I am trying to compile with
I am doing a small program to help me with learning Python (which I
So, I've got a multithreaded python program, which is currently suffering from deadlock. I
For performance reasons I want to port parts of my python program to C++
I wrote a Python extension in C, and my python program uses that extension.
I need to build a chrome extension where I can run the python program?
I have written a module in python which performs some function. I then created

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.