Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » U++ Developers corner » AString::Compare missing explicit return value (q will = 0 if the loop isn't broken. This value should be returned explicitly.)
AString::Compare missing explicit return value [message #44081] Tue, 23 December 2014 10:56 Go to next message
rainbowsally is currently offline  rainbowsally
Messages: 29
Registered: December 2014
Promising Member
For x86-based C/C++, the eax/rax register is usually returned by default (for int types) and this is probably the register holding 'q' below, but it can't be counted on.

File: uppsrc/Core/AString.hpp
template <class B>
int AString<B>::Compare(const tchar *b) const
{
	const tchar *a = B::Begin();
	const tchar *ae = End();
	for(;;) {
		if(a >= ae)
			return *b == 0 ? 0 : -1;
		if(*b == 0)
			return 1;
		int q = cmpval__(*a++) - cmpval__(*b++);
		if(q)
			return q;
	}
	// -rs added 1 line to explicitly return q(=0)
	return 0;
}


Re: AString::Compare missing explicit return value [message #44085 is a reply to message #44081] Tue, 23 December 2014 14:53 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi rainbowsally,

There was nothing wrong with the function. If you look carefully, you'll find out that the line you added will actually never be called Wink There is no way the code evaluation could get out of the infinite for-loop, other than one of the first three return statements.

So even though some compilers might warn about missing return statement, there is actually no problem or undefined behavior in this function.

Best regards,
Honza
Re: AString::Compare missing explicit return value [message #44088 is a reply to message #44085] Wed, 24 December 2014 04:42 Go to previous message
rainbowsally is currently offline  rainbowsally
Messages: 29
Registered: December 2014
Promising Member
Hi Honza.

Yer right. Thanks. Smile I'll un-fix my version.

dolik.rce wrote on Tue, 23 December 2014 14:53
Hi rainbowsally,

There was nothing wrong with the function. If you look carefully, you'll find out that the line you added will actually never be called Wink There is no way the code evaluation could get out of the infinite for-loop, other than one of the first three return statements.

So even though some compilers might warn about missing return statement, there is actually no problem or undefined behavior in this function.

Best regards,
Honza

Previous Topic: CbGen not compiling in linux.
Next Topic: Multithreaded Animated GIF
Goto Forum:
  


Current Time: Thu Mar 28 10:10:57 CET 2024

Total time taken to generate the page: 0.00831 seconds