no compiler error with no return value

Started by
7 comments, last by ZQJ 18 years, 6 months ago
I have this little bit of code, and I was attempting to figure out why I kept getting a runtime segmentation fault.

C2DMatrix<float> createHilbertMatrix(unsigned int n)
{
	C2DMatrix<float> f(n);

	for(int i=0; i < n; i++)
		for(int j=0; j < n; j++)
			f(i,j) = (1.0f / (1.0f + i + j) );

}

The problem was that I didn't have the "return f;" statement. I was using g++ on linux, not sure of the version. Here's my question: why would this not come up as a compiler error when I'm compiling?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
Omitting a return will just be a warning, not an error.
Ra
I'm not sure if it's required like C# or Java. I don't know of any situation where this behavior is useful. What probably happens is that it will return what ever value happens to be in the return register.
If you use gcc/g+ you should always use -Wall. It will save you hours, literally.

BTW, what exactly are you returning? You can't just do "return f" right?

EDIT: Forget about the last thing, I had a dumb moment :))

[Edited by - etothex on October 9, 2005 11:48:33 PM]
C was very loose with its function declarations and defintions.
MSVC reports it as an error now (for C++).
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Quote:Original post by etothex
You can't just do "return f" right?

Sure you can.

If forced to guess, I'd say the issue is that gcc can't tell whether that code should be an error until it gets to the optimization stage. Perhaps you never reach that last line, because an earlier return always triggers? There are a few warnings the docs mention won't trigger without an optimization pass because that is the only time it looks at the code as a whole rather than each line individually.

CM
Quote:Original post by Shannon Barber
C was very loose with its function declarations and defintions.
MSVC reports it as an error now (for C++).

Sometimes. Sometimes its a warning:
int f(int i){   if(i < 0)      return -i;   //c:\code\test\test.cpp(15): warning C4715: 'f' : not all control paths return a value}int g(){   //c:\code\test\test.cpp(12): error C4716: 'g' : must return a value}

Personally, this is one of the reasons I prefer VC to gcc. Because I forget that closing return statement entirely too often.

Pointless aside: the exception is with main. Although it is required to have a return type of int, it is not required to actually return a value. If you reach the end of main without a return, the compiler automatically inserts a return 0.

CM
Quote:Original post by Conner McCloud
Personally, this is one of the reasons I prefer VC to gcc. Because I forget that closing return statement entirely too often.


-Wall

//../main.cc: In function `int yarr(int)':int yarr ( int arg ) {	if ( arg ) return 0; //../main.cc:3: warning: control reaches end of non-void function}
Since you're returning a structure/class, it's probably to do with gccs return mechanism and the copy constructor for C2DMatrix. gcc returns structs by copying them out into a buffer whose address is given by the calling function, but if there's no return statement the buffer will be junk. Depending on exactly what happens next the buffer may be copied to another location; it's most likely when that's done that the program crashes because the value of n will be junk and so crash your copy constructor.

This topic is closed to new replies.

Advertisement