在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/ C++問(wèn)答
情皺 回答

可重入鎖不是這么用的,一般是在面向?qū)ο笾惺褂?,比?/p>

class A:
   def f1(self):
       mutex.acquire()
       try:
           #do something
       finally:
           mutex.release()
   def f2(self):
       mutex.acquire()
       try:
           #do something
       finally:
           mutex.release()

def run1(obj):
    obj.f1()
    obj.f2()
    
def run2(obj):
    obj.f2()
    obj.f1()

obj1 = A()
t1 = threading.Thread(target=run1, args=(obj1, ))
t2 = threading.Thread(target=run2, args=(obj1, ))
t1.start()
t2.start()

調(diào)用順序不同,而且都需要同步的時(shí)候,如果不用遞歸鎖,會(huì)死鎖。如果f1或f2不加鎖,數(shù)據(jù)不同步,報(bào)錯(cuò)

痞性 回答

new is not an operator!

In c++, new and operator are both keywords. new int(x) is a(n) (new-)expression. operator new is a function. new operator in your title is new-expression indeed. new-expression will invoke oeprator new function.

placement new的作用就是在operator new分配好的內(nèi)存上執(zhí)行對(duì)象的構(gòu)造,
Yes, that's true. To help you understand, here is a sample:
char* ptr = new char[sizeof(T)]; // ptr use a new-expression(newchar [sizeof(T)] to allocate memory, char is guaranteed to be sizeof 1
T* tptr = new(ptr) T;            // Here is placement new

In a nutshell, placement new will use already allocated memory to construct the object. Where is the already allocated memory from? Either from new expression(free store) or allocated from activation record like int buffer[10], both ok.

那么new operator使用了operator new來(lái)申請(qǐng)內(nèi)存之后是使用了placement new嗎?如果沒(méi)有話是怎么構(gòu)造的呢?和placement new有關(guān)系嗎?

Above is my answer to your questions

BTW, from the case int buffer[10], we can see pre-new-expression is not a must for placement new(however, note that placement new itself is a new-expression, which will invoke operator new function because all it does is just construct here). If your question is "will placement new always be after operator new/new-expression", it will be a good question.

Update

One year ago, I was confused about how to combine operator new with the constructor then asked a question, FrankHB answered my question: https://tieba.baidu.com/p/508... Now, look back to this question, it is a X-Y question, what really confused me was how does new expression invoke constructor, so it is not related to placement new. Hope it will also inspire you.

Update again

所以我認(rèn)為或許自己和您一年前的疑問(wèn)相似,內(nèi)存申請(qǐng)和構(gòu)造函數(shù)這兩個(gè)過(guò)程是如何結(jié)合的呢?
the word combination(結(jié)合) is not properly now(I also make such mistake as said above), let me re-organize my wording:

new expression does two things:

  1. allocate memory
  2. initialization of the object

You and I(one year ago) are both confused about how does compiler initialize the object(via constructor or else) after allocating. Yes, I mentioned compiler, because C++ standard guarantee new will do the two things, but didn't how to, so, its compiler's work. So, it is not c++'s work, just compiler's. Now, we can see the assembly:

struct Foo
{
    Foo(int i) {}
};
int main()
{
    auto *a = new Foo(1);
}  

-O0:

Foo::Foo(int):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        mov     DWORD PTR [rbp-12], esi
        pop     rbp
        ret
main:
        push    rbp
        mov     rbp, rsp
        push    rbx
        sub     rsp, 24
        mov     edi, 1
        call    operator new(unsigned long)
        mov     rbx, rax
        mov     esi, 1
        mov     rdi, rbx
        call    Foo::Foo(int)
        mov     QWORD PTR [rbp-24], rbx
        mov     eax, 0
        add     rsp, 24
        pop     rbx
        pop     rbp
        ret

codes related the new expression is follows: a

    mov     edi, 1
    call    operator new(unsigned long)
    mov     rbx, rax
    mov     esi, 1
    mov     rdi, rbx
    call    Foo::Foo(int)
    mov     QWORD PTR [rbp-24], rbx
    

Now, it is clear enough, right? assemble calls two procedures, oeprator new and Foo::Foo(int)

That's all, cheers!

So, your question is how the two combined?

絯孑氣 回答

謝邀先!
不過(guò)先提醒你稍微學(xué)一下markdown的語(yǔ)法,把語(yǔ)句標(biāo)示出來(lái),否則直接插入的語(yǔ)句可能會(huì)被系統(tǒng)處理而不能表達(dá)完整的意思了。請(qǐng)先處理這點(diǎn)。

吢丕 回答

應(yīng)該是發(fā)件太頻繁,被當(dāng)成垃圾郵件制造者了。

乖乖瀦 回答

vector<int>無(wú)需釋放內(nèi)存,但是vector<string>需要swap釋放
當(dāng)然你會(huì)疑惑 int無(wú)需釋放內(nèi)存,string 也無(wú)需釋放內(nèi)存(會(huì)自動(dòng)析構(gòu)釋放),但是為什么在vector中不一樣?
vector<int>等內(nèi)置類型無(wú)需釋放內(nèi)存,自動(dòng)釋放。string類型的本質(zhì)是指針,vector<string> ,vector<int*>等指針類型需要手動(dòng)swap釋放。

陪我終 回答
"010203".match(/\d{2}/g);

或者

"010203".split(/\B(?=(?:\d{2})+\b)/);

或者

"010203".split(/\B(?=0)/);
墨小白 回答

解決辦法如下。post這樣發(fā)送請(qǐng)求就好了

axios.post( apiUrl, qs.stringify({name: 'testName', pass: 'testPass'}), {
  headers: {
     'Content-Type': 'application/x-www-form-urlencoded'
  }
}).then(//***).catch(//***)
  1. install qs module, and use qs.stringify(dataObject) to format your data object
  2. add axios config
{
  headers: {
     'Content-Type': 'application/x-www-form-urlencoded'
  }
}
別傷我 回答

behavior 屬性只有IE兼容吧? 應(yīng)該要盡量避免使用

忠妾 回答

boost不是標(biāo)準(zhǔn)庫(kù),但它仍然是庫(kù)。<>就是優(yōu)先搜庫(kù),沒(méi)毛病。
你要是用其他的庫(kù),也得用<>

冷眸 回答

首先,n<2 類似于初始化,F(xiàn)ib(0) = 0, Fib(1) = 1;第一個(gè)return就返回了,不會(huì)執(zhí)行下面的打印。
然后,n>2時(shí),F(xiàn)ib(2) = Fib(1) + Fib(0),會(huì)打印Fib(1)和Fib(0)。
這個(gè)就是斐波那契數(shù)列的一邊表達(dá)式:f(n) = f(n-1) + f(n-2)。

哎呦喂 回答

@course.comments.sum{ |cc| cc.comment.length }

薄荷綠 回答
//testgo.h
#ifdef __cplusplus
extern "C"{
#endif
extern long calladdr(long addr);
#ifdef __cplusplus
}
#endif
//testgo.cpp
#include "testgo.h"

long calladdr(long addr){
    return addr;
}
//test.go
package main

// #include "testgo.h"
// #cgo LDFLAGS: ${SRCDIR}/testgo.so -lstdc++
import "C"
import "fmt"
import "unsafe"

type Work struct {
    Name string
}

func main() {
    data := &Work{Name: "aaa"}
    ad := C.long(uintptr(unsafe.Pointer(data)))

    addr := C.calladdr(ad)
    newdata := (*Work)(unsafe.Pointer(uintptr(addr)))
    fmt.Println("newdata is", newdata)
}

應(yīng)該可以滿足要求

我認(rèn)為使用define的最大價(jià)值在于是否可以將大段的重復(fù)性代碼濃縮為define,后續(xù)使用可能用一個(gè)宏就解決大段的重復(fù)性代碼。比如mfc中的消息映射,成功的將很多重復(fù)性的代碼濃縮為一兩個(gè)宏,雖然mfc本身很復(fù)雜,但是它的宏定義的使用堪稱教科書。其他常用的使用場(chǎng)景比如定義不定長(zhǎng)的函數(shù),你給出的這個(gè)例子就是。比如與操作系統(tǒng)和編譯環(huán)境有關(guān)的,比如__FILE__,__LINE__,DEBUG等。至于你講的可能的代替,我認(rèn)為是定義常量、定義簡(jiǎn)單的函數(shù)等。

涼心人 回答

select操作不會(huì)造成死鎖。我猜測(cè):update語(yǔ)句有大字段更新,導(dǎo)致事務(wù)時(shí)間較長(zhǎng)(即長(zhǎng)事務(wù)),同時(shí),其他select語(yǔ)句引起update操作,當(dāng)update同一條記錄時(shí),就會(huì)導(dǎo)致死鎖(等待長(zhǎng)事務(wù)的完成)。

瘋子范 回答

當(dāng)然有意義。
你覺(jué)得你用不到,很多時(shí)候是框架幫你做了這些事情。
我舉幾個(gè)例子

  • 怎么判斷是 ajax 請(qǐng)求,是不是需要 http header 的信息
  • 怎么判斷請(qǐng)求是 post、get、put ?
  • 怎么設(shè)置 cookie

誠(chéng)然,其實(shí)不用這些標(biāo)準(zhǔn)也能實(shí)現(xiàn)很多東西,但是我們?yōu)槭裁凑f(shuō)面向接口編程,就是不需要知道實(shí)現(xiàn)細(xì)節(jié),也能很好的使用。

淚染裳 回答

&的用意我只知道除了是引用和位址之外,它還有一個(gè)功能就是防止拷貝(比較有效率)。

void swap(int &a, int &b)

void swap(int a, int b)

的差別是一個(gè)是整數(shù)a b的本身,另一個(gè)是整數(shù)a b的副本。如果你使用第二個(gè)swap函數(shù)來(lái)進(jìn)行交換整數(shù)的話,你會(huì)發(fā)現(xiàn)沒(méi)有換到。因?yàn)槟闼幚淼闹皇窃诟北旧线M(jìn)行交換整數(shù)而已,它本身的自己卻沒(méi)有更動(dòng)到。


Box operator+(const Box& b)
    {
        Box box;
        box.length = this->length + b.length;
        box.breadth = this->breadth + b.breadth;
        box.height = this->height + b.height;
        return box;
    }

知道了&的一些概念后,你可能會(huì)問(wèn)為什么 Box& b前面要放 const!
因?yàn)槟阒?&會(huì)改變對(duì)象本身,然后我們只是想用b里面的變數(shù)且不想更動(dòng)到它們,所以前面加const的用意是防止改變對(duì)象!

總結(jié)來(lái)說(shuō),const B &b 達(dá)到了無(wú)副本 + 無(wú)法修改的目的。

不討囍 回答

你的中文編碼是UTF8的?如果是查看一下你對(duì)應(yīng)的要匹配的中文的UTF8編碼,然后在regex正則表達(dá)式中使用對(duì)應(yīng)的UTF8編碼匹配。

如 sserver 日志所示

 15 E0403 17:24:39.589262  4886 Utils.cc:221] unable to request data from: https://127.0.0.1/get_user_id_list?last_id=0, error: Couldn't connect to server
 16 E0403 17:24:39.589727  4886 StratumServer.cc:480] http get request user list fail, url: https://127.0.0.1/get_user_id_list?last_id=0
 17 E0403 17:24:39.589777  4886 StratumServer.cc:547] update user list failure
 18 E0403 17:24:39.589867  4886 StratumServer.cc:776] fail to setup server
 19 F0403 17:24:39.589912  4886 StratumServerMain.cc:171] init failure

參考 sserver.cfg 的注釋,你需要自己搭建一個(gè)網(wǎng)站服務(wù)器(xx.yy),并提供一個(gè)網(wǎng)站接口(/get_user_id_list),然后把網(wǎng)址寫入 sserver.cfg 文件的 list_id_api_url 項(xiàng),如下

users = {
    list_id_api_url = "http://xx.yy/get_user_id_list";
};

你可以在本機(jī)搭建一個(gè) apache 或者 nginx 服務(wù)器,然后按照
https://github.com/btccom/btc...
上的說(shuō)明操作便可。

墨小白 回答

sem_init函數(shù)入?yún)⑿枰獋魅虢Y(jié)構(gòu)體地址,即sem_t mutex;
sem_init(&mutex);

還有看到你buffer定義是數(shù)組指針,但感覺(jué)初始化是按照字符數(shù)組初始化的。

雅痞 回答

你可以裝個(gè)瀏覽器版的插件,就不用裝在本機(jī)了。圖片描述