-1

I'm new to obj-c . During these day's practices I noticed that every class based on NSObject can't have an entity like : NSObject en; in c++ but NSObject* en instead. But, sometimes I need to know the Size of an Object.I can't simply write sizeof(en) because en is a pointer var.I can't simply use sizeof(NSObject) neither for the compiler telling me Application of sizeof to interface 'XXXX' is not supported on this architecture and platform.

I want to know if there is a way to get sizeof(NSObject) .If not,what the syntax is designed this for & any other ways to get the size.

AntiMoron
  • 1,286
  • 1
  • 11
  • 29
  • 3
    Why do you need to know the size of an NSObject? – Hot Licks Aug 05 '14 at 02:22
  • Understand that you can't create a `[]` array of Objective-C objects, nor can you allocate/free them outside of the standard framework. And the "size" of an object may only be a small header, with the "guts" of the object in other allocations. You really can't even *define* "size", much less make effective use of it. – Hot Licks Aug 05 '14 at 02:38

2 Answers2

1

From doc

class_getInstanceSize

Returns the size of instances of a class.

size_t class_getInstanceSize(Class cls)

Parameters cls A class object. Return Value The size in bytes of instances of the class cls, or 0 if cls is Nil.

But I doubt this is what you really want. Because I never found it useful and can't think a case it may be useful. (other than learning memory layout of objects and low level implementation details)

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
0

First, you should import malloc.h

If you use Non-ARC:

malloc_size(myObject);

if you are using ARC:

malloc_size((__bridge const void *) myObject));

This linker is a question similar to yours.

Community
  • 1
  • 1
simalone
  • 2,768
  • 1
  • 15
  • 20