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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:14:51+00:00 2026-05-25T14:14:51+00:00

What is the Best practice to allocate memory for primitive types in objective-c? Is

  • 0

What is the Best practice to allocate memory for primitive types in objective-c?

Is using C allocations is okey (malloc and free)

unsigned int* val = malloc(sizeof(unsigned int));
free(val);

Or Is there any obj-c specific allocations?

And Which is better if a function expects an pointer to int, creating and managing the pointer myself, or just create a regular variable and send its address using the address-of operator:

The first form:

NSScanner* scanner = [NSScanner scannerWithString: @"F"];
unsigned int* val = malloc(sizeof(unsigned int));
[scanner scanHexInt: val];
NSLog(@"%d", *val);
free(val);

The second form:

NSScanner* scanner = [NSScanner scannerWithString: @"F"];
unsigned int  val;
[scanner scanHexInt: &val];
NSLog(@"%d", val);

Myself with the second form to free myself from the alloc-free memory headache.

  • 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-25T14:14:52+00:00Added an answer on May 25, 2026 at 2:14 pm

    Both forms are valid and have pros/cons, but the first one is extra work in this example and has negative performance implications as compared to the second form.

    In the first form, you will be allocating that int on the heap, so malloc’s subsystem will have to perform bookkeeping to make sure there is an allocation available. You will also have to remember to free the allocation at the correct time.

    In the second form, the allocation will be made on the stack, which has much less bookkeeping required than malloc does. It will also automatically be free for you when you leave this function.

    The key time to use malloc/free is if you want the scope of your variable to last outside of the current function. Otherwise, when you leave the function the stack will be popped and you will lose the data.

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

Sidebar

Related Questions

What's the best practice for using a switch statement vs using an if statement
Please best practice and 'how to' for using enum with jpa as a data
What's the best-practice in handling memory in C? There are no classes with constructors/destructors
What's considered best practice writing OOP classes when it comes to using a property
I read that the best practice for using WCF proxy would be: YourClientProxy clientProxy
My coding practice using Qt can best be described as follows: If the Widget
Best practice is to use unique ivs, but what is unique? Is it unique
Short best practice question: If an object A is injected into another object B,
The 'best practice' (as I see it) to atomically create a new file, is
What's the best practice for making sure that certain ajax calls to certain pages

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.