return string

E

Eric Kaplan

I have a function like this
=============
bool Table::Get(char* FieldName, char* FieldValue)

and I call the function like the follow
=============
while(!tbl.ISEOF())
{
char id2[300];
if(tbl.Get("program",id2))
cout<<"program:"<<id2<<endl;
else
{
tbl.GetErrorErrStr(ErrStr);
cout<<"\n"<<ErrStr<<"\n";
break;
}

}


above I passed in a char pointer (char*) - FieldValue and use it just
like a return value.

but it seem not efficient as each time i initialize a char array of
[300] characters.

but some string (FieldValue) is only like [5] char.

how to I do better for returning string? any example code?

=================

should I just return the string in regular return type like -
std::string Table::Get(char* FieldName)
 
I

Ian Collins

Eric said:
I have a function like this
=============
bool Table::Get(char* FieldName, char* FieldValue)
Why not

std::string Table::Get( const std::string FieldName& );

You can test for an empty string as you failure condition.
 
A

Andrey Tarasevich

Eric said:
I have a function like this
=============
bool Table::Get(char* FieldName, char* FieldValue)

and I call the function like the follow
=============
while(!tbl.ISEOF())
{
char id2[300];
if(tbl.Get("program",id2))
cout<<"program:"<<id2<<endl;
else
{
tbl.GetErrorErrStr(ErrStr);
cout<<"\n"<<ErrStr<<"\n";
break;
}

}


above I passed in a char pointer (char*) - FieldValue and use it just
like a return value.

but it seem not efficient as each time i initialize a char array of
[300] characters.

Where exactly do you "initialize" a char array of 300 characters? What I
see in your code is an _uninitialized_ array of 300 characters.
 
J

Jim Langston

Eric said:
I have a function like this
=============
bool Table::Get(char* FieldName, char* FieldValue)

and I call the function like the follow
=============
while(!tbl.ISEOF())
{
char id2[300];
if(tbl.Get("program",id2))
cout<<"program:"<<id2<<endl;
else
{
tbl.GetErrorErrStr(ErrStr);
cout<<"\n"<<ErrStr<<"\n";
break;
}

}


above I passed in a char pointer (char*) - FieldValue and use it just
like a return value.

but it seem not efficient as each time i initialize a char array of
[300] characters.

but some string (FieldValue) is only like [5] char.

how to I do better for returning string? any example code?

Instead of using a c-style string, you would be better to use a std::string
which is dynamically sized. You can copy from/to c-style strings relatively
easy.

bool Table::Get( char* FieldName, std::string& FieldValue )
{
// Determine the data that FieldValue is to get. You'll probably get a
pointer
// to a c-style string somewhere returned from some database.
// for an example:
char data[] = "12345678";

FieldValue = data;
}

The key to it is assigning std::string = char* will dynamically size the
string to hold the amount of characters needed.

Now to call it:

while(!tbl.ISEOF())
{
std::string id2;
if (tbl.Get("program", id2))
cout<<"program:"<<id2<<endl;
else
{
tbl.GetErrorErrStr(ErrStr);
cout<<"\n"<<ErrStr<<"\n";
break;
}
}
=================

should I just return the string in regular return type like -
std::string Table::Get(char* FieldName)

You could, yes. You seem to know about std::string yet didn't realize you
could pass a reference to a std::string just as you can a char pointer.
 
I

Ian Collins

Jim said:
Eric said:
I have a function like this
=============
bool Table::Get(char* FieldName, char* FieldValue)

and I call the function like the follow
=============
while(!tbl.ISEOF())
{
char id2[300];
if(tbl.Get("program",id2))
cout<<"program:"<<id2<<endl;
else
{
tbl.GetErrorErrStr(ErrStr);
cout<<"\n"<<ErrStr<<"\n";
break;
}

}


above I passed in a char pointer (char*) - FieldValue and use it just
like a return value.

but it seem not efficient as each time i initialize a char array of
[300] characters.

but some string (FieldValue) is only like [5] char.

how to I do better for returning string? any example code?

Instead of using a c-style string, you would be better to use a std::string
which is dynamically sized. You can copy from/to c-style strings relatively
easy.

bool Table::Get( char* FieldName, std::string& FieldValue )

Make that const char* FieldName or const std::string& FieldName.
 
E

Eric Kaplan

Why not

std::string Table::Get( const std::string FieldName& );

You can test for an empty string as you failure condition.

Is this

std::string Table::Get( const std::string FieldName& );

return by value? should I return a pointer instead? which speed up the
copy of value. may be the string is 500 character long.
 
I

Ian Collins

Eric said:
Is this

std::string Table::Get( const std::string FieldName& );

return by value? should I return a pointer instead? which speed up the
copy of value. may be the string is 500 character long.

Why don't you ask Carmen?
 
T

Thomas J. Gritzan

Ian said:
Why not

std::string Table::Get( const std::string FieldName& );

You can test for an empty string as you failure condition.

Should be:

std::string Table::Get( const std::string& FieldName );
^
 
J

James Kanze

std::string Table::Get( const std::string FieldName& );

return by value?
Yes.

should I return a pointer instead?

A pointer to what?

If the string is guaranteed to be part of your internal
structure, and can never be a calculated value, then returning a
pointer can make sense if you need the possibility of indicating
some sort of non-differentiated error as well---it's a classical
solution in cases where the string is the result of a table
lookup, and there is no default value.

If the string is part of your internal structure, and you want
the client code to be able to modify it (i.e. the value that it
has in your internal structure), then you must return either a
pointer or a reference.

If the string is guaranteed to be part of your internal
structure, but you don't need the error possibility, it's
preferrable to return a reference, rather than a pointer
(although a value might still be preferred to a reference).
which speed up the copy of value. may be the string is 500
character long.

And maybe it's not. And maybe copying the value is fast enough.
Until you actually have a performance problem, and the profiler
indicates that it is due to copying the string, it's stupid to
worry about it.
 
J

James Kanze

That depends on context. Certainly, when you're writing an application,
spending time optimizing things that aren't bottlenecks is a waste of
time. But when you're writing a general purpose library that's going to
be distributed to others, if you don't spend time optimizing things,
you'll spend time responding to complaints from users that your code is
too slow. If that's just an internal feedback loop, fine; but if you're
not part of your users' development process, you'd better put the time
in up front.

I'll admit that library implementers have it tough, here. They
do have to do some guessing, and (for example) were I
implementing the standard library, I'd certainly make sure that
std::vector<>::eek:perator[]() was as fast as possible. Without
waiting for feedback from actual users.

Still, given the apparent level of the original poster, I don't
think he's implementing a standard library. First things first,
and all that. Until you know how to write correct code, you
shouldn't worry about optimization (and you probably shouldn't
be writing libraries for widespread use).
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top