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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:18:16+00:00 2026-05-25T01:18:16+00:00

I am writing some code and I am getting a strange error: my for

  • 0

I am writing some code and I am getting a strange error: my for loop does not seem to exit when the condition statement becomes false. The code is as follows:

static void wstrcpy_from_Py_UNICODE(Py_UNICODE *inBuf, Py_ssize_t strLength, wchar_t **outBuf)
{
    if (strLength == 0) *outBuf = NULL;
    else
    {
        Py_ssize_t i;
        wprintf(L"String Length: %d\n", strLength);
        *outBuf = (wchar_t *)malloc (sizeof (wchar_t) * (strLength +1));
        for (i=0; i < strLength; i++)
        {
            wprintf("i:%d, strLength:%d\n", i, strLength);
            (*outBuf)[i] = (wchar_t)(inBuf[i]);
            wprintf(L"i < strLength: %d\n\n", i < strLength);
        }
    /* Make sure new string is zero terminated */
    (*outBuf)[i] = L'\0';
    }
}

When running this code with an example input, (The Py_UNICODE * buffer points to the internal unicode python object made with u”example”) I get the following output:

String Length: 7
i:0, strLength: 7
i < strLength: 1

i:1, strLength: 7
i < strLength: 1

i:2, strLength: 7
i < strLength: 1

i:3, strLength: 7
i < strLength: 1

i:4, strLength: 7
i < strLength: 1

i:5, strLength: 7
i < strLength: 1

i:6, strLength: 7
i < strLength: 1

i:7, strLength: 7
i < strLength: 1

i:8, strLength: 7
i < strLength: 1

...

The loop doesn’t exit until the python interpreter the code is running from (I am wrapping a c module for python) crashes.

The printf’s were put in for debugging.

I am compiling this on Mac OSX 10.6, here are the commands I am using to compile:

gcc -c source.c -I/usr/include/python2.6 -I/usr/lib/python2.6
ld -bundle -flat_namespace -undefined suppress -o out.so source.o -F./ -framework some_framework -macosx_version_min 10.6 -rpath ./

As you can see I am linking in the framework that I am making the python wrapper for. This is not a problem as I can call the functions just fine that use the linked framework, just when I call the function that uses the helper function shown above do I get the problem.

Am I being stupid here and doing something very basic wrong or is something wrong with the compiler? Any help would be much appreciated!

  • 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-25T01:18:16+00:00Added an answer on May 25, 2026 at 1:18 am

    I think this mostly have to do with number precission. If Py_ssize_t is a 64 bit type, it may be of the form: 0xffffffff00000008 (maybe because a previous calculation value that involves incorrect precision numbers, or mixing signed with unsigned calculations). When considered as an int (32 bits), its result is 8, but when considered as a 64 bit value, it yields a very small negative number (signed), or a very big positive number (unsigned). Try to change the wprintf expressions to write a long decimal (%ld) and see what get’s printed, or debug your code with gdb to see the number in its real size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm writing some code for getting some values including course -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
I'm having a weird error with some Google App Engine code I'm writing. My
I'm writing some code that does date and time calculations against the current time.
I'm writing some code in VB.Net which I hope demonstrate to colleagues (not to
I am writing some code to determine whether a network domain is registered. For
So I was writing some code today that basically looks like this: string returnString
I am writing some code and trying to speed it up using SIMD intrinsics
I'm writing some code for a class constructor which loops through all the properties
I'm writing some code in python and I'm having trouble when trying to retrieve
I'm writing some code that id like to be able to work with any

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.