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

The Archive Base Latest Questions

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

in an effort to solve question #3367795 here on SO i have to cope

  • 0

in an effort to solve question #3367795 here on SO i have to cope with a number of subproblems. one of these is: in said algorithm (levenshtein distance), several arrays are allocated in memory and initialized with the lines

cdef char   *m1     = <char *>calloc(   blen + 2,    sizeof( char ) )
cdef char   *m2     = <char *>calloc(   blen + 2,    sizeof( char ) )
cdef char   *m3     = <char *>malloc( ( blen + 2 ) * sizeof( char ) )
#.........................................................................
for i from 0 <= i <= blen:
  m2[ i ] = i
  <...snip...>

blen here refers to the length of a Python bytes variable. now as far as i understand the algorithm (see my original post for the full code) and as the code for the initialization of m2 clearly shows, these arrays are meant to hold integer numbers, not characters, so one would think the correct allocations should look like

cdef int    *m3     = <int *>malloc( ( blen + 2 ) * sizeof( int ) )

and so on. can anyone with a background in C elucidate to me why char is used? also, maybe more for people inclined to Cython, why is there a cast <char *>? one would think that char *x = malloc( ... ) should suffice to define x.

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

    Quite simply, to save memory — but please note carefully that declaring these arrays as char limits the result distance to either 127 or 255, depending on whether the C compiler defaults to signed char or unsigned char respectively. In C, char is an integer type — you don’t need an ord() to get its integer value.

    Your original code contains no mention of this limitation. Note that if a char overflows, it does so silently and the code will produce incorrect results — 127 + 1 -> -128 (signed); 255 + 1 -> 0 (unsigned).

    You didn’t respond to my comment on your original question: “””What are the (a) maximum (b) average sizes of your strings? Do you really need to do the whole O(M*N) thing if the two strings are nothing like each other?””” ….. Please answer that now (edit your question); had you done so then, you would have had this question answered then.

    Update: Reading the original post again, I’ve noticed a problem: The code that reads

    m1, m2 = m2, m1
    strcpy( m3, m2 )
    

    is WRONG on three grounds: (1) it doesn’t shuffle the rows properly (should do strcpy() before swapping m1 and m2) (2) strcpy() will not copy anything beyond the first null (zero byte) (3) there is no need to copy anything, just shuffle the pointers

    m3, m2, m1 = m2, m1, m3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have resisted asking this seemingly n00b question, but all my recent effort to
I've solved my problem, but because I have tried and failed to solve it
I have one issue, but I'm not getting a solution. I tried Google too.
Ok, my question's not as broad as it seems, to summarize 8 months effort
I'm having a really tough problem and, in an effort to solve it, I
I am trying to solve a question in Project Euler, which is creating a
Here is a question that has been bugging me for a while, nowadays it's
One of my biggest frustrations with my favorite language is the effort it takes
I have trouble understanding what minimal effort is needed on my part in removing
In an effort to lean Rx I'm writing a simple application which imitates reading

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.