C object allocating objects on the stack

I am new to Objective-C and practicing with objects. Although something like Fraction* f = [[Fraction alloc] init ]; works whenever I try to do Fraction c; I get Interface type cannot be statically allocated . Anyway, to allocate objects on the stack (e.g. in C ++)? or am I trying to make a mistake?

+4
source share
6 answers

You cannot select an object statically in Objective C. There are many reasons for this. Including the fact that objects must be initialized, but initialization methods allow you to change the address of an object.

In C ++, the constructor must initialize the called object and cannot in any way change the address of the object. This is not true in Objective C. The equivalent of constructors (alloc + init * sequence or class level method) allows us to decide that they will change the address of the object to which they are called (they will take care of course, freeing the original object).

There is no way to free statically allocated memory or change the address of your stack, of course.

+3
source

Technically, you can (see code below), but you shouldn't. Also, whether on the stack or heap, you only have a pointer to the object, not the object itself. That is, you should write Fraction *c , not Fraction c .

 // Allocate an Objective-C object on the stack. // Original code By Graham Lee: // https://gist.github.com/iamleeg/5290797 #import <Foundation/Foundation.h> #include <stdlib.h> #include <objc/runtime.h> @interface A : NSObject @property (assign) int meaning; @end @implementation A - (id)init { if ([super init]) { _meaning = 42; } return self; } @end int main(int argc, char *argv[]) { @autoreleasepool { // allocate and zero stack memory size_t size = class_getInstanceSize([A class]); id obj = (__bridge_transfer id) alloca(size); memset((__bridge void*)obj, 0, size); // set class and initialize the object object_setClass(obj, [A class]); obj = [obj init]; NSLog(@"meaning: %d", [obj meaning]); // transfer ownership from ARC to CF so ARC doesn't // try to improperly free the stack allocated memory CFTypeRef ref = (__bridge_retained CFTypeRef) obj; } } 

alloca() is non-standard, unsafe, non portable, and prone to stack overflows.

+2
source

No, all objects are allocated on the heap in Obj-C.

+1
source

No, you cannot use the stack to store an Objective-C object, it must be a bunch (via alloc and init ).

Although, as pointed out by @ wattson12, you can obviously store a reference to an object (pointer) on the stack, it still does not use the stack to memory the object.

+1
source

The error you receive is related to a syntax error that you must execute:

 Fraction *c; 

note the missing * indicating that this is a pointer to an object (you do not store this object on the stack, you just create a link to the object (potential))

0
source

All objective-c objects are allocated on the heap , with the exception of blocks. The main reason for this:

  • pointers to an object are not tracked in Objective-C (instead, objective-c uses a reference-counting system to manage memory); they cannot be updated if you move the object around.
  • the stack of objects will be destroyed when the function that created them is returned.

A message from Mike Ash is useful for understanding this.

0
source

Source: https://habr.com/ru/post/1434176/


All Articles