Returning structures

K

kelvSYC

What is the best way to create functions that, based on some input,
return either structures or a null value if the structure cannot be
made?

The problem is that the structure has be created inside the function,
and I've heard that returning a pointer to a locally created structure
is unsafe.
 
E

E. Robert Tisdale

kelvSYC said:
What is the best way to create functions that, based on some input,
return either structures or a null value
if the structure cannot be made?

The problem is that the structure has be created inside the function,
and I've heard that returning a pointer to a locally created structure
is unsafe.

struct X { /* . . . */ };

X f(void) {
X x;
// modify x
return x;
}

This is the safest *and* most efficient way.
You can invoke it like this:

X x = f();

and *no* copies will be required if your C++ compiler implements
the Named Return Value Optimization (NRVO).
 
V

Victor Bazarov

kelvSYC said:
What is the best way to create functions that, based on some input,
return either structures or a null value if the structure cannot be
made?

The problem is that the structure has be created inside the function,
and I've heard that returning a pointer to a locally created structure
is unsafe.

Correct. But returning a pointer to a dynamically created structure
is perfectly fine. Just make sure that the caller disposes of it
correctly when they don't need it any longer.

SomeStruct* func() {
...
SomeStruct* toreturn = 0;
if (everything_is_OK)
toreturn = new SomeStruct(arguments);
...
return toreturn;
}

void foo() {
SomeStruct* pSS = func();
if (pSS) {
// do something
delete pSS;
}
}

Victor
 
V

Victor Bazarov

E. Robert Tisdale said:
struct X { /* . . . */ };

X f(void) {
X x;
// modify x
return x;
}

This is the safest *and* most efficient way.
You can invoke it like this:

X x = f();

and *no* copies will be required if your C++ compiler implements
the Named Return Value Optimization (NRVO).

What about the requirement to return something different (like 0)
to indicate the object creation is impossible?

Do you actually read the posts you reply to?

V
 
E

E. Robert Tisdale

Victor said:
What about the requirement to return something different (like 0)
to indicate the object creation is impossible?

Do you actually read the posts you reply to?

Of course I do.
And I read your response which answered kelvSYC's question very well.

But I think that I gave the OP better advice.
 
C

Chris Mantoulidis

kelvSYC said:
What is the best way to create functions that, based on some input,
return either structures or a null value if the structure cannot be
made?

The problem is that the structure has be created inside the function,
and I've heard that returning a pointer to a locally created structure
is unsafe.

Then don't return a pointer;

struct SimpleStruct {
bool valid;
string name;
//add more
}

SimpleStruct function_returns_ss(const string &name) {
SimpleStruct dummy;
if (name == "") dummy.valid = false;
else {
dummy.name = name;
dummy.valid = true;
}

return dummy;
}

int main() {
string name_given;

//prompt for name
cout << "Name: ";
cin >> name_given;

SimpleStruct rslt;
rslt = function_return_ss(name_given);
switch (rslt.valid) {
case false: cout << "no name...\n"; break;
case true: //..... do stuff.........
}

//.... do other stuff......

return 0;
}

This is the first solution that came to my mind. I know there's a
better one but it's too early for me to think of one ;)
 
S

Siemel Naran

What is the best way to create functions that, based on some input,
return either structures or a null value if the structure cannot be
made?

The problem is that the structure has be created inside the function,
and I've heard that returning a pointer to a locally created structure
is unsafe.

Of course the easy solution is to document that your function may return a
new object, and that the caller ought to delete the object through delete,
free, or whatever is appropriate.

But you'll still get memory leaks as your program grows, because sooner or
later someone will screw up and forget to delete something.

So consider using a smart pointer like boost::shared_ptr or std::auto_ptr.
There's lots written about smart pointers. Search this newsgroup or the
internet, buy a book, etc.

And finally, you could return an object, and throw an exception to indicate
failure. Make sure to document the fact that your class may throw this or
that exception, but preferrably without exception specifications.
 
S

Siemel Naran

E. Robert Tisdale said:
X f(void) {
X x;
// modify x
return x;
}

This is the safest *and* most efficient way.
You can invoke it like this:

X x = f();

and *no* copies will be required if your C++ compiler implements
the Named Return Value Optimization (NRVO).

Most compilers don't support this optimization at the present time. But one
could always move the body of f() and its function arguments, if any, into
the constructor X::X().
 
E

Eric Entressangle

kelvSYC said:
What is the best way to create functions that, based on some input,
return either structures or a null value if the structure cannot be
made?

The problem is that the structure has be created inside the function,
and I've heard that returning a pointer to a locally created structure
is unsafe.

Well, you could for example create your structure outside the
function, and pass it as out parameter to the function with your
inputs.

boolean function (struct & struct, input1, input2, ...)
{
//There, fill in struct with inputs
....
}

If the structure cannot be made, simply return true (or false, as
preferred).
 
A

anubis

Eric said:
Well, you could for example create your structure outside the
function, and pass it as out parameter to the function with your
inputs.

boolean function (struct & struct, input1, input2, ...)
{
//There, fill in struct with inputs
...
}

If the structure cannot be made, simply return true (or false, as
preferred).

Actually, this solution is the best for me for these reasons :

- Having news and deletes in different scopes is very hard to maintain.
- Having a dynamic creation as it may not be required is bad ©.

With this solution, you could make a new or a static creation (as you
wish) and let the function "build" your structure. Then you could delete
it when job is done.

OT: And boolean does not exist in C++, only bool :).
 
O

Old Wolf

What is the best way to create functions that, based on some input,
What about the requirement to return something different (like 0)
to indicate the object creation is impossible?

The OP specified an impossible combination of events. There are
(to my mind) 3 ways of resolving this:
1) relax the requirement for a struct to be returned
2) relax the requirement for NULL to be returned
3) return both in a std::pair<> or similar

Trollsdale answered number (2) with (surprisingly) a mostly
correct answer, if you ignore his extraneous use of
the dereference operator.

One could augment his answer by giving X a flag, or some such,
indicating whether there was an error condition.

Note that the OP's other requirement "if a structure cannot be made"
is also unlikely, because either a struct is constructible
or it isn't (perhaps the OP meant, "if some error condition occurs").

Other posters on the the thread have answered (1) (with suggestions
like returning a pointer, or filling out a reference-to-struct
passed as parameter).
 
A

Anubis

Old said:
The OP specified an impossible combination of events. There are
(to my mind) 3 ways of resolving this:
1) relax the requirement for a struct to be returned
2) relax the requirement for NULL to be returned
3) return both in a std::pair<> or similar

Actually there is another option, modifying the needs.

Yes, returning a structure OR null is impossible. But what do you think
the OP will choose between forgetting one functionnality or modifying one ?

Choosing between not returning a structure or returning a structure by
reference parameter instead of return is an easy choice I think.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top