return string

Discussion in 'C++' started by Eric Kaplan, Apr 17, 2008.

  1. Eric Kaplan

    Eric Kaplan Guest

    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)
     
    Eric Kaplan, Apr 17, 2008
    #1
    1. Advertising

  2. Eric Kaplan

    Ian Collins Guest

    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.
     
    Ian Collins, Apr 17, 2008
    #2
    1. Advertising

  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.


    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
     
    Andrey Tarasevich, Apr 17, 2008
    #3
  4. Eric Kaplan

    Jim Langston Guest

    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
     
    Jim Langston, Apr 17, 2008
    #4
  5. Eric Kaplan

    Ian Collins Guest

    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.
     
    Ian Collins, Apr 17, 2008
    #5
  6. Eric Kaplan

    James Kanze Guest

    On Apr 17, 2:47 am, Ian Collins <> 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.


    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:
    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
     
    James Kanze, Apr 17, 2008
    #6
  7. Eric Kaplan

    Eric Kaplan Guest

    On Thu, 17 Apr 2008 12:47:45 +1200, Ian Collins <>
    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.
     
    Eric Kaplan, Apr 17, 2008
    #7
  8. Eric Kaplan

    Ian Collins Guest

    Eric Kaplan wrote:
    > On Thu, 17 Apr 2008 12:47:45 +1200, Ian Collins <>
    > 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.
     
    Ian Collins, Apr 17, 2008
    #8
  9. 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
     
    Thomas J. Gritzan, Apr 17, 2008
    #9
  10. Eric Kaplan

    James Kanze Guest

    On Apr 17, 9:05 pm, Eric Kaplan <> wrote:
    > On Thu, 17 Apr 2008 12:47:45 +1200, Ian Collins <>
    > 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:
    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
     
    James Kanze, Apr 18, 2008
    #10
  11. Eric Kaplan

    James Kanze Guest

    On 18 avr, 13:58, Pete Becker <> wrote:
    > On 2008-04-18 04:24:05 -0400, James Kanze <> said:
    > > 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<>::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).

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Apr 18, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randolf Richardson
    Replies:
    2
    Views:
    3,243
    Randolf Richardson
    Feb 3, 2007
  2. Greenhorn
    Replies:
    15
    Views:
    833
    Keith Thompson
    Mar 6, 2005
  3. DotNetGruven

    Web Services return string: An XML string

    DotNetGruven, Jun 28, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    656
    DotNetGruven
    Jun 30, 2004
  4. Xeno Campanoli
    Replies:
    0
    Views:
    229
    Xeno Campanoli
    Feb 13, 2006
  5. Replies:
    1
    Views:
    195
    Dr.Ruud
    Jul 29, 2006
Loading...

Share This Page