【发布时间】:2011-02-17 13:35:36
【问题描述】:
根据another thread 的提示,我想分析指针,它们的解除引用是否会导致分段错误。这个想法是编写这样的代码:
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;
}
但是无论我传递给IsPointerValid(..),它总是返回true - 因为iBytesWritten 总是4。但是当打开"/tmp/testfile" 或fifo 时,它会像预期的那样工作:将NULL 指针传递给write(..),它会返回-1。
"/dev/null"这种特殊待遇的原因是什么?
谢谢,
查理
【问题讨论】: