文件描述符問題 在上面的內存問題表中,對于大多數的內存問題來說,相信對于熟悉 C/C++ 的 程序員 ,并不陌生。有一些關于 Watchpoint 和文件描述符的內容,可能會讓你看得比較模糊,對于 Watchpoint ,我會在后面講述。這一節,我就一個示例" name="description" />
在上面的內存問題表中,對于大多數的內存問題來說,相信對于熟悉C/C++的程序員,并不陌生。有一些關于Watchpoint和文件描述符的內容,可能會讓你看得比較模糊,對于Watchpoint,我會在后面講述。這一節,我就一個示例說一說文件描述述問題是如何產生的,并由此介紹一下Purify的一些特性。
先查看下面這段程序:
#include <stdio.h> main() { FILE* fp; int num; fp = fopen("./test.txt", "r"); if ( fp == NULL){ perror("Error:"); exit(-1); } fscanf(fp, "%d", &num); if ( num < 0 ){ printf("Error: the num should be greater than 0!\n"); exit(-1); } fclose(fp); } |
在當前目錄下建一個test.txt的文件,并設置其內容為-20。使用Purify編譯并運行程序:
> purify gcc -g -o testfd testfd.c
Purify 2003.06.00 Solaris 2 (32-bit) Copyright (C) 1992-2002 Rational Software Corp. All rights reserved.
Instrumenting: ccqqF6pY.o Linking
>./testfd
原文轉自:http://www.anti-gravitydesign.com