Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why this works?

Reply
Thread Tools

Why this works?

 
 
James Kanze
Guest
Posts: n/a
 
      12-28-2012
On Friday, 28 December 2012 09:05:06 UTC, Juha Nieminen wrote:
> Rui Maciel <> wrote:


> > The compiler can ignore scenarios such as calls to exit(), throwing
> > exceptions, or invoking functions that do not return, and check which code
> > path doesn't include a properly formatted return statement. It's better if
> > the compiler requires unnecessary return statements than it is to support
> > invalid constructs such as returning uninitialized references of objects
> > that don't exist.


> AFAIK C++ inherits this optionality of a return value from C. I'm guessing
> that it exists in C because in the 70's they didn't want to force the
> programmer to increase the size of functions by even one byte if they
> absolutely didn't have to.


The rules in C were (and I think still are) slightly different;
falling off the end was only undefined behavior if the callee
tried to use the returned value. Originally, C didn't have
`void`; functions which didn't have anything to return returned
`int`, and the callee just ignored it.

In the case of C++, things start getting more complicated if the
return type has non-trivial constructors and destructors. The
callee is going to call the destructor, even if the coder
ignores the return value.

> Nowadays increasing the size of a function by a few bytes due to an
> extraneous 'return' might not be a problem, but the thing is, there
> might be cases where the 'return' actually takes a lot more than just
> a few bytes.


> The return value might in fact be very large, like several kilobytes.
> *Creating* the return value might be very complicated or laborious
> (such as it being an object with a mandatory constructor that takes
> hundreds of mandatory parameters.) Being forced to construct such a
> complicated object can in some cases be completely unnecessary, for
> example in situations like


Constructing a return value might note even be possible, if the
type doesn't have a default constructor. You may not have the
appropriate values.

There *is* a simple solution: just insert an illegal instruction
in the generated code. If the function does fall off the end,
the program crashes.

--
James
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments