anonymous array of strings // ("taking address of temporary"- how long is temporary valid?)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anon.asdf@gmail.com

    #1

    anonymous array of strings // ("taking address of temporary"- how long is temporary valid?)

    Hello!

    1) =============== =============== =
    When trying to define an array of std::string ...

    func( (std::string []) { std::string("ab "), std::string("cd "),
    std::string("ef ") } , 3 );

    ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
    ....g++ tells me: "invalid use of non-lvalue array"
    Can the above be modified to work?

    void func(std::strin g arr[], int n)
    {
    while ((--n) >= 0) {
    std::cout << arr[n];
    }
    }


    2)============= =============== ====

    When trying to work with an array of std::string-pointers ...

    func2( (std::string* []) { &std::string("a b"), &std::string("c d"),
    &std::string("e f") } , 3 );
    //
    ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^
    // ~~ nextline

    .... g++ reports:
    "taking address of temporary" (reported 3 times)
    "invalid use of non-lvalue array".

    When are the 3 temporary addresses in danger of being "corrupted" ? Is
    it during the execution of func2, or is it only after func2, i.e.
    at // ~~ nextline

    How can the error be fixed?

    void func2(std::stri ng* arr[], int n)
    {

    // possible memory allocations etc...

    while ((--n) >= 0) {
    std::cout << *(arr[n]);
    }
    }



    Thanks for advice!
    -Albert
  • Martin York

    #2
    Re: anonymous array of strings // (&quot;taking address of temporary&quot; -how long is temporary valid?)

    On Feb 11, 7:06 am, anon.a...@gmail .com wrote:
    Hello!
    >
    1) =============== =============== =
    When trying to define an array of std::string ...
    >
    func( (std::string []) { std::string("ab "), std::string("cd "),
    std::string("ef ") } , 3 );
    Perl programmer are we? :-)

    Thats hard to read and I am not sure it would be valid syntax. It
    looks like a comma expression to me, I don't think you can use the
    array initializer like that (somebody with a copy of the standard will
    give you the exact problem). Also the fact that you are using a C
    style cast is an indication that something is not good with the code.

    A simple solution would be:

    std::string[] temp = {"ab","cd","ef" };
    func(temp,3);



    func2( (std::string* []) { &std::string("a b"), &std::string("c d"),
    &std::string("e f") } , 3 );
    //
    ... g++ reports:
    "taking address of temporary" (reported 3 times)
    "invalid use of non-lvalue array".

    Temporary objects are constant and only last as long as the statement.
    Thus taking there address is probably not a good idea that is why the
    compiler is generating a "WARNING". Also again I don't think you are
    actually creating an array. Using a cast like that forces the type to
    be something without checking. The compiler is not adding any code to
    do the conversion.

    When are the 3 temporary addresses in danger of being "corrupted" ? Is
    it during the execution of func2, or is it only after func2, i.e.
    at // ~~ nextline
    The temporaries are valid until the end of the statement they are
    created in (except in funny situations that don't arise here). So they
    will be valid until after func2() returns.




    Comment

    • anon.asdf@gmail.com

      #3
      Re: anonymous array of strings // (&quot;taking address of temporary&quot; -how long is temporary valid?)

      On 11 Feb., 16:06, anon.asdf wrote:
      Hello!
      >
      1) =============== =============== =
      When trying to define an array of std::string ...
      >
      func( (std::string []) { std::string("ab "), std::string("cd "),
      std::string("ef ") } , 3 );
      >
      void func(std::strin g arr[], int n)
      {
      while ((--n) >= 0) {
      std::cout << arr[n];
      }
      >
      }
      Interestingly, the following works fine:

      //=============== =============== =====
      #include <iostream>

      void func3(char* arr[], int n);

      int main(void)
      {
      func3((char* []){"ab", "cd", "ef"}, 3);
      return 0;
      }

      void func3(char* arr[], int n)
      {
      while ((--n) >= 0) {
      std::cout << arr[n];
      }
      std::cout << std::endl;
      }
      //=============== =============== =====

      Can it be similarly adapted for std::string, without naming an array
      variable?

      -Albert

      Comment

      Working...