Print Page | Close Window

CString function parameter

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: Toolkit Pro
Forum Description: Topics Related to Codejock Toolkit Pro
URL: http://forum.codejock.com/forum_posts.asp?TID=3548
Printed Date: 28 June 2025 at 5:46am
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: CString function parameter
Posted By: jimmy
Subject: CString function parameter
Date Posted: 23 January 2006 at 4:30am
Hello CodeJock Team,

1) the codejock software is great.

2) optimize point
    a big part of the software have FunctionName(CString strParam)
but it is better use FunctionName(const CString& strParam)
The software ist much faster, less memory defragmentation, smaller code size.
Please, change this for the next release.
also look for other function parameter, to add the parameter as
const referenz.

    Jimmy




Replies:
Posted By: Oleg
Date Posted: 24 January 2006 at 1:56pm

Hi,

1. Agree :)  Thank you

2. Actually in 9.81 we changed most of "CString strParameter" to "LPCTSTR lpszParameter"

some virtual methods we can't change to avoid your complains about compatibility ;)

 



-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS


Posted By: jimmy
Date Posted: 25 January 2006 at 5:09am
Hi oleg,

LPCTSTR is also not good, because
if you do m_strCaption = lpszParameter it always make
a strlen, alloc.
It's slow, and higher memory defragmentation.
For compatibility it's ok.
But for new, i think const CString& is the best solution.

    Jimmy




Posted By: Oleg
Date Posted: 25 January 2006 at 8:49am

lets see.

if method declared as SomeMethods(cont CString& strItem)

and called as

SomeMethod(_T("SomParameter"));

anyway compiller first create CString variable and assign it with "SomParameter", and later destroy it.

 

 



-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS


Posted By: Warren
Date Posted: 07 February 2006 at 10:30am

Jimmy,

You said "...LPCTSTR is also not good..." but according to Microsoft: "...in most cases it is best to declare the string function parameter as LPCTSTR..."

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_Strings.3a_.CString_Argument_Passing.asp - http://msdn.microsoft.com/library/default.asp?url=/library/e n-us/vccore/html/_core_Strings.3a_.CString_Argument_Passing. asp



Posted By: jimmy
Date Posted: 12 February 2006 at 8:21am
Hi,

1) if i call a function many time, i use

static CString s_strText(_T("My Text"));

Function( s_strText );
And void Function( const CString& strText );

So it must alloc the text only one time.

In some function i use also (LPCTSTR lpText), but only if it call not often.

also use as return parameter
const CString& GetName()
{ return m_stName; }

The most time if you call GetName (or other) is, to put the paramter to other function.

x.SetName( y.GetName() );

So this is much faster than always create a new CString.

If i look into our program code, 95 percent of function call put a CString as parameter.

    Jimmy



Posted By: Warren
Date Posted: 12 February 2006 at 4:37pm
Are you under the impression that CString allocates a new copy of the text, and copies the text into the new buffer each time you return "CString" instead of "const CString&"? Because that is not how it works.

AFAIK, CString has a COW (copy on write) implementation, so what are you really saving? It's basically:

return 1 pointer/reference
vs.
change reference count, return 1 pointer/reference

Either way, I wouldn't worry about it nowadays. With 2+ Ghz computers, 99% of optimization is algorithmic and not tweaking code.

That's just my 2 cents...



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net