• 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: Darwin: Weird call to structure constructor in formatter.h
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Darwin: Weird call to structure constructor in formatter.h


  • Subject: Re: Darwin: Weird call to structure constructor in formatter.h
  • From: Marshall Clow <email@hidden>
  • Date: Sun, 4 Feb 2007 10:43:13 -0800

At 6:16 PM +0000 2/4/07, Daniel Stenning wrote:
So are you saying that one can instantiate that structure via the
constructor WITHOUT  using the new keyword ?

Yes.

Out of curiositiy , why would one do that ?

1) To avoid having to allocate memory on the heap.
2) To take advantage of C++'s static lifetime rules - otherwise known as "scoping".


The line:
>> _M_parameters[_M_num_parameters++] = _Parameter(__value, __name);

will be compiled into the following actions:
1) Allocate enough space on the stack for a variable of type _Parameter (note #1: this
may occur on entry to the function. Note #2: This is an 'anonymous' or 'temporary' variable,
it cannot be referred to by name, since it has no name - unlike, say "_M_num_parameters" )


A more familiar example might be:
someArray [ someIndex ] = 1.0 * 23;
Clearly, somewhere, there is a variable with the value 23. you can't use it, because it
has no name.


2) Call _Parameter::_Parameter ( const char *, const char * ) to create a _Parameter
object using that memory.


3) Call _M_parameters[_M_num_parameters]'s assignment operator with the temporary
variable as the parameter, thereby assigning into the array.


4) Increment _M_num_parameters.

5) Call _Parameter::~_Parameter () on the temporary object that was constructed
(and then assigned from).

General notes:
1) This code is exception safe, unless _M_num_parameters operator++ throws. In no
other case will the data structure be left in an inconsistent state, nor will there be a
memory leak. If the constructor (step #2) succeeds, then the destructor (step #5) will
always be called.


2) I believe that the compiler can choose to delay the destruction of the temporary variable until the end of the enclosing scope. There's a lot of stuff in the C++ standard about the lifetime of temporary objects. (search for sequence points if you wish to learn more).

3) There are some specific optimizations that the compiler can perform under some circumstances,
depending on how complicated the _Parameter object is.
--
-- Marshall


Marshall Clow     Idio Software   <mailto:email@hidden>

It is by caffeine alone I set my mind in motion.
It is by the beans of Java that thoughts acquire speed,
the hands acquire shaking, the shaking becomes a warning.
It is by caffeine alone I set my mind in motion.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: Darwin: Weird call to structure constructor in formatter.h (From: Daniel Stenning <email@hidden>)

  • Prev by Date: Re: Apple Bug Reporter - was Re: XCode dependency handling
  • Next by Date: Token Concatation in Info.plist Files?
  • Previous by thread: Re: Darwin: Weird call to structure constructor in formatter.h
  • Next by thread: Re: Darwin: Weird call to structure constructor in formatter.h
  • Index(es):
    • Date
    • Thread