Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is it legal code?

Reply
Thread Tools

Is it legal code?

 
 
Öö Tiib
Guest
Posts: n/a
 
      02-20-2011
On Feb 20, 4:03*pm, "Paul" <(E-Mail Removed)> wrote:
> " Tiib" <(E-Mail Removed)> wrote in message news:375b0c4b-0c6f-4bc8-bae4-
>
> < snip >
>
> --Yes. It will be compiled by standard-compliant compiler so it is legal
> --code and it will run with undefined results so it is invalid code.
>
> This is where I am uncertain, the definition of 'legal code' and 'valid
> code'. This seems a pretty fair
> description you have given . Is this actually defined anywhere or is it just
> the generally accepted definition?


I am also uncertain about terminology. Let me try to elaborate.

Language contains that feature so it is legal code. People use that
feature sometimes knowingly. For example they reinterpret a buffer of
bytes as object of some plain old data class and call member function
for it (or some non-member function that uses it). If the alignment
and layout of the bytes matches with real object then it works. I do
not know any platforms where it does not work.

When it is not plain old data class or the bytes do not match with
real object's invariant then it is undefined by standard. It may be
specified for implementation, then it is implementation-specific.

For example: The buffer reinterpreted into object contains byte with
value 42 at position where there should be a bool data member in
object. bool however may have only values true or false (1 or 0).
Called member function that reads that bool data member still likely
runs. Only that it will behave like that bool has value true or has
value false or has both values or has neither of them, depending on
compiler implementation. I don't know implementation that specifies
behavior of bool with underlying binary value 42 so it is invalid
program with programming error in it on all platforms i know of.
 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      02-20-2011

"James Kanze" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 20, 1:09 am, "Paul" <(E-Mail Removed)> wrote:
>> "gwowen" <(E-Mail Removed)> wrote in message

>
>> news:(E-Mail Removed)...
>> On Feb 19, 11:27 am, "Paul" <(E-Mail Removed)> wrote:

>
>> > Is it legal to invoke a (non static) member function without an object?

>
>> > I read something in the C++ standard that it may not be standard
>> > compliant
>> > code to invoke a (non static)member function directly from a pointer.

>
>> --If the pointer points to an object that still exists, its fine.
>> --Otherwise, no. Essentially the same as dereferencing the pointer.

>
>> So given that the following is true:

>
>> "If a nonstatic member function of a class X is called for an
>> object that is not of type X, or of a type derived from X, the
>> behavior is undefined."

>
>> It follows that if an object(or derived object) does not exist then it is
>> undefined behaviour to call its respective nonstatic member function?
>> Therefore it must be true that a member function does not exist without
>> an
>> object.

>
> I don't quite follow you here. If I have a function:
> void f(int&);
> , it's undefined behavior for me to call f without a an object
> of type int.

You must've forgot we are talking about *member functions*.

>The function f(int&) still "exists". For some
> definition of "exists"---this is getting a bit too metaphysical
> for me. One could argue that in C++, only "objects" exist, and
> references and functions aren't objects, so can't "exist".
>

An object(instance of a class type) has a lifetime, it exists for the
duration of its lifetime.
Because C++ disallows calling nonstatic member functions, without an object
of , or derived from, the respective class type. A non static member
function cannot exist unless an object exists.

 
Reply With Quote
 
 
 
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      02-20-2011
itaj sherman wrote:

> I hope you misunderstood me.
>
> On Feb 19, 8:05 pm, "Johannes Schaub (litb)"
> <(E-Mail Removed)> wrote:
>> itaj sherman wrote:
>> > On Feb 19, 5:19 pm, "Johannes Schaub (litb)"
>> > <(E-Mail Removed)> wrote:
>> >> Paul wrote:
>> >> > Is it legal to invoke a (non static) member function without an
>> >> > object?

>>
>> >> > I read something in the C++ standard that it may not be standard
>> >> > compliant code to invoke a (non static)member function directly from
>> >> > a pointer.

>>
>> >> It is undefined behavior if you do so.

>>
>> >> struct A { void f() { } };
>> >> int main() { ((A*)0)->f(); }

>>
>> >> Undefined. No object.

>
> I agree, but the same thing is true for all functions, not just
> nonstatic members:
>


The question then is whether you still "invoke a (non static) member
function without an object". If you interpret the question as meaning "with
arguments that don't represent objects" then the question becomes moot. Is
the following an answer?

void f() { }
f(); // yes you can!

It's pretty clear to me the only sense you can get out of the question is
when you interpret "without an object" to refer to the implied object
argument that "*this" refers to.

>>
>> > struct A {};
>> > void foo( A& );
>> > int main() { foo( *((A*)0) ); }

>>
>> > This is undefined behaviour just as well.
>> > There's nothing in this issue that has specifically to do with
>> > nonstatic member functions.

>>
>> Yes, please act in a troll-way.

>
> So what do you mean by that?????????????
>


I have shown

1) How you call a non-static member function without an object (subject of
the question)
2) That it is undefined behavior (I did not quote the spec - I left that as
an exercise to the reader).

You then show something completely unrelated (calling a free function with a
null reference as an argument). Which is of course also undefined behavior,
but has nothing to do with the question nor with the answer ("to invoke a
(non static) member function without an object"). Why does that
question/this answer have not specifically to do with nonstatic member
functions?

 
Reply With Quote
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      02-20-2011
Johannes Schaub (litb) wrote:

> itaj sherman wrote:
>
>> I hope you misunderstood me.
>>
>> On Feb 19, 8:05 pm, "Johannes Schaub (litb)"
>> <(E-Mail Removed)> wrote:
>>> itaj sherman wrote:
>>> > On Feb 19, 5:19 pm, "Johannes Schaub (litb)"
>>> > <(E-Mail Removed)> wrote:
>>> >> Paul wrote:
>>> >> > Is it legal to invoke a (non static) member function without an
>>> >> > object?
>>>
>>> >> > I read something in the C++ standard that it may not be standard
>>> >> > compliant code to invoke a (non static)member function directly
>>> >> > from a pointer.
>>>
>>> >> It is undefined behavior if you do so.
>>>
>>> >> struct A { void f() { } };
>>> >> int main() { ((A*)0)->f(); }
>>>
>>> >> Undefined. No object.

>>
>> I agree, but the same thing is true for all functions, not just
>> nonstatic members:
>>

>
> The question then is whether you still "invoke a (non static) member
> function without an object". If you interpret the question as meaning
> "with arguments that don't represent objects" then the question becomes
> moot. Is the following an answer?
>
> void f() { }
> f(); // yes you can!
>


An just for the record. That would not be a "nonstatic member function".
 
Reply With Quote
 
itaj sherman
Guest
Posts: n/a
 
      02-20-2011
On Feb 20, 5:56 pm, "Johannes Schaub (litb)"
<(E-Mail Removed)> wrote:
> itaj sherman wrote:
> > I hope you misunderstood me.

>


Ok, it was a misunderstanding. I thought you would agree with me that
what I said is related. I'll try to explain what I meant, as I said in
my other posts in this thread. You might still not agree with me that
it is related, then I can only apologize that I used the momentum of
your post to make my point.
Sorry.

> > On Feb 19, 8:05 pm, "Johannes Schaub (litb)"
> > <(E-Mail Removed)> wrote:
> >> itaj sherman wrote:
> >> > On Feb 19, 5:19 pm, "Johannes Schaub (litb)"
> >> > <(E-Mail Removed)> wrote:
> >> >> Paul wrote:
> >> >> > Is it legal to invoke a (non static) member function without an
> >> >> > object?

>
> >> >> > I read something in the C++ standard that it may not be standard
> >> >> > compliant code to invoke a (non static)member function directly from
> >> >> > a pointer.

>
> >> >> It is undefined behavior if you do so.

>
> >> >> struct A { void f() { } };
> >> >> int main() { ((A*)0)->f(); }

>
> >> >> Undefined. No object.

>
> > I agree, but the same thing is true for all functions, not just
> > nonstatic members:

>
> The question then is whether you still "invoke a (non static) member
> function without an object". If you interpret the question as meaning "with
> arguments that don't represent objects" then the question becomes moot. Is
> the following an answer?
>


My only point was that they are idealy the same question. That there's
nothing special about nonstatic member functions with respect to this
issue.
And I think that limiting the discussion to only the nonstatic member
case, is at least unnecessary but can also cause confusion due to
extra assumptions.
As I said elsewhere the difference of nonstatic member functions are
in syntax and name lookup mainly.

To explain my POV I'll clear that:
For example, a nonstatic member function with 2 extra paramters inside
the braces, is actualy an 3-ary function (in CS sense), whose first
parameter is written outside the braces (with the member lookup
syntax).

i.e
arg0.foo( arg1, arg2 );
instead:
foo( arg0, arg1, arg2 );

declared:
Par0::foo( Par1, Par2 );
instead:
foo( Par0&, Par1, Par2 );

> void f() { }
> f(); // yes you can!
>
> It's pretty clear to me the only sense you can get out of the question is
> when you interpret "without an object" to refer to the implied object
> argument that "*this" refers to.
>
>
> >> > struct A {};
> >> > void foo( A& );
> >> > int main() { foo( *((A*)0) ); }

>


And my demonstration required to replace your function:
void A::f(); //with arity 1
With my example:
void f( A& ); //also arity 1

and not
void f(); //with arity 0
which wasn't my example.

And then I can say that my function, which is also arity 1, cannot be
called "without an object" for it's first parameter.
And this requirement has nothing to do with the differences in syntax
between nonstatic members and other functions.

Please also look at my other posts to understand what I mean, and why
I think it is important for this thread.

itaj
 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      02-20-2011
On Feb 20, 3:30*pm, "Paul" <(E-Mail Removed)> wrote:

> strlen without an object of ,
> or derived from , string.


strlen(const char*) is a C++ standard function, the same as the C
standard function strlen(const char*). If you don't know that, you
should probably stop trying to make weird ontological assertions about
what is and is not said in the standard, as you don't appear to be
sufficiently familiar with it.

> That is assuming strlen is defined as a non static member function of
> string, in class string.


Which it isn't. It's a stand-alone function, but its still undefined
behaviour to call it on a pointer when that pointer does not point to
a NULL-terminated string.

Consider the following:

int main()
{
char* foo = new char(20);
/* q declared int, rather than std::size_t in order to save myself
typing */
int q = strlen(foo);
std::cout << "The length of foo is " << q;
}

The above program invokes undefined behaviour.
None of its constituent parts are non-existent.
Therefore, your syllogism "UB -> Non-existence" is wrong.
 
Reply With Quote
 
itaj sherman
Guest
Posts: n/a
 
      02-20-2011
On Feb 20, 5:48*pm, "Paul" <(E-Mail Removed)> wrote:
> "James Kanze" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > On Feb 20, 1:09 am, "Paul" <(E-Mail Removed)> wrote:
> >> "gwowen" <(E-Mail Removed)> wrote in message

>
> >>news:(E-Mail Removed)....
> >> On Feb 19, 11:27 am, "Paul" <(E-Mail Removed)> wrote:

>
> >> > Is it legal to invoke a (non static) member function without an object?

>
> >> > I read something in the C++ standard that it may not be standard
> >> > compliant
> >> > code to invoke a (non static)member function directly from a pointer..

>
> >> --If the pointer points to an object that still exists, its fine.
> >> --Otherwise, no. Essentially the same as dereferencing the pointer.

>
> >> So given that the following is true:

>
> >> "If a nonstatic member function of a class X is called for an
> >> object that is not of type X, or of a type derived from X, the
> >> behavior is undefined."

>
> >> It follows that if an object(or derived object) does not exist then it is
> >> undefined behaviour to call its respective nonstatic member function?
> >> Therefore it must be true that a member function does not exist without
> >> an
> >> object.

>
> > I don't quite follow you here. *If I have a function:
> > * *void f(int&);
> > , it's undefined behavior for me to call f without a an object
> > of type int.

>
> You must've forgot we are talking about *member functions*.
>
> >The function f(int&) still "exists". *For some
> > definition of "exists"---this is getting a bit too metaphysical
> > for me. *One could argue that in C++, only "objects" exist, and
> > references and functions aren't objects, so can't "exist".

>
> An object(instance of a class type) has a lifetime, it exists for the
> duration of its lifetime.
> Because C++ disallows calling nonstatic member functions, without an object
> of , or derived from, the respective class type. A non static member
> function cannot exist unless an object exists.


Does the following also work?

An object(instance of a class type) has a lifetime, it exists for the
duration of its lifetime.
Because C++ disallows calling global functions with a class reference
parameter, without an object of , or derived from, the respective
class type. Such function cannot exist unless an object exists.

struct A {};
void foo( A& )
{
}
// foo does not exist here.

int main()
{
//foo does not exist here either.
A a;
// aah there you go, now foo exists.
foo( a );
}

itaj
 
Reply With Quote
 
itaj sherman
Guest
Posts: n/a
 
      02-20-2011
On Feb 20, 6:29 pm, itaj sherman <(E-Mail Removed)> wrote:
> On Feb 20, 5:48 pm, "Paul" <(E-Mail Removed)> wrote:
>


>
> struct A {};
> void foo( A& )
> {}
>
> // foo does not exist here.
>
> int main()
> {
> //foo does not exist here either.
> A a;
> // aah there you go, now foo exists.
> foo( a );
>
> }
>


Actually even better:

struct A
{
public: void bar()
{
}
};

void foo( A& )
{
}

// foo and bar don't exist here.

typedef void (*FuncPtr)( A& );
typedef void (A::*MemberFuncPtr)();

int main()
{
//they don't exist here either.
FuncPtr x = &foo;
MemberFuncPtr y = &A::bar;
//OMG, they don't yet exist even here.
A a;
// aah there you go, an object exists, and now the functions also
exist.
foo( a );
a.bar();
}

itaj
 
Reply With Quote
 
itaj sherman
Guest
Posts: n/a
 
      02-20-2011
On Feb 20, 5:48*pm, "Paul" <(E-Mail Removed)> wrote:

>
> You must've forgot we are talking about *member functions*.
>


Why?
Why you only stick to nonstatic member function?
Why can't you deal with this issue generally about all functions?
IMO, if you do that it will clear a lot of the confusion.

itaj
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-20-2011

"gwowen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On Feb 20, 3:30 pm, "Paul" <(E-Mail Removed)> wrote:

> strlen without an object of ,
> or derived from , string.


strlen(const char*) is a C++ standard function, the same as the C
standard function strlen(const char*). If you don't know that, you
should probably stop trying to make weird ontological assertions about
what is and is not said in the standard, as you don't appear to be
sufficiently familiar with it.

> That is assuming strlen is defined as a non static member function of
> string, in class string.


Which it isn't. It's a stand-alone function, but its still undefined
behaviour to call it on a pointer when that pointer does not point to
a NULL-terminated string.

Consider the following:

int main()
{
char* foo = new char(20);
/* q declared int, rather than std::size_t in order to save myself
typing */
int q = strlen(foo);
std::cout << "The length of foo is " << q;
}

The above program invokes undefined behaviour.
None of its constituent parts are non-existent.
Therefore, your syllogism "UB -> Non-existence" is wrong.
-----------------------------------------------------------------------------
dam this newsreader
----------------------------------------------------------------------------

You misunderstand me , I don't mean that anything that produces undefined
behaviour does not exist.

Perhpas this is a better way to phrase it:
The function is only defined if an object exists so if there is no object
there is no defined function



 
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
Is it legal to write an logical equation for a FPGA LUT in claims of a patent? Weng Tianxiang VHDL 12 12-10-2005 03:49 PM
Research: File-sharers big legal download spenders. Silverstrand Front Page News 0 07-27-2005 03:00 PM
State machine transition on internal signals - is it legal? Divyang M VHDL 9 05-18-2005 03:58 PM
State machine transition on internal signals -- is it legal? Divyang M VHDL 1 05-15-2005 09:36 AM
Is this legal? Valentin Tihomirov VHDL 20 10-29-2003 10:31 AM



Advertisments