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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:23:43+00:00 2026-05-24T17:23:43+00:00

Is it possible to alloc space for objects on the stack? I don’t want

  • 0

Is it possible to alloc space for objects on the stack? I don’t want to contest
the language principles, and yes, auto-release pools work very nicely. It’s
just a matter of curiosity (and of investigating it). I’ve heard of things
like:

struct {
    Class isa;
} s_obj;

s_obj.isa = [NSObject class];

NSObject *obj = (NSObject *)&s_obj;

But I’m not sure if this will work properly. At least, it doesn’t sound very
practical since the structure of the object would need to be re-created.

Maybe overwriting +alloc, and somehow calling an alloca() behind the
scenes? Looks like passing a different zone in allocWithZone: would be the
best approach, but NSZone is quite undocumented. CocoaDev has an article
on it, but again I’m not sure if it’s the way to go. Any ideas?

Eventually, if an easy to use solution appears it would be nice to use it in
addition to the already present language features.

  • 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-24T17:23:43+00:00Added an answer on May 24, 2026 at 5:23 pm

    Nope; can’t do that. Or, technically, you can, but you cannot pass the object to any system API and you can’t subclass NSObject.

    The system assumes that the object is in the heap; autorelease will work as expected, and the object’s lifespan can be controlled by managing a retained reference, none of which will be true for a stack allocated object.

    (There is one exception; Blocks start out on the stack sometimes. Very special case and it is entirely controlled by the compiler. It also trips people up.)


    alloca() won’t work; the memory will be “allocated” (the stack pointer will be bumped) in the frame that did the allocation. As soon as you try to return the object, the allocation is gunk and the returned reference is now a dangling pointer.

    NSZone is deprecated in all but declaration. OpenStep tried to perpetuate zone based allocations for the purpose of quick destruction back in 1994 or so. Proved to be way too fragile; there were way too many ways that a reference to a zone could escape into another zone and zone destruction would produce dangling pointers.


    Capturing what was in comments on Daniel’s question

    • Once the stack frame containing a stack allocated object is destroyed, any references to that object are invalid. Thus, any messages to any references will have undefined behavior and will, quite likely and at best, crash outright. I.e. you can’t set the retain count to some super high value because the call to release is already too late in that it will dereference through the now-invalid object reference to try and obtain the isa to try and execute the method.

    • You cannot subclass NSObject in such an object because NSObject carries with it a boatload of functionality, both public like KVO, KVC, and associated objects, and various private implementation details of the system frameworks. Any of this functionality should be free to retain/release/autorelease the instance at any time because a fundamentally a retain guarantees an object’s lifespan until release when subclassing NSObject.

    • You can’t test for “working” behavior because any assumptions related to retain/release/autorelease of the system provided frameworks are inherently implementation details that may change across any software update for any reason.

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

Sidebar

Related Questions

Is it possible to prevent stack allocation of an object and only allow it
Is this possible? I just want a small table in my current view... here
-(void)CallingView2{ SettingsViewController *aSettingsView = [[SettingsViewController alloc] initWithNibName:@Settings bundle:nil]; [self setSettingsViewController:aSettingsView]; [aSettingsView release]; [UIView beginAnimations:nil
I want to get data from a DMA enabled, PCIe hardware device into user-space
Is it possible to anchor a control to another control? Lets say i want
Is it possible to allow embed, object, and param HTML tags with HTMLPurifier? I'm
Is it possible to allow users to drag and drop items in mobile safari?
How is it possible to allow a user to create a webpage containing some
Is it possible to allow a user to select text in a silverlight text
Is it possible for a website to allow users to log in via multiple

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.