Jump to content
  • Advertisement
Sign in to follow this  
Endar

no compiler error with no return value

This topic is 4612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!