• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Declaring Variables - Setting to nil VS Not Setting?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Declaring Variables - Setting to nil VS Not Setting?


  • Subject: Re: Declaring Variables - Setting to nil VS Not Setting?
  • From: Nick Zitzmann <email@hidden>
  • Date: Thu, 26 Apr 2012 14:33:09 -0600

On Apr 26, 2012, at 2:00 PM, Chris Tracewell wrote:

> I usually use the sample in line 1 when declaring vars inside my methods.
>
> NSString *theString = [NSString string];

Here you are creating a pointer to an empty string. This consumes memory.

> NSString *theString = nil;

Here you are creating a null pointer. This does not consume memory. If the program sends a message to a null pointer, then the runtime will catch it and not send the message.

> NSString *theString;

Here you are declaring an uninitialized pointer. This does not consume memory, but attempting to message it prior to initialization will either cause a crash if it points to an unallocated memory address, or cause bizarre behavior if it points to an allocated memory address.

> when do you use the style shown in line 3?

Only when you will store something in that pointer at a later time in the function/method. Never read from uninitialized memory unless you **really** know what you're doing.

> Thanks and pointers to docs are welcome.


This is C programming language 101, so I'd recommend you read a good book on C programming, particularly one that deals with pointers.

Nick Zitzmann
<http://www.chronosnet.com/>


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden

References: 
 >Declaring Variables - Setting to nil VS Not Setting? (From: Chris Tracewell <email@hidden>)

  • Prev by Date: Re: Declaring Variables - Setting to nil VS Not Setting?
  • Next by Date: Re: Declaring Variables - Setting to nil VS Not Setting?
  • Previous by thread: Re: Declaring Variables - Setting to nil VS Not Setting?
  • Next by thread: Re: Declaring Variables - Setting to nil VS Not Setting?
  • Index(es):
    • Date
    • Thread