Re: True object size
Re: True object size
- Subject: Re: True object size
- From: Ali Ozer <email@hidden>
- Date: Thu, 15 Mar 2007 21:01:18 -0700
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