On 25 Jul 2009, at 03:56, Uli Kusterer wrote:
I don't think you can use sizeof() on a pointer passed as a parameter, though. That only works on variables in the current scope. That pointer could point at anything, so the compiler has no idea whether it points at the stack or the heap. Even if it generated specialized code for the stack case, what code would it generate for the heap case? sizeof() is independent from malloc(), NewPtr() and all the other allocator schemes the OS knows, sizeof() wouldn't know which one to use.

sizeof() does exactly one thing, which is return the amount of storage allocated *by the compiler* for its argument. Despite appearances, it is not a function: it's a compiler directive, and compiles to a constant value. For example:

  char charArray[24];
  sizeof(charArray) = 24

  int intArray[16];
  sizeof(intArray) = 64

  char *charArrayPointer = charArray;
  int *intArrayPointer = intArray;
sizeof(charArrayPointer) = sizeof(intArrayPointer) = 4 (8 on 64-bit builds)

What's happened here? Taking the sizeof() a pointer doesn't tell you anything about the size of the object it's pointing to - all it can tell you is how big *the pointer itself* is.

You can dereference a pointer in a sizeof() expression, but only in a limited fashion:

  sizeof(*charArrayPointer) = 1
  sizeof(*intArrayPointer) = 4

Confused? Keep in mind that *charArrayPointer is equivalent to charArrayPointer[0] - it returns the first element, not the whole original array.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please 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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to