Cha*_*rly 2 linux gcc g++ dev-null
根据另一个线程的提示,我想分析指针,如果它们的解引用会导致分段错误.想法是编写这样的代码:
bool IsPointerValid( void* pPointer )
{
// when opening "/tmp/hugo" here, it works fine... but not with /dev/null??
int iFD = open( "/dev/null", O_WRONLY );
int iBytesWritten = write( iFD, pPointer, 4 );
close( iFD );
return iBytesWritten > 0;
}
Run Code Online (Sandbox Code Playgroud)
但whatevery我传递到IsPointerValid(..),它返回总是如此-因为iBytesWritten始终4.但是当打开"/tmp/testfile"或者是fifo时,它会像预期的那样工作:传递NULL指针write(..),它返回-1.
这种特殊治疗的原因是"/dev/null"什么?
谢谢,
查理
因为它是一个特殊的文件,所以它带来了自己的定义read和write.(linux内核使用驱动程序层中的函数指针实现多态性).显然,设备write提供的版本/dev/null不使用您传入的指针.