By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,109 Members | 1,008 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,109 IT Pros & Developers. It's quick & easy.

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

P: n/a
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::string 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("ab"), &std::string("cd"),
&std::string("ef") } , 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::string* arr[], int n)
{

// possible memory allocations etc...

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

Thanks for advice!
-Albert
Feb 11 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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("ab"), &std::string("cd"),
&std::string("ef") } , 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.


Feb 11 '08 #2

P: n/a
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::string 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
Feb 12 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.