Compiler didn't error on function with no return statement
Andy Falanga (afalanga)
afalanga@micron.com
Thu Jun 20 18:06:00 GMT 2013
Hi everyone,
I need some help in understanding why my GCC didn't consider this an issue. I have a function that was constructing a path to a daemon program based on the location of the shared object file where this code is. Something similar to this:
namespace {
std::string ConstructPath()
{
int lastSlash(0);
std::string pathVar;
Dl_info dl_info;
memset(&dl_info, 0, sizeof(dl_info));
if((dladdr((void*)ConstructPath, &dl_info)) == 0)
{
throw std::runtime_error("Failed to get address ");
}
pathVar = dl_info.dli_fname;
lastSlash = pathVar.find_last_of('/');
if(std::string::npos == lastSlash)
{
// no slashes given ... must be that *.so
// is in the current directory
pathVar = "mydaemond";
}
else
{
pathVar.erase(pathVar.begin() + (lastSlash + 1), pathVar.end());
pathVar.append("mydaemond");
}
// first check if we can find the daemon
{
// introducing sub-scope to ensure the file object is closed
std::ifstream test(pathVar.c_str());
if(!test.good())
{
throw std::runtime_error("cannot find mydaemond");
}
}
// *** the below statement wasn't there originally, the
// *** function simply exited after the forced-scope block above,
// *** however, the function *did* have the return type
return pathVar;
}
}
My comments above the final return statement illustrate what my question is about. Why wasn't this a problem? There was no return statement and yet, the code compiled fine. I'm using GCC 4.4.4 on CentOS 6.2. Is this just a problem with the 4.4.4 compiler that was fixed? I'm betting there's some subtlety in C++ here that I'm not yet aware of and I'd like to be schooled.
Thanks,
Andy
More information about the Gcc-help
mailing list