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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:12:48+00:00 2026-05-28T19:12:48+00:00

I’m learning C and also at the same time attempting to implement a Python

  • 0

I’m learning C and also at the same time attempting to implement a Python C Extension, this works perfectly until I pass it a list that is rather large…

Example..

>>> import shuffle
>>> shuffle.riffle(range(100))

Works Great!

>>> shuffle.riffle(range(1000))
Bus Error: 10

Any ideas as to what my problem is?

#include <Python.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static PyObject *shuffle_riffle(PyObject *self, PyObject *args)
{
    int const MAX_STREAK = 10;
    int m, f, l, end_range, streak, *current_ptr;
    double length;

    PyObject * origList;
    PyObject * shuffledList;
    srand((int)time(NULL));

    // parse args to list
    if (! PyArg_ParseTuple( args, "O!", &PyList_Type, &origList) )
    {
        return NULL;
    }

    length = (int)PyList_Size(origList);
    current_ptr = (rand() % 2) ? &f : &l;
    end_range = (int)(length / 2) + (rand() % (length > 10 ? (int)(.1 * length) : 2));
    shuffledList = PyList_New((int)length);

    for(m = 0, f = 0, l = (end_range + 1), streak = 0; m < length && l < length && f < end_range + 1; m++, *current_ptr += 1)
    {
        double remaining = 1 - m / length;
        double test = rand() / (double)RAND_MAX;

        if (test < remaining || streak > MAX_STREAK)
        {
            current_ptr = (current_ptr == &f ? &l : &f);
            streak = 0;
        }

        PyList_SetItem(shuffledList, m, PyList_GetItem(origList, *current_ptr));
        streak += 1;
    }

    // change the pointer to the one that didn't cause the for to exit
    current_ptr = (current_ptr == &f ? &l : &f);

    while(m < length)
    {
        PyList_SetItem(shuffledList, m, PyList_GetItem(origList, *current_ptr));
        m++;
        *current_ptr += 1;
    }



    return Py_BuildValue("O", shuffledList);

}

static PyMethodDef ShuffleMethods[] = {
    {"riffle", shuffle_riffle, METH_VARARGS, "Simulate a Riffle Shuffle on a List."},
    {NULL, NULL, 0, NULL}
};

void initshuffle(void){
    (void) Py_InitModule("shuffle", ShuffleMethods);
}
  • 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-28T19:12:49+00:00Added an answer on May 28, 2026 at 7:12 pm

    I see three problems with your code.

    First, PyList_GetItem returns a borrowed reference and PyList_SetItem steals the reference which means that you will end up with two lists pointing to the same object but the object’s reference count will be 1 instead of 2. This will definitely cause serious problems down the road (Python will at some point try to delete an already-deleted object).

    Second, you aren’t checking for errors. You should check the return value of all Python calls and if you detect a problem, decref all references that you hold and return NULL.

    For example:

    PyObject *temp = PyList_GetItem(origList, *current_ptr);
    if (temp == NULL) {
        Py_DECREF(shuffledList);
        return NULL;
    }
    

    Then, because of the first problem, you have to incref the reference when setting the item:

    PyList_SET_ITEM(shuffledList, m, temp);
    Py_INCREF(temp);
    

    You can use the PyList_SET_ITEM macro here because you know that the shuffledList is un-initialized yet.

    Third, you’re leaking a reference to the shuffledList object in this line:

    return Py_BuildValue("O", shuffledList);
    

    This is equivalent to:

    Py_INCREF(shuffledList);
    return shuffledList;
    

    Since you already own the reference (because you created this object), you want to return it directly:

    return shuffledList;
    

    Leaking a reference means that this list will never be freed from memory.

    • 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'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.