English 中文(简体)
难道这种“*ptr++ = *ptr + a”没有定义的行为吗?
原标题:Is this "*ptr++ = *ptr + a" undefined behavior?

诚然,我真的不需要这一答案,我是实事求是的。

诸如*ptr++ = a之类的表述完全有效,因为我们在两个物体上运行:ptr*ptr,但如果是书写*ptr++ = *ptr + a,则仍然有效?

举例来说,应考虑以下几页:

int main(void){
   int a[] = {5,7,8,9,2};

   int* p =a;

   *p++ = 76; /*altering the first element */
   *p++ = *p + 32; /*altering the second element */    

   p = a;
   int i;
   for(i = 0;i<5; i++)
      printf("%d ",*p++);

   return 0;
}

我认为,对以下表述没有什么担忧:*p++ = *p+32;,但我对所涉顺序不持怀疑态度。

最佳回答

First let us assume that p is a pointer type.
Otherwise all the operation are just syntactic sugar for function calls.

让我们把发言分为几部分。

int* p = a;

*p++ = *p + 32;

<< Sequence Point >>
// Part 1: p++
// Note the definition of post increment in the standard is (5.2.6)
// The result of the expression p++ is the value of  p  while the value of the 
// objects represented by  p  is incremented. This can be represented in pseudo code as:
(A) int*  p1 = p;
(B) p = p + 1;

// Part 2: *p (On the result of Part 1) (On *p++)
(C) int& p2 = *p1;  // Note the use of p1;

// Part 3: *p (On *p + 32)
// Note: There is no linkage between this use of  p  and the  p  in Part 1&2
(D) int& p3 = *p;

// Part 4: *p + 32;
(E) int p5 = p3 + 32; // Note the use of p3;

// Part 5: Assignment.
(F) p2 = p5;
<< Sequence Point >>

Ordering that must be preserved:
(A) Before (B)
(A) Before (C)
(D) Before (E)
(C) Before (F)
(E) Before (F)

Given the above constraints:
The compiler can re-order those instructions in several ways,
But the main point to note is that (B) can happen anywhere the only constraint on (B) is that it happen after (A) Thus the value of p3 as defined in (D) could be one of two different values depending on the exact position of (B).

As the value of p3 can not be defined here.
The resulting statement has undefined behavior.

问题回答

<代码>*ptr++ = *ptr + a 的结果未界定。 等值签字不是序列点,因此在该表中再次使用<代码>ptr的价值,导致不明确的行为。 仅考虑结果,如果在评价《区域协调制度》的表述之前添加了<代码>ptr,并将之与下述情况相比较:ptr>在《区域协调制度》的表述上加添>。

注:这并不是说,表达的结果将来自这两种设想中的任何一个。 定义不明。

基本上,你只能看两点:在最后顺序点到下一个顺序之间,对加固后的评价是一段时间的,而<条码>ptr++/代码>的表述则将<条码>ptr<>>><>>>><>>>>>t/em>的数值增减。 因此,<代码>*ptr++ = 是罚款的,因为您可以依据<代码>计算。 ptr++。

就C而言,*ptr++ = *ptr + 32 将不按6.5.2界定: “如果对 object子的副作用与对同一 object子物体的不同副作用或使用同一微粒价值进行的价值计算相比没有发生,则行为是不可界定的。 您在另一处计算中对<代码>ptr的数值作修改和尝试,但无中间序列点。

我认为,这是没有定义的。 然而我并不肯定。

但是,一个范围更广的大纲:如果某一表述使你开始怀疑它是否不明确,那么这或许就意味着你的守则不明确其意图,而且需要不明确,而且对评价顺序的依赖程度也较少。

我认为C确实是冷静的,因为你可以撰写许多非常简短的发言,这些发言确实是cra的。 我不想再这样认为:

There are not the same if that what you re asking. It will compile though...

It is legal - if PTR points to an array, and not on the last cell of that array, so incrementing PTR will point to the next object in the array.

*ptr++ = *ptr + 2 is the same as *ptr = *ptr + 2, ptr++





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?