第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C+程序員應該知道哪些常見的未定義行為?

C+程序員應該知道哪些常見的未定義行為?

C C++
holdtom 2019-05-30 17:40:44
C+程序員應該知道哪些常見的未定義行為?C+程序員應該知道哪些常見的未定義行為?比如說:a[i] = i++;
查看完整描述

3 回答

?
不負相思意

TA貢獻1777條經(jīng)驗 獲得超10個贊

計算函數(shù)參數(shù)的順序是未指定行為。(這不會使你的節(jié)目崩潰,爆炸,或訂購比薩餅.不像未定行為.)

唯一的要求是在調用函數(shù)之前必須對所有參數(shù)進行完全評估。


這是:

// The simple obvious one.callFunc(getA(),getB());

可以等效于以下內容:

int a = getA();int b = getB();callFunc(a,b);

或者這個:

int b = getB();int a = getA();callFunc(a,b);

這兩者都可以;這取決于編譯器。結果可能很重要,取決于副作用。


查看完整回答
反對 回復 2019-05-30
?
繁星點點滴滴

TA貢獻1803條經(jīng)驗 獲得超3個贊


編譯器可以自由地重新排序表達式的計算部分(假設含義不變)。


根據(jù)原來的問題:


a[i] = i++;


// This expression has three parts:

(a) a[i]

(b) i++

(c) Assign (b) to (a)


// (c) is guaranteed to happen after (a) and (b)

// But (a) and (b) can be done in either order.

// See n2521 Section 5.17

// (b) increments i but returns the original value.

// See n2521 Section 5.2.6

// Thus this expression can be written as:


int rhs  = i++;

int lhs& = a[i];

lhs = rhs;


// or

int lhs& = a[i];

int rhs  = i++;

lhs = rhs;

雙重檢查鎖定。一個容易犯的錯誤。


A* a = new A("plop");


// Looks simple enough.

// But this can be split into three parts.

(a) allocate Memory

(b) Call constructor

(c) Assign value to 'a'


// No problem here:

// The compiler is allowed to do this:

(a) allocate Memory

(c) Assign value to 'a'

(b) Call constructor.

// This is because the whole thing is between two sequence points.


// So what is the big deal.

// Simple Double checked lock. (I know there are many other problems with this).

if (a == null) // (Point B)

{

    Lock   lock(mutex);

    if (a == null)

    {

        a = new A("Plop");  // (Point A).

    }

}

a->doStuff();


// Think of this situation.

// Thread 1: Reaches point A. Executes (a)(c)

// Thread 1: Is about to do (b) and gets unscheduled.

// Thread 2: Reaches point B. It can now skip the if block

//           Remember (c) has been done thus 'a' is not NULL.

//           But the memory has not been initialized.

//           Thread 2 now executes doStuff() on an uninitialized variable.


// The solution to this problem is to move the assignment of 'a'

// To the other side of the sequence point.

if (a == null) // (Point B)

{

    Lock   lock(mutex);

    if (a == null)

    {

        A* tmp = new A("Plop");  // (Point A).

        a = tmp;

    }

}

a->doStuff();


// Of course there are still other problems because of C++ support for

// threads. But hopefully these are addresses in the next standard.


查看完整回答
反對 回復 2019-05-30
  • 3 回答
  • 0 關注
  • 675 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號