temporary variables storage duration type?

Discussion in 'C++' started by Ivan Novick, Jan 6, 2007.

  1. Ivan Novick

    Ivan Novick Guest

    Is the string below automatic storage duration?

    #include <iostream>
    #include <string>
    int main(int argc, char** argv)
    {
    std::cout << std::string().size() << std::endl;
    return 0;
    }

    Thanks,
    ---
    Ivan
    http://www.0x4849.net
     
    Ivan Novick, Jan 6, 2007
    #1
    1. Advertising

  2. * Ivan Novick:
    > Is the string below automatic storage duration?
    >
    > #include <iostream>
    > #include <string>
    > int main(int argc, char** argv)
    > {
    > std::cout << std::string().size() << std::endl;
    > return 0;
    > }


    Yes, but it may internally use dynamic memory allocation.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 6, 2007
    #2
    1. Advertising

  3. Ivan Novick

    Ivan Novick Guest

    Alf P. Steinbach wrote:
    > * Ivan Novick:
    > > Is the string below automatic storage duration?
    > >
    > > #include <iostream>
    > > #include <string>
    > > int main(int argc, char** argv)
    > > {
    > > std::cout << std::string().size() << std::endl;
    > > return 0;
    > > }

    >
    > Yes, but it may internally use dynamic memory allocation.
    >

    Hmmm... What does that mean?

    -- When does the destructor get called? At the end of the statement?
    When the expression goes out of scope?

    -- Is the memory for this string on the heap or the stack?

    Thank you,
    ----
    Ivan
    http://www.0x4849.net
     
    Ivan Novick, Jan 7, 2007
    #3
  4. * Ivan Novick:
    > Alf P. Steinbach wrote:
    >> * Ivan Novick:
    >>> Is the string below automatic storage duration?
    >>>
    >>> #include <iostream>
    >>> #include <string>
    >>> int main(int argc, char** argv)
    >>> {
    >>> std::cout << std::string().size() << std::endl;
    >>> return 0;
    >>> }

    >> Yes, but it may internally use dynamic memory allocation.
    >>

    > Hmmm... What does that mean?
    >
    > -- When does the destructor get called? At the end of the statement?
    > When the expression goes out of scope?


    An object with automatic storage duration is at the latest destroyed at
    the end of the block it's created in.

    A temporary object persists at least until the end of the
    full-expression it occurs in.

    That is, if I recall correctly -- you shouldn't try to take advantage
    of any particular destruction point, so it's mostly irrelevant when that
    occurs. If you need the object to persist until the end block, then
    declare it as a variable or bind it to a reference to const. And in
    general, when you need control, take control.


    > -- Is the memory for this string on the heap or the stack?


    Automatic storage duration is technically a stack, in the sense that the
    storage lifetimes are (required to be) nested in LIFO order.

    In that sense the object "is on the stack", but beware that C++
    compilers have existed where that stack was not implemented as the usual
    moving stack pointer now in fashion: the C++ standard requires a
    logical stack for a fully conforming implementation, but not necessarily
    a hardware stack pointer or a contiguous memory area designated as stack.

    Whether the object internally makes use of dynamic memory allocation
    (the "heap") or not, is not specified by the standard. It is a quality
    of implementation issue. If you're concerned about efficiency for
    creating an empty string or vector, then measure, Measure, MEASURE.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 7, 2007
    #4
  5. Ivan Novick

    Dizzy Guest

    Alf P. Steinbach wrote:

    > * Ivan Novick:
    >> Alf P. Steinbach wrote:
    >>> * Ivan Novick:
    >>>> Is the string below automatic storage duration?
    >>>>
    >>>> #include <iostream>
    >>>> #include <string>
    >>>> int main(int argc, char** argv)
    >>>> {
    >>>> std::cout << std::string().size() << std::endl;
    >>>> return 0;
    >>>> }
    >>> Yes, but it may internally use dynamic memory allocation.
    >>>

    >> Hmmm... What does that mean?
    >>
    >> -- When does the destructor get called? At the end of the statement?
    >> When the expression goes out of scope?

    >
    > An object with automatic storage duration is at the latest destroyed at
    > the end of the block it's created in.
    >
    > A temporary object persists at least until the end of the
    > full-expression it occurs in.


    With the exception that a temporary to which a reference (to const) is bound
    will exist as long as the reference exists (of course the reference itself
    has to be in the same exact scope as the code creating the temporary).

    Thus:
    MyClass const& ref = getMyClassInstance();
    // where getMyClassInstance() returns by value, ie a temporary

    The compiler will make sure that the temporary exists as long as ref exists.

    --
    Dizzy
    http://dizzy.roedu.net
     
    Dizzy, Jan 7, 2007
    #5
  6. * Dizzy:
    > Alf P. Steinbach wrote:
    >> An object with automatic storage duration is at the latest destroyed at
    >> the end of the block it's created in.
    >>
    >> A temporary object persists at least until the end of the
    >> full-expression it occurs in.

    >
    > With the exception that a temporary to which a reference (to const) is bound
    > will exist as long as the reference exists (of course the reference itself
    > has to be in the same exact scope as the code creating the temporary).
    >
    > Thus:
    > MyClass const& ref = getMyClassInstance();
    > // where getMyClassInstance() returns by value, ie a temporary
    >
    > The compiler will make sure that the temporary exists as long as ref exists.


    I think that was summarized in the immediately following part of my
    posting that you elided,

    <q>
    That is, if I recall correctly -- you shouldn't try to take advantage
    of any particular destruction point, so it's mostly irrelevant when that
    occurs. If you need the object to persist until the end block, then
    declare it as a variable or bind it to a reference to const.
    </q>

    Probably you didn't see that.

    For just about any C++ subject one would care to pick, any explanation
    in a Usenet posting will always fall short of being correct when
    compared to the standard's full treatment, except if it quotes in full
    all that's relevant from the standard (which may be all of it). In this
    case the detail you noted was actually not left out, but covered in the
    posting. But there are more such details, there always is, e.g.

    - For full accuracy the above simplified explanation has to make an
    exception for binding the temporary via a return statement (not a
    good idea).

    - For another example, a class type object ceases to exist (as an
    object, not as a region of memory) if you explicitly call the
    destructor (not a good idea).

    - And for a third example, take the situation where you bind the
    temporary to a reference to const member (not a good idea!), like
    T(int const& x = 666): m(x){} where x may refer to a temporary.

    Cheers,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 7, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adrian

    Dynamic storage duration

    Adrian, Apr 10, 2007, in forum: C++
    Replies:
    9
    Views:
    380
    Adrian
    Apr 11, 2007
  2. Replies:
    1
    Views:
    303
    Victor Bazarov
    Jul 26, 2007
  3. Victor Bazarov
    Replies:
    9
    Views:
    322
  4. Replies:
    5
    Views:
    581
    Chris Torek
    May 8, 2008
  5. Giacomo Catenazzi

    scope, linkage and storage duration

    Giacomo Catenazzi, Nov 4, 2008, in forum: C Programming
    Replies:
    1
    Views:
    419
Loading...

Share This Page