473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

return string

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)
Jun 27 '08 #1
12 1997
Eric Kaplan wrote:
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.

--
Ian Collins.
Jun 27 '08 #2
Eric Kaplan wrote:
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.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #3
Eric Kaplan wrote:
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.

--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #4
Jim Langston wrote:
Eric Kaplan wrote:
>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.

--
Ian Collins.
Jun 27 '08 #5
On Apr 17, 2:47 am, Ian Collins <ian-n...@hotmail.comwrote:
Eric Kaplan wrote:
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.
Except that an empty string could be a legitimate field value.
I'd use a fallible in this case:

Fallible< std::string Table::get(
std::string const& fieldName ) ;

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #6
On Thu, 17 Apr 2008 12:47:45 +1200, Ian Collins <ia******@hotmail.com>
wrote:
>Eric Kaplan wrote:
> 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.
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.
Jun 27 '08 #7
Eric Kaplan wrote:
On Thu, 17 Apr 2008 12:47:45 +1200, Ian Collins <ia******@hotmail.com>
wrote:
>Eric Kaplan wrote:
>> 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.

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?

--
Ian Collins.
Jun 27 '08 #8
Ian Collins schrieb:
Eric Kaplan wrote:
> 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.
Should be:

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

--
Thomas
http://www.netmeister.org/news/learn2quote.html
To iterate is human, to recurse divine.
-L. Peter Deutsch
Jun 27 '08 #9
On Apr 17, 9:05 pm, Eric Kaplan <tobycraf...@yahoo.comwrote:
On Thu, 17 Apr 2008 12:47:45 +1200, Ian Collins <ian-n...@hotmail.com>
wrote:
Eric Kaplan wrote:
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.
Is this
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 27 '08 #10
On 2008-04-18 04:24:05 -0400, James Kanze <ja*********@gmail.comsaid:
>
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.
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.

Of course, that doesn't mean micro-optimizing every return statement.
But understanding relative performance is an important part of proper
optimization; if the time taken by a return statement is a significant
fraction of the overall time taken by a function, it could easily
become a bottleneck in a customer's code.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #11
On 18 avr, 13:58, Pete Becker <p...@versatilecoding.comwrote:
On 2008-04-18 04:24:05 -0400, James Kanze <james.ka...@gmail.comsaid:
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.
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<>::operator[]() 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).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #12
On 2008-04-18 15:59:26 -0400, James Kanze <ja*********@gmail.comsaid:
>
Still, given the apparent level of the original poster, I don't
think he's implementing a standard library.
<g>
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).
Well, sure. Proper optimization of library code is an advanced topic.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
12
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
7
by: nafri | last post by:
hello all, I want to create a function that returns the first element of the Array that is input to it. However, the Input Array can be an Array of points, double, or anyother type, which means...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
9
by: MSDNAndi | last post by:
Hi, I have a set of simple webservices calls that worked fine using .NET Framework 1.0. I am calling a Java/Apache based webservices, the calling side is not able to supply a proper WSDL. ...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
2
by: utab | last post by:
Dear all, I tried sth easy(actually this was an exercise) but I tried to use the standard lib. heavily for this problem(as far as I can). There was one point I could not figure out. The problem...
6
KoreyAusTex
by: KoreyAusTex | last post by:
If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()? import java.util.*; public class...
14
by: =?Utf-8?B?QmVu?= | last post by:
Hi all, I'm trying to understand the concept of returning functions from the enclosing functions. This idea is new to me and I don't understand when and why I would need to use it. Can someone...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.