在開發多線程的時候,想在test函數中創建一個線程,並把參數傳給handler這個函數,這個參數定義成了一個int型(4字節)
在編譯時有丟失精度的報錯,原因是64位機器中,指針佔8字節;4字節int轉換成8字節指針時,是從低精度轉向高精度,此時不會報錯,而在handler中,從高精度8字節的指針,轉向低精度4字節的int時,損失了精度(有潛在的溢出可能性)就會報錯。
見下表,在64位機器中long佔8字節,只需將int換成long,就不會出現這個報錯了。
報錯的程序:
void test()
{
......
int clientfd = accept(listenfd, NULL, NULL);
......
pthread_create(&tid, NULL, handler, (int *)clientfd);
......
}
void *handler(void *fd)
{
......
int arg = (int)fd;//此處報錯, error: cast from ‘void*’ to ‘int’ loses precision
......
}