Re: True object size
Re: True object size
- Subject: Re: True object size
- From: Ofri Wolfus <email@hidden>
- Date: Fri, 16 Mar 2007 16:52:51 +0200
IIRC, class_createInstance() uses calloc() under the hood so using
malloc_size() should get you the right size even if there are extra
bytes. With that being said, I wouldn't use it for anything but
learning purposes as it depends on implementation details that may
(and probably will) change in the future.
- Ofri
- - - - - - - - - - - - - - - - - - -
http://www.dpompa.com
- - - - - - - - - - - - - - - - - - -
On 16/03/2007, at 08:04, Ken Ferry wrote:
Also, there are a few Cocoa objects that override allocWithZone: and
call NSAllocateObject passing a non-zero value for extraBytes. This
means that even the inline contiguous chunk of memory allocated for
the object is larger than you'd expect from looking at the class
definitions.
-ken
On 3/15/07, Ali Ozer <email@hidden> wrote:
That is a dangerous way to compute the size of an arbitrary
object, if
it's really for purposes of making persistent or sending across the
network. The object may have pointers to allocated blocks and other
objects that should be saved along; it may have fields that should
not
be saved; and writing it verbatim will give you problems on other
architectures. On top of this, individual instances might have
additional storage tacked on top of the base instance. You're better
off archiving the object as an NSData and using that, assuming it
supports archiving.
You also say "size on the stack" --- Objective-C objects are not
allocated on the stack; all you have is the pointer which points
at an
allocated block of memory.
Having said this, you can find out the canonical size of instances of
a class by looking at the instance_size field in the class
structure. I wouldn't call this straightforward, and it's not
necessarily a technique that will work in the future.
Ali
On Mar 15, 2007, at 8:06 , R. Tyler Ballance wrote:
> I'm trying to find out the actual size of a custom (or Foundation)
> object in terms of the bytes, in effect, allowing something like
the
> following:
>
> @interface SomeObject : NSObject {
> NSString *someString;
> NSData *someData;
> NSNumber *someNumber;
> }
>
> @end
>
> To where if I did a [Sizer sizeOf:someObject]; it would return
to me
> the number of bytes that the someObject would represent if you were
> to write it out to disk or the network (verbatim).
>
> Obviously, sizeof() is out, but I'm wondering if there's any
> "straightforward" means of finding out the size on the stack that
> any given instantiated object is taking up, any runtime methods I
> can call, etc?
>
>
> Cheers
> R. Tyler Ballance: Lead Mac Developer at bleep. software
> contact: email@hidden | jabber: email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
40gmail.com
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden