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

  • Home
  • SEARCH
  • 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 632099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:02:21+00:00 2026-05-13T20:02:21+00:00

I found a c function which I would like to use in my app.

  • 0

I found a c function which I would like to use in my app. Unfortunately, my c knowledge is not great. The first section of code shows the original c code and the second my “translation” to objective c. I have 3 questions I would appreciate help with please:

  1. Is my translation of the variables from their c counterparts to their objective c counterparts valid? (I have had no compiler warnings)
  2. Is it acceptable to use the free () at the end or should this be done in another way in objective c

c code:

unsigned int i, j, diagonal, cost, s1len, s2len;
unsigned int *arr;

char *str1, *str2;

general code...

s1len = strlen(str1);
s2len = strlen(str2);

arr = (unsigned int *) malloc(sizeof(unsigned int) * j);

general code...

free(arr);

objective c code:

NSUInteger i, j, diagonal, cost, s1len, s2len;
NSUInteger *arr;

const char *str1 = [source cStringUsingEncoding:NSISOLatin1StringEncoding];
const char *str2 = [target cStringUsingEncoding:NSISOLatin1StringEncoding];

general code...

s1len = strlen(str1);
s2len = strlen(str2);

arr = (NSUInteger *) malloc(sizeof(NSUInteger) * j);

general code...

free(arr);
  • 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-13T20:02:21+00:00Added an answer on May 13, 2026 at 8:02 pm

    Objective-C is a strict superset of C, therefore you can use any C code without any modifications. I suggest either using the C code as is (as far as possible) or re-implementing the algorithm with objects in Objective-C.

    NSString to char

    What you need to provide, is a way to make convert Objective-C objects into C types, like NSString* to char*.

    The conversion is correct, but you might want to use -UTF8String to keep all chars intact, Latin-1 might lose some information. The disadvantage of utf-8 is, that you’re C code might not be able to correctly work with it.

    You’d better get the lengths using one of NSString’s methods instead of strlen, because it has linear running time and NSString’s methods could be constant.

    // utf-8
    int len = [source length];
    // latin 1
    int len = [source lengthOfBytesUsingEncoding:NSISOLatin1StringEncoding];
    

    int to NSInteger

    There’s no reason to convert ints to NSIntegers. Apple has added this type for 64bit compatibility. NSInteger is typedef’d so that on 32bit platforms it needs 32bit and on 64bit platforms 64bit.

    I’d try to change as little as possible of the C code. (Makes it easier to update it when the original gets updated.) So just leave the ints as they are.

    Memory management

    C’s memory management is more basic than Objective-C’s, but as you seem to know you use malloc and free, that just stays the same. Retain/release and the garbage collector are only useful for objects anyway.

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

Sidebar

Ask A Question

Stats

  • Questions 370k
  • Answers 370k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $("#left_img img").fadeOut(500, function() { $(this).attr("src","http://www.abc.net/image/2_char.gif").fadeIn(500); }); May 14, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer Start Management Studio and Select Database Engine as your SqlExpress… May 14, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer class Account < ActiveRecord::Base has_many :users has_many :transactions end class… May 14, 2026 at 6:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.