Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #13246 > unrolled thread
| Started by | Novice Coder <extngg1@gmail.com> |
|---|---|
| First post | 2012-01-11 09:08 -0800 |
| Last post | 2012-01-14 01:52 -0800 |
| Articles | 20 on this page of 44 — 15 participants |
Back to article view | Back to comp.lang.c++
Simultaneous read and write on a variable in C++ Novice Coder <extngg1@gmail.com> - 2012-01-11 09:08 -0800
Re: Simultaneous read and write on a variable in C++ Victor Bazarov <v.bazarov@comcast.invalid> - 2012-01-11 12:28 -0500
Re: Simultaneous read and write on a variable in C++ Victor Bazarov <v.bazarov@comcast.invalid> - 2012-01-12 13:07 -0500
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoonsaurav.nit@gmail.com> - 2012-01-12 10:11 -0800
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 18:46 +0000
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoonsaurav.nit@gmail.com> - 2012-01-12 11:18 -0800
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 19:28 +0000
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 19:41 +0000
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoonsaurav.nit@gmail.com> - 2012-01-12 11:46 -0800
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoonsaurav.nit@gmail.com> - 2012-01-12 11:44 -0800
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 19:46 +0000
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 19:53 +0000
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoonsaurav.nit@gmail.com> - 2012-01-12 12:01 -0800
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 20:10 +0000
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoonsaurav.nit@gmail.com> - 2012-01-12 12:13 -0800
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 20:31 +0000
Re: Simultaneous read and write on a variable in C++ scott@slp53.sl.home (Scott Lurndal) - 2012-01-13 18:12 +0000
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoonsaurav.nit@gmail.com> - 2012-01-12 11:37 -0800
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 19:56 +0000
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoonsaurav.nit@gmail.com> - 2012-01-12 11:58 -0800
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 20:04 +0000
Re: Simultaneous read and write on a variable in C++ Juha Nieminen <nospam@thanks.invalid> - 2012-01-13 06:34 +0000
Re: Simultaneous read and write on a variable in C++ SG <s.gesemann@gmail.com> - 2012-01-11 13:11 -0800
Re: Simultaneous read and write on a variable in C++ Calum <spambox@calumgrant.net> - 2012-01-11 13:18 -0800
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoonsaurav.nit@gmail.com> - 2012-01-12 03:11 -0800
Re: Simultaneous read and write on a variable in C++ "Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl> - 2012-01-12 16:35 +0100
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoonsaurav.nit@gmail.com> - 2012-01-12 08:06 -0800
Re: Simultaneous read and write on a variable in C++ gwowen <gwowen@gmail.com> - 2012-01-12 07:48 -0800
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoonsaurav.nit@gmail.com> - 2012-01-12 07:55 -0800
Re: Simultaneous read and write on a variable in C++ Gareth Owen <gwowen@gmail.com> - 2012-01-12 19:36 +0000
Re: Simultaneous read and write on a variable in C++ Victor Bazarov <v.bazarov@comcast.invalid> - 2012-01-12 16:31 -0500
Re: Simultaneous read and write on a variable in C++ Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-13 02:05 -0800
Re: Simultaneous read and write on a variable in C++ Goran <goran.pusic@gmail.com> - 2012-01-13 04:24 -0800
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoonsaurav.nit@gmail.com> - 2012-01-13 04:52 -0800
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 12:39 +0000
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 12:50 +0000
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 12:58 +0000
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoon@parallocity.com> - 2012-01-12 05:55 -0800
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 13:59 +0000
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoon@parallocity.com> - 2012-01-12 06:14 -0800
Re: Simultaneous read and write on a variable in C++ Leigh Johnston <leigh@i42.co.uk> - 2012-01-12 14:30 +0000
Re: Simultaneous read and write on a variable in C++ Juha Nieminen <nospam@thanks.invalid> - 2012-01-12 14:34 +0000
Re: Simultaneous read and write on a variable in C++ Prasoon Saurav <prasoon@parallocity.com> - 2012-01-12 05:54 -0800
Re: Simultaneous read and write on a variable in C++ BCFD36 <bcfd36@cruzio.com> - 2012-01-14 01:52 -0800
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2012-01-12 20:04 +0000 |
| Message-ID | <OaednVnwYpFyoZLSnZ2dnUVZ8nadnZ2d@giganews.com> |
| In reply to | #13305 |
On 12/01/2012 19:58, Prasoon Saurav wrote:
> On Jan 13, 12:56 am, Leigh Johnston<le...@i42.co.uk> wrote:
>> On 12/01/2012 19:37, Prasoon Saurav wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> On Jan 13, 12:28 am, Leigh Johnston<le...@i42.co.uk> wrote:
>>>> On 12/01/2012 19:18, Prasoon Saurav wrote:
>>
>>>>> On Jan 12, 11:46 pm, Leigh Johnston<le...@i42.co.uk> wrote:
>>>>>> On 12/01/2012 18:11, Prasoon Saurav wrote:
>>
>>>>>>> On Jan 12, 11:07 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote:
>>>>>>>> On 1/11/2012 12:28 PM, Victor Bazarov wrote:
>>
>>>>>>>>> On 1/11/2012 12:08 PM, Novice Coder wrote:
>>>>>>>>>> Here is the code
>>
>>>>>>>>>> int main()
>>>>>>>>>> {
>>>>>>>>>> char *p;
>>>>>>>>>> char a[]={"Hello"};
>>>>>>>>>> (p=a)[0]; // This line
>>>>>>>>>> return 0;
>>>>>>>>>> }
>>
>>>>>>>>>> Is the behaviour of this code undefined in C++?
>>
>>>>>>>>> Yes, AFAIK. There is no intervening sequence point between the
>>>>>>>>> assignment and indexing. Even though there are parentheses, it is not
>>>>>>>>> guaranteed that the compiler isn't going to "optimize" it and use the
>>>>>>>>> previous value of 'p' (undefined) for dereferencing it.
>>
>>>>>>>> I think now that I was wrong there. The expression in parentheses has
>>>>>>>> the value the same as 'a', and *that* is used to dereference the pointer
>>>>>>>> (the indexing is adding the index and then dereferencing). The relevant
>>>>>>>> portion of the Standard (using the latest Draft):
>>
>>>>>>>> <<The assignment operator (=) and the compound assignment operators all
>>>>>>>> group right-to-left. All require a modifiable lvalue as their left
>>>>>>>> operand and return an lvalue referring to the left operand. The result
>>>>>>>> in all cases is a bit-field if the left operand is a bit-field. In all
>>>>>>>> cases, the assignment is sequenced after the value computation of the
>>>>>>>> right and left operands, and before the value computation of the
>>>>>>>> assignment expression.>>
>>
>>>>>>>> The last sentence is important, I believe. "Sequenced" is the key word.
>>
>>>>>>>> > If you need to
>>
>>>>>>>>> introduce a sequence point, use the comma:
>>
>>>>>>>>> p=a,p[0]; // no problem
>>
>>>>>>>>> V
>>
>>>>>>>> V
>>>>>>>> --
>>>>>>>> I do not respond to top-posted replies, please don't ask
>>
>>>>>>> The term sequenced before/after and value computation are part of C+
>>>>>>> +11 and not of C++03. In C++11 the expression has well defined
>>>>>>> behavior. In C++03 it is not.
>>
>>>>>> Nonsense.
>>
>>>>>> (p=a)[0] is perfectly fine in C++03 as is *(p=a) as is *++p2; you are
>>>>>> confusing this defined behaviour with ((p=a)[0] + (p=a+1)[0]) which is UB.
>>
>>>>>> /Leigh
>>
>>>>> Instead of using words like "bullshit", "nonsense" why don't you take
>>>>> help of the Standard to prove yourself correct. Which statement of
>>>>> mine do you think is incorrect and why? If you have some counter-
>>>>> argument please make it.
>>
>>>> You said '*--p' is UB which disagrees with the draft standard:
>>
>>>> 24.4.1/5
>>>> "...
>>>> *first++ = *--last;
>>>> ..."
>>
>>>> So you clearly do not know what you are talking about in this area and I
>>>> still suspect you of being a troll.
>>
>>>> /Leigh
>>
>>> Which draft are you quoting from, n3290? Each of my comment is from C+
>>> +03 point of view. In C++11 *++p; is well defined. I am still not sure
>>> whether it is well defined in C++03
>>> See my this answer :http://stackoverflow.com/a/4183735/165520
>>
>> You are so wrong that if your wrongness had mass it would have a massive
>> event horizon; see my answer follow-up else-thread and then admit you
>> are wrong.
>>
>> /Leigh
>
>
> LOL.
> If *++p is well defined you might argue ++p =10; (for some int 'p') is
> also well defined. :P
But I am not arguing that. You are digging a bigger hole for yourself;
you are either clueless or trolling or cannot admit to mistakes like "Paul".
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2012-01-13 06:34 +0000 |
| Message-ID | <4f0fd058$0$3091$7b1e8fa0@news.nbl.fi> |
| In reply to | #13297 |
Leigh Johnston <leigh@i42.co.uk> wrote: > So you clearly do not know what you are talking about in this area and I > still suspect you of being a troll. Could you please stop using that word to mean "disagrees with me"? You are approaching the same level of stupidity as paul.
[toc] | [prev] | [next] | [standalone]
| From | SG <s.gesemann@gmail.com> |
|---|---|
| Date | 2012-01-11 13:11 -0800 |
| Message-ID | <b2faf553-b12f-43e5-9ac6-e57c46338144@o12g2000vbd.googlegroups.com> |
| In reply to | #13246 |
On 11 Jan., 18:08, Novice Coder wrote:
> Here is the code
>
> int main()
> {
> char *p;
> char a[]={"Hello"};
> (p=a)[0]; // This line
> return 0;
> }
>
> Is the behaviour of this code undefined in C++?
No. The assignment is done which yields the new pointer value itself
which is then dereferenced. No problem at all.
Cheers!
SG
[toc] | [prev] | [next] | [standalone]
| From | Calum <spambox@calumgrant.net> |
|---|---|
| Date | 2012-01-11 13:18 -0800 |
| Message-ID | <7c4fa293-014f-49fa-a3a4-175e3e854080@y12g2000yqc.googlegroups.com> |
| In reply to | #13246 |
On Jan 11, 5:08 pm, Novice Coder <extn...@gmail.com> wrote:
> Here is the code
>
> int main()
> {
> char *p;
> char a[]={"Hello"};
> (p=a)[0]; // This line
> return 0;
>
> }
>
> Is the behaviour of this code undefined in C++?
Looks fine to me. It's fairly common (especially in C) to combine an
assignment and an evaluation into the same expression. (p=a) will
always evaluate to a, no undefined behaviour in sight. You might come
across code like
while( (ch=fgetc(fp))!=-1 )
{
}
The only problem would be if you attempted to use p before the next
sequence point. E.g.
(p=a)+a // Ok
(p=a)+(p=b) // Undefined
(p=a)+p // Undefined
[toc] | [prev] | [next] | [standalone]
| From | Prasoon Saurav <prasoonsaurav.nit@gmail.com> |
|---|---|
| Date | 2012-01-12 03:11 -0800 |
| Message-ID | <cdba2001-4d1a-4d60-a58c-0dd5c26a9392@m4g2000pbc.googlegroups.com> |
| In reply to | #13246 |
On Jan 11, 10:08 pm, Novice Coder <extn...@gmail.com> wrote:
> Here is the code
>
> int main()
> {
> char *p;
> char a[]={"Hello"};
> (p=a)[0]; // This line
> return 0;
>
> }
>
> Is the behaviour of this code undefined in C++?
(p=a)[0]; is UB . The side effect of the assignment operation is
guaranteed to be complete only after we reach a sequence point i.e ' ;
'. In this case the access is made prior to the side effect
completion. The is no total ordering between the assignment and the
dereference
The result of (a=b) is the lvalue 'a' with its rvalue as b, so its 'a'
which is getting accessed and that too before the next sequence point.
Noone can guarantee that the side effect of (a=b) is complete when we
dereference 'a' or the result of (a=b) i.e the lvalue a itself.
HTH
Prasoon
[toc] | [prev] | [next] | [standalone]
| From | "Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl> |
|---|---|
| Date | 2012-01-12 16:35 +0100 |
| Message-ID | <jemuks$el$1@news.albasani.net> |
| In reply to | #13267 |
"Prasoon Saurav" wrote in message news:cdba2001-4d1a-4d60-a58c-0dd5c26a9392@m4g2000pbc.googlegroups.com... > > ... > >The result of (a=b) is the lvalue 'a' with its rvalue as b, so its 'a' >which is getting accessed and that too before the next sequence point. >Noone can guarantee that the side effect of (a=b) is complete when we >dereference 'a' or the result of (a=b) i.e the lvalue a itself. Does that mean that a=(b=c) is also UB?
[toc] | [prev] | [next] | [standalone]
| From | Prasoon Saurav <prasoonsaurav.nit@gmail.com> |
|---|---|
| Date | 2012-01-12 08:06 -0800 |
| Message-ID | <6f788be2-f389-4f63-8119-1eb14d72d9e0@c8g2000pbl.googlegroups.com> |
| In reply to | #13287 |
On Jan 12, 8:35 pm, "Fred Zwarts \(KVI\)" <F.Zwa...@KVI.nl> wrote: > "Prasoon Saurav" wrote in message > > news:cdba2001-4d1a-4d60-a58c-0dd5c26a9392@m4g2000pbc.googlegroups.com... > > > > > ... > > >The result of (a=b) is the lvalue 'a' with its rvalue as b, so its 'a' > >which is getting accessed and that too before the next sequence point. > >Noone can guarantee that the side effect of (a=b) is complete when we > >dereference 'a' or the result of (a=b) i.e the lvalue a itself. > > Does that mean that a=(b=c) is also UB? This is what the FAQ says "If an object is written to within a full expression, any and all accesses to it within the same expression must be directly involved in the computation of the value to be written." This expression satisfies that condition so it is well defined.
[toc] | [prev] | [next] | [standalone]
| From | gwowen <gwowen@gmail.com> |
|---|---|
| Date | 2012-01-12 07:48 -0800 |
| Message-ID | <47e6756d-03b0-47a7-ad82-93b6ffe14ed3@k8g2000yqk.googlegroups.com> |
| In reply to | #13267 |
On Jan 12, 11:11 am, Prasoon Saurav <prasoonsaurav....@gmail.com> wrote: > > Is the behaviour of this code undefined in C++? > > (p=a)[0]; is UB . The side effect of the assignment operation is > guaranteed to be complete only after we reach a sequence point i.e ' ; > '. You're not deferencing p, you're dereferencing the result of (p=a) which is (unambiguously) a.
[toc] | [prev] | [next] | [standalone]
| From | Prasoon Saurav <prasoonsaurav.nit@gmail.com> |
|---|---|
| Date | 2012-01-12 07:55 -0800 |
| Message-ID | <fc198a11-76c4-40fd-bf53-ac9a9e6c0bf6@b4g2000pbi.googlegroups.com> |
| In reply to | #13288 |
On Jan 12, 8:48 pm, gwowen <gwo...@gmail.com> wrote: > On Jan 12, 11:11 am, Prasoon Saurav <prasoonsaurav....@gmail.com> > wrote: > > > > Is the behaviour of this code undefined in C++? > > > (p=a)[0]; is UB . The side effect of the assignment operation is > > guaranteed to be complete only after we reach a sequence point i.e ' ; > > '. > > You're not deferencing p, you're dereferencing the result of (p=a) > which is (unambiguously) a. The result of (p=a) is lvalue 'p' with its rvalue 'a'.
[toc] | [prev] | [next] | [standalone]
| From | Gareth Owen <gwowen@gmail.com> |
|---|---|
| Date | 2012-01-12 19:36 +0000 |
| Message-ID | <87ipkglnlc.fsf@gmail.com> |
| In reply to | #13289 |
Prasoon Saurav <prasoonsaurav.nit@gmail.com> writes: >> You're not deferencing p, you're dereferencing the result of (p=a) >> which is (unambiguously) a. > > The result of (p=a) is lvalue 'p' with its rvalue 'a'. "The result of the assignment operation is the value stored in the left operand _after the assignment has taken place_; the result is an lvalue." [C++ standard : expr.ass] "The assignment operators return the value of the object specified by the left operand *after the assignment*" (my emphasis) http://msdn.microsoft.com/en-us/library/b0zx90af%28v=vs.80%29.aspx Any other interpretation would make C++ gratuitously incompatible with C.
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2012-01-12 16:31 -0500 |
| Message-ID | <jenjf1$60f$1@dont-email.me> |
| In reply to | #13298 |
On 1/12/2012 2:36 PM, Gareth Owen wrote: > Prasoon Saurav<prasoonsaurav.nit@gmail.com> writes: > >>> You're not deferencing p, you're dereferencing the result of (p=a) >>> which is (unambiguously) a. >> >> The result of (p=a) is lvalue 'p' with its rvalue 'a'. > > "The result of the assignment operation is the value stored in the left > operand _after the assignment has taken place_; the result is an > lvalue." [C++ standard : expr.ass] Thank you, Gareth. ^5 V -- I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | Nick Keighley <nick_keighley_nospam@hotmail.com> |
|---|---|
| Date | 2012-01-13 02:05 -0800 |
| Message-ID | <494e9f94-6db1-4416-bc52-d9029fad1a05@u32g2000yqe.googlegroups.com> |
| In reply to | #13267 |
On Jan 12, 11:11 am, Prasoon Saurav <prasoonsaurav....@gmail.com>
wrote:
> On Jan 11, 10:08 pm, Novice Coder <extn...@gmail.com> wrote:
>
> > Here is the code
>
> > int main()
> > {
> > char *p;
> > char a[]={"Hello"};
> > (p=a)[0]; // This line
> > return 0;
>
> > }
>
> > Is the behaviour of this code undefined in C++?
>
> (p=a)[0]; is UB . The side effect of the assignment operation is
> guaranteed to be complete only after we reach a sequence point i.e ' ;
why?
consider
while ((p = f (x, y)) == 0)
a fairly common C/C++ idiom. Yet you claim it won't work.
> '. In this case the access is made prior to the side effect
> completion. The is no total ordering between the assignment and the
> dereference
>
> The result of (a=b) is the lvalue 'a' with its rvalue as b, so its 'a'
> which is getting accessed and that too before the next sequence point.
> Noone can guarantee that the side effect of (a=b) is complete when we
> dereference 'a' or the result of (a=b) i.e the lvalue a itself.
>
> HTH
> Prasoon
[toc] | [prev] | [next] | [standalone]
| From | Goran <goran.pusic@gmail.com> |
|---|---|
| Date | 2012-01-13 04:24 -0800 |
| Message-ID | <55969622-56a4-40bf-bc83-d315245a5f41@p42g2000vbt.googlegroups.com> |
| In reply to | #13267 |
On Jan 12, 12:11 pm, Prasoon Saurav <prasoonsaurav....@gmail.com>
wrote:
> On Jan 11, 10:08 pm, Novice Coder <extn...@gmail.com> wrote:
>
> > Here is the code
>
> > int main()
> > {
> > char *p;
> > char a[]={"Hello"};
> > (p=a)[0]; // This line
> > return 0;
>
> > }
>
> > Is the behaviour of this code undefined in C++?
>
> (p=a)[0]; is UB . The side effect of the assignment operation is
> guaranteed to be complete only after we reach a sequence point i.e ' ;
> '. In this case the access is made prior to the side effect
> completion. The is no total ordering between the assignment and the
> dereference
You're looking at this the wrong way. The assignment to "p" does not
matter. The result of "=a" is some lvalue of type char*. It might, or
not, be "p" (as lvalue). That is what is being indexed with 0, not
necessarily "p". "p" might, or might not, have it's value assigned by
the time we reach sequence point, but that is completely irrelevant.
Goran.
[toc] | [prev] | [next] | [standalone]
| From | Prasoon Saurav <prasoonsaurav.nit@gmail.com> |
|---|---|
| Date | 2012-01-13 04:52 -0800 |
| Message-ID | <f59656a5-0cf0-40ce-bddd-1e18258e6f1e@c9g2000pbh.googlegroups.com> |
| In reply to | #13322 |
On Jan 13, 5:24 pm, Goran <goran.pu...@gmail.com> wrote:
> On Jan 12, 12:11 pm, Prasoon Saurav <prasoonsaurav....@gmail.com>
> wrote:
>
>
>
>
>
>
>
>
>
> > On Jan 11, 10:08 pm, Novice Coder <extn...@gmail.com> wrote:
>
> > > Here is the code
>
> > > int main()
> > > {
> > > char *p;
> > > char a[]={"Hello"};
> > > (p=a)[0]; // This line
> > > return 0;
>
> > > }
>
> > > Is the behaviour of this code undefined in C++?
>
> > (p=a)[0]; is UB . The side effect of the assignment operation is
> > guaranteed to be complete only after we reach a sequence point i.e ' ;
> > '. In this case the access is made prior to the side effect
> > completion. The is no total ordering between the assignment and the
> > dereference
>
> You're looking at this the wrong way. The assignment to "p" does not
> matter. The result of "=a" is some lvalue of type char*. It might, or
> not, be "p" (as lvalue). That is what is being indexed with 0, not
> necessarily "p". "p" might, or might not, have it's value assigned by
> the time we reach sequence point, but that is completely irrelevant.
>
> Goran.
Yes now I understand why I was wrong. Thanks!
Here is a related link : http://stackoverflow.com/questions/8845569/is-p-actually-legalwell-formed-in-c03
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2012-01-12 12:39 +0000 |
| Message-ID | <l6udndJcZeTkSZPSnZ2dnUVZ8jOdnZ2d@giganews.com> |
| In reply to | #13246 |
On 11/01/2012 17:08, Novice Coder wrote:
> Here is the code
>
> int main()
> {
> char *p;
> char a[]={"Hello"};
> (p=a)[0]; // This line
> return 0;
> }
>
> Is the behaviour of this code undefined in C++?
So we have two votes for and against so far; dead heat.
The sane answer is to not write brain-dead code like that.
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2012-01-12 12:50 +0000 |
| Message-ID | <U7ednSvxxt-HSpPSnZ2dnUVZ8jSdnZ2d@giganews.com> |
| In reply to | #13246 |
On 11/01/2012 17:08, Novice Coder wrote:
> Here is the code
>
> int main()
> {
> char *p;
> char a[]={"Hello"};
> (p=a)[0]; // This line
> return 0;
> }
>
> Is the behaviour of this code undefined in C++?
I vote that it is not UB.
(p=a)[0] is equivalent to *(p=a); if *(p=a) is UB then surely *(p2++) is
also UB? Clearly nonsense. (p=a)[0] + (p=a+1)[0] is UB however.
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2012-01-12 12:58 +0000 |
| Message-ID | <VLSdnd9BY4qcRJPSnZ2dnUVZ8rWdnZ2d@giganews.com> |
| In reply to | #13270 |
On 12/01/2012 12:50, Leigh Johnston wrote:
> On 11/01/2012 17:08, Novice Coder wrote:
>> Here is the code
>>
>> int main()
>> {
>> char *p;
>> char a[]={"Hello"};
>> (p=a)[0]; // This line
>> return 0;
>> }
>>
>> Is the behaviour of this code undefined in C++?
>
> I vote that it is not UB.
>
> (p=a)[0] is equivalent to *(p=a); if *(p=a) is UB then surely *(p2++) is
> also UB? Clearly nonsense. (p=a)[0] + (p=a+1)[0] is UB however.
I meant '*(++p2)' not '*(p2++)'.
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | Prasoon Saurav <prasoon@parallocity.com> |
|---|---|
| Date | 2012-01-12 05:55 -0800 |
| Message-ID | <f044a2e4-ec1e-492c-a261-c0d29815a22c@lr19g2000pbb.googlegroups.com> |
| In reply to | #13271 |
On Jan 12, 5:58 pm, Leigh Johnston <le...@i42.co.uk> wrote:
> On 12/01/2012 12:50, Leigh Johnston wrote:
>
>
>
>
>
>
>
>
>
> > On 11/01/2012 17:08, Novice Coder wrote:
> >> Here is the code
>
> >> int main()
> >> {
> >> char *p;
> >> char a[]={"Hello"};
> >> (p=a)[0]; // This line
> >> return 0;
> >> }
>
> >> Is the behaviour of this code undefined in C++?
>
> > I vote that it is not UB.
>
> > (p=a)[0] is equivalent to *(p=a); if *(p=a) is UB then surely *(p2++) is
> > also UB? Clearly nonsense. (p=a)[0] + (p=a+1)[0] is UB however.
>
> I meant '*(++p2)' not '*(p2++)'.
>
> /Leigh
Yes I believe *(++p2) is UB too.
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2012-01-12 13:59 +0000 |
| Message-ID | <3qadnekWasjLepPSnZ2dnUVZ8i-dnZ2d@giganews.com> |
| In reply to | #13276 |
On 12/01/2012 13:55, Prasoon Saurav wrote:
> On Jan 12, 5:58 pm, Leigh Johnston<le...@i42.co.uk> wrote:
>> On 12/01/2012 12:50, Leigh Johnston wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> On 11/01/2012 17:08, Novice Coder wrote:
>>>> Here is the code
>>
>>>> int main()
>>>> {
>>>> char *p;
>>>> char a[]={"Hello"};
>>>> (p=a)[0]; // This line
>>>> return 0;
>>>> }
>>
>>>> Is the behaviour of this code undefined in C++?
>>
>>> I vote that it is not UB.
>>
>>> (p=a)[0] is equivalent to *(p=a); if *(p=a) is UB then surely *(p2++) is
>>> also UB? Clearly nonsense. (p=a)[0] + (p=a+1)[0] is UB however.
>>
>> I meant '*(++p2)' not '*(p2++)'.
>>
>> /Leigh
>
> Yes I believe *(++p2) is UB too.
Bullshit; are you trolling?
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | Prasoon Saurav <prasoon@parallocity.com> |
|---|---|
| Date | 2012-01-12 06:14 -0800 |
| Message-ID | <df0bd28e-6dcb-432c-9d1e-9d9f535aa068@gj9g2000pbc.googlegroups.com> |
| In reply to | #13277 |
On Jan 12, 6:59 pm, Leigh Johnston <le...@i42.co.uk> wrote:
> On 12/01/2012 13:55, Prasoon Saurav wrote:
>
>
>
>
>
>
>
>
>
> > On Jan 12, 5:58 pm, Leigh Johnston<le...@i42.co.uk> wrote:
> >> On 12/01/2012 12:50, Leigh Johnston wrote:
>
> >>> On 11/01/2012 17:08, Novice Coder wrote:
> >>>> Here is the code
>
> >>>> int main()
> >>>> {
> >>>> char *p;
> >>>> char a[]={"Hello"};
> >>>> (p=a)[0]; // This line
> >>>> return 0;
> >>>> }
>
> >>>> Is the behaviour of this code undefined in C++?
>
> >>> I vote that it is not UB.
>
> >>> (p=a)[0] is equivalent to *(p=a); if *(p=a) is UB then surely *(p2++) is
> >>> also UB? Clearly nonsense. (p=a)[0] + (p=a+1)[0] is UB however.
>
> >> I meant '*(++p2)' not '*(p2++)'.
>
> >> /Leigh
>
> > Yes I believe *(++p2) is UB too.
>
> Bullshit; are you trolling?
>
> /Leigh
Huh?
[toc] | [prev] | [next] | [standalone]
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
Back to top | Article view | comp.lang.c++
csiph-web