![]() |
Passing C++ string Results
I have the following function, but it fails after the first
invocation whenever the length of the input variable is longer than the previous data. Specifically: strToUpper("5h"); // works strToUpper("nope"); // fails (aborts) Apparently the "static" keyword is the problem, but I don't know how to assure that the function will always return the proper result from all calls. Do I need the "static" at all with C++ strings or not? TIA string strToUpper(string str)// force string data to UPPER case { size_t nnn; static string strResult = str; for(nnn = 0; nnn < str.length(); nnn++) { strResult.at(nnn) = toupper(strResult.at(nnn)); } // for return strResult; } // strToUpper |
Re: Passing C++ string Results
Mike Copeland wrote:
> I have the following function, but it fails after the first > invocation whenever the length of the input variable is longer than the > previous data. Specifically: > > strToUpper("5h"); // works > strToUpper("nope"); // fails (aborts) > > Apparently the "static" keyword is the problem, but I don't know how > to assure that the function will always return the proper result from > all calls. Do I need the "static" at all with C++ strings or not? TIA The use of static in this example is both unnecessary and incorrect. A static variable is initialised once, in this case to the first string passed to the function. Since you are returning the result by value, static is unnecessary. > string strToUpper(string str)// force string data to UPPER case > { > size_t nnn; > static string strResult = str; > for(nnn = 0; nnn < str.length(); nnn++) This is also a bug, strResult.at(nnn) will throw an exception when nnn = str.length(). Don't forget strings are zero indexed. -- Ian Collins |
Re: Passing C++ string Results
mrc2323@cox.net (Mike Copeland) writes:
> Apparently the "static" keyword is the problem, but I don't know how >to assure that the function will always return the proper result from >all calls. Do I need the "static" at all with C++ strings or not? TIA What did make you believe in the first place that you had to use »static«? |
Re: Passing C++ string Results
On Sun, 2013-01-20, Stefan Ram wrote:
> mrc2323@cox.net (Mike Copeland) writes: >> Apparently the "static" keyword is the problem, but I don't know how >>to assure that the function will always return the proper result from >>all calls. Do I need the "static" at all with C++ strings or not? TIA > > What did make you believe in the first place that you had to > use »static«? I get the impression (from this posting and vague memories of his earlier ones) that he's trying to learn C++ by trial and error. If he is, it's not a strategy. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
Re: Passing C++ string Results
On 13-01-19 02:41 PM, Mike Copeland wrote:
> I have the following function, but it fails after the first > invocation whenever the length of the input variable is longer than the > previous data. Specifically: > > strToUpper("5h"); // works > strToUpper("nope"); // fails (aborts) > > Apparently the "static" keyword is the problem, but I don't know how > to assure that the function will always return the proper result from > all calls. Do I need the "static" at all with C++ strings or not? TIA > > string strToUpper(string str)// force string data to UPPER case > { > size_t nnn; > static string strResult = str; > for(nnn = 0; nnn < str.length(); nnn++) > { > strResult.at(nnn) = toupper(strResult.at(nnn)); > } // for > return strResult; > } // strToUpper > The static key insures your variable is initialized once and then persists across invocations of your function. You are then iterating over one string while using the length of another as a guard. You have two statements that make no sense in this context - the static attribute serves no purpose in this context and str.length is just wrong, though it will work correctly if you remove the static from your assignment. You are iterating over strResult so use its length as your guard and you don't want the first invocation to persist strResult so remove static. |
Re: Passing C++ string Results
On 2013-01-19, Ian Collins <ian-news@hotmail.com> wrote:
>> string strToUpper(string str)// force string data to UPPER case >> { >> size_t nnn; >> static string strResult = str; >> for(nnn = 0; nnn < str.length(); nnn++) > > This is also a bug, strResult.at(nnn) will throw an exception when nnn = > str.length(). Don't forget strings are zero indexed. Is it? Here's the complete fragment: size_t nnn; static string strResult = str; for(nnn = 0; nnn < str.length(); nnn++) { strResult.at(nnn) = toupper(strResult.at(nnn)); } // for strResult.at(nnn) is never called with nnn = str.length(), the loop body is only executed for nnn < str.length(). |
Re: Passing C++ string Results
On 2013-01-20, Ike Naar <ike@iceland.freeshell.org> wrote:
> On 2013-01-19, Ian Collins <ian-news@hotmail.com> wrote: >>> string strToUpper(string str)// force string data to UPPER case >>> { >>> size_t nnn; >>> static string strResult = str; >>> for(nnn = 0; nnn < str.length(); nnn++) >> >> This is also a bug, strResult.at(nnn) will throw an exception when nnn = >> str.length(). Don't forget strings are zero indexed. > > Is it? Here's the complete fragment: > > size_t nnn; > static string strResult = str; > for(nnn = 0; nnn < str.length(); nnn++) > { > strResult.at(nnn) = toupper(strResult.at(nnn)); > } // for > > strResult.at(nnn) is never called with nnn = str.length(), > the loop body is only executed for nnn < str.length(). Sorry, missed the fact that strResult is static, and therefor only initialized when the function is called for the first time, strResult.at(nnn) can indeed throw an exception. |
Re: Passing C++ string Results
On Sunday, January 20, 2013 12:00:19 AM UTC+1, Ian Collins wrote:
> Mike Copeland wrote: > > > I have the following function, but it fails after the first > > > invocation whenever the length of the input variable is longer than the > > > previous data. Specifically: > > > > > > strToUpper("5h"); // works > > > strToUpper("nope"); // fails (aborts) > > > > > > Apparently the "static" keyword is the problem, but I don't know how > > > to assure that the function will always return the proper result from > > > all calls. Do I need the "static" at all with C++ strings or not? TIA > > > > The use of static in this example is both unnecessary and incorrect. > > > > A static variable is initialised once, in this case to the first string > > passed to the function. Since you are returning the result by value, > > static is unnecessary. > > > > > string strToUpper(string str)// force string data to UPPER case > > > { > > > size_t nnn; > > > static string strResult = str; > > > for(nnn = 0; nnn < str.length(); nnn++) > > > > This is also a bug, strResult.at(nnn) will throw an exception when nnn = > > str.length(). Don't forget strings are zero indexed. That's not his bug. nnn will not reach str.length due to "nnn < str.length()". The bug is related the use of static: strResult is created with value "5th" on the first call. On the second, strResult is still "5th". nnn will eventually become 3. When that happens, strResult.at will throw out_of_range. Mike, just remove static. That will cause strResult to be a copy of str on every entry to the function. That said, you'll be better off by removing strResult altogether: string strToUpper(string str)// force string data to UPPER case { size_t nnn; for(nnn = 0; nnn < str.length(); nnn++) { str.at(nnn) = toupper(str.at(nnn)); } // for return str; } // strToUpper And if this is a rewrite of a C function "char* strToUpper(char* str)", where str was modified in-place and returned back, you should better do: void strToUpper(string& str)// force string data to UPPER case { size_t nnn; for(nnn = 0; nnn < str.length(); nnn++) { str.at(nnn) = toupper(str.at(nnn)); } // for } // strToUpper (The original C function was rather wrong to "return" anything, too; conceptually, it works with the string in-place, and the caller already has a pointer to that string; there's no great benefit in "returning" it back) Goran. |
Re: Passing C++ string Results
On Sun, 2013-01-20, Jorgen Grahn wrote:
.... > I get the impression (from this posting and vague memories of his > earlier ones) that he's trying to learn C++ by trial and error. > If he is, it's not a strategy. (I meant to write "not a good strategy". Although some might find the above more fitting.) /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
Re: Passing C++ string Results
<goran.pusic@gmail.com> wrote:
> (The original C function was rather wrong to "return" anything, too; > conceptually, it works with the string in-place, and the caller already > has a pointer to that string; there's no great benefit in "returning" it back) It is often convenient to write: f(strToUpper(s)); instead of: strToUpper(s); f(s); operator= that returns *this belongs to the same category. Tobi |
| All times are GMT. The time now is 05:37 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.