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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:30:28+00:00 2026-06-16T04:30:28+00:00

I have function to convert an integer into byte array (for iPhone). To add

  • 0

I have function to convert an integer into byte array (for iPhone). To add dynamicity I have allocate the array using malloc. But I think this will leak memory. What’s best way to manage this memory,

+ (unsigned char *) intToByteArray:(int)num{
    unsigned char * arr = (unsigned char *) 
                          malloc(sizeof(num) * sizeof(unsigned char));
    for (int i = sizeof(num) - 1 ; i >= 0; i --) {
        arr[i] = num & 0xFF;
        num = num >> 8;
    }
    return arr;
}

When calling,

int x = 500;
unsigned char * bytes = [Util intToByteArray:x];

I want to avoid the call free(bytes) since, the calling function do not know or explicitly knows, the memory is allocated and not freed.

  • 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-16T04:30:29+00:00Added an answer on June 16, 2026 at 4:30 am

    A few things:

    1. The char type (and signed char and unsigned char) all have a size of 1 by definition, so sizeof(unsigned char) is unnecessary.

    2. It looks like you just want to get the byte representation of an int object, if this is the case, it is not necessary to allocate more space for it, simply take the address of the int and cast it to a pointer to unsigned char *. If the byte order is wrong you can use the NSSwapInt function to swap the order of the bytes in the int and then take the address and cast to unsigned char *. For example:

      int someInt = 0x12345678;
      unsigned char *bytes = (unsigned char *) &someInt;
      

      This cast is legal and reading from bytes is legal up until sizeof(int) bytes are read. This is accessing the “object representation”.

    3. If you insist on using malloc, then you simply need to pass the buffer to free when you are done, as in:

      free(bytes);
      
    4. The name of your method does not imply the correct ownership of the returned buffer. If your method returns something that the caller is responsible for freeing, it is conventional to name the method using new, copy, or sometimes create. A more suitable name would be copyBytesFromInt: or something similar. Otherwise you could have the method accept a pre-allocated buffer and call the method getBytes:fromInt:, for example:

      + (void) getBytes:(unsigned char *) bytes fromInt:(int) num
      {
          for (int i = sizeof(num) - 1 ; i >= 0; i --) {
              bytes[i] = num & 0xFF;
              num = num >> 8;
          }
      }
      
    5. You could wrap your bytes into a NSData instance:

      NSData *data = [NSData dataWithBytesNoCopy:bytes length:sizeof(num) freeWhenDone:YES];
      

      Make sure your method follows the usual object ownership rules.

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

Sidebar

Related Questions

i have a function to convert into NSDate by using NSDateFormatter . +(NSDate*)getDateFromCurrentTimestamp:(NSString*)currentTimestamp{ //
I have this function in C# to convert a little endian byte array to
I have an array of byte that I wish to convert into a hex
I have the need to convert a hex string into an integer using C#.
I have a function in Java which I need to convert into php. It
I have two php variables, one integer and other json, which I convert into
I have one function in oracle i need to convert it into c# code
I have a function that convert ticks to time_t format long ticks = DateTime.Now.Ticks;
I have the following function to convert a point on the same plane as
I have my class public function convert( $title ) { $nameout = strtolower( $title

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.