Thread: In terms of optimizations

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    28

    In terms of optimizations

    In many articles have I read about pointers being a lot easier to pass than strings and should usually be used when using classes and such. But there is a lot more code involved when making this happen. Take the below example:

    Code:
        wxString input = InputBox->GetValue(); //Get value from input box.
        wxString* output = grid->Encode(&input); //Receive pointer from function. 
        OutputBox->SetValue( *output ); //Print that pointers variables value
        delete output; //Deallocate output.
    This compared to:

    Code:
        OutputBox->SetValue(grid->Encode( InputBox->GetValue() ) ); //Encode uses return string from GetValue() and SetValue uses return string of Encode
    wxString is simply a STD String except a few modifications from wxWidgets team.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Whatever you've been reading sounds like a bunch of crap, or it was related to C rather than C++, or you misinterpreted it. Just pass by reference or const-reference where appropriate, or by pointer if the object is optional. Returning a string is not so bad when NRVO kicks in, and it's one hell of a lot better than having to delete anything manually.
    Add to that the fact that the second example is so much shorter and you have your clear winner.
    You need a more complete example though, to demonstrate whether what you've talked about does actually make a lot of sense.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    This is the expression that calls my function.
    Code:
    OutputBox->SetValue( grid->Encode(InputBox->GetValue()) );
    This is the function that is called. I don't really get why it works. InputBox->GetValue() returns a temporary value so how can I make a reference to it? Is this the correct way of calling a function with a reference parameter?
    Code:
    wxString MatrixGrid::Encode(const wxString& input)
    {
        bool resetFor;
        wxString output;
    
        for ( unsigned int a = 0; a < input.Length(); ++a)
        {
            resetFor = false;
            for ( int i = 0; i < GetNumberRows(); ++i)
            {
                for ( int j = 0; j < GetNumberCols(); ++j)
                {
                    if (input.GetChar(a) == GetCellValue( i, j ))
                    {
                        output << i << j;
                        resetFor = true;
                        break;
                    }
                }
                if (resetFor == true)
                {
                    break;
                }
            }
        }
        return output;
    
    }
    Comments are erased because the code shouldn't be necessary to the subject except for the declaration.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by computerquip View Post
    This is the expression that calls my function.
    Code:
    OutputBox->SetValue( grid->Encode(InputBox->GetValue()) );
    This is the function that is called. I don't really get why it works. InputBox->GetValue() returns a temporary value so how can I make a reference to it? Is this the correct way of calling a function with a reference parameter?
    It works because a const-reference extends the lifetime of a temporary object. (non-const references do not).
    A const-reference parameter type accepts more things than any other type of function parameter, though it of course cannot accept NULL. It also means that it is as efficient as passing by pointer - no copy is made.
    Also, that function should be a suitable candidate for NRVO, thus returning by value should avoid the copy-construction overhead in release builds.
    Does SetValue also take a const-reference?

    What I probably should have said earlier is that certainly traditionally in C one would often get faster code produced when passing structs by pointer instead of by value, but fortunately C++ provides a third option - (const)references .
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    Thanks a lot! Recently, I've been freaking out over whether or not I'm coding everything right. I'll spend hours on reviewing my own stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc & Calloc difference in terms of memory allocated
    By swapnaoe in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 12:57 AM
  2. how many terms
    By rams in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:10 AM
  3. Food Terms
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 12-01-2001, 06:59 PM
  4. Terms
    By Drakon in forum Game Programming
    Replies: 7
    Last Post: 09-27-2001, 06:24 PM
  5. Optimizations disabled?
    By gliptic in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2001, 01:12 AM