求教大牛,一个诡异的关于“No such device or address&quot 转义;的错误

retrying with upper case share name mount error 6 = No such device or address
retrying with upper case share name mount error 6 = No such device or address
[摘要:本日念把windows下的材料同享给Linux体系用,日了,装置了samba以后,总是提醒如图 650) this.width=650;" src="/uploads02/u02/e2/e4/e2e4e7a05b5cc5d2a951.png" title="捕捉.PNG" alt="wKioL1PePrHymJpyAAAoKJ]
今天想把windows下的资料共享给Linux系统用,日了,安装了samba之后,老是提示如图650) this.width=650;" src="/uploads02/u02/e2/e4/e2e4e7a05b5cc5d2a951.png" title="捕获.PNG" alt="wKioL1PePrHymJpyAAAoKJZC3LQ271.png" />气的牙疼,后来把用户名和密码加上之后就OK了。650) this.width=650;" src="/uploads02/u02/58/c8/58c8fb42edd5e32335e52b.png" title="捕获.PNG" alt="wKiom1PePezAMfUIAAAe3OQeGdU952.png" />本文出自 “linux菜鸟” 博客,请务必保留此出处http://ludihua./5097
感谢关注 Ithao123精品文库频道,是专门为互联网人打造的学习交流平台,全面满足互联网人工作与学习需求,更多互联网资讯尽在 IThao123!
Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。
Hadoop是一个由Apache基金会所开发的分布式系统基础架构。
用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力进行高速运算和存储。
Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上;而且它提供高吞吐量(high throughput)来访问应用程序的数据,适合那些有着超大数据集(large data set)的应用程序。HDFS放宽了(relax)POSIX的要求,可以以流的形式访问(streaming access)文件系统中的数据。
Hadoop的框架最核心的设计就是:HDFS和MapReduce。HDFS为海量的数据提供了存储,则MapReduce为海量的数据提供了计算。
产品设计是互联网产品经理的核心能力,一个好的产品经理一定在产品设计方面有扎实的功底,本专题将从互联网产品设计的几个方面谈谈产品设计
随着国内互联网的发展,产品经理岗位需求大幅增加,在国内,从事产品工作的大部分岗位为产品经理,其实现实中,很多从事产品工作的岗位是不能称为产品经理,主要原因是对产品经理的职责不明确,那产品经理的职责有哪些,本专题将详细介绍产品经理的主要职责
IThao123周刊本文链接:/blog/1127327&& (转载请注明出处)&用途说明perror命令是MySQL数据库系统中的一个工具程序,它用于打印C语言中errno错误代码的含义、也可以打印MySQL中存储引擎错误代码的含义。(perror prints a description for a system error code or for a storage engine (table handler) error code.)&由于perror是MySQL中的一个工具程序,在没有安装MySQL的机器上就无法执行,本文的示例四将给出大致的源代码供大家参考。&常用参数格式:perror &errno&打印系统错误代码errno值的含义。&格式:perror &Errcode&打印MySQL数据库系统中定义的错误代码的含义。&格式:perror &err1& &err2& ...打印多个错误代码的含义。&man perror 写道For most system errors, MySQL displays, in addition to an internal text message, the system error code in oneof the following styles:message ... (errno: #)message ... (Errcode: #)&Linux操作系统中定义的errno值如下所示。errno.h 写道#define EPERM 1 /* Operation not permitted */#define ENOENT 2 /* No such file or directory */#define ESRCH 3 /* No such process */#define EINTR 4 /* Interrupted system call */#define EIO 5 /* I/O error */#define ENXIO 6 /* No such device or address */#define E2BIG 7 /* Arg list too long */#define ENOEXEC 8 /* Exec format error */#define EBADF 9 /* Bad file number */#define ECHILD 10 /* No child processes */#define EAGAIN 11 /* Try again */#define ENOMEM 12 /* Out of memory */#define EACCES 13 /* Permission denied */#define EFAULT 14 /* Bad address */#define ENOTBLK 15 /* Block device required */#define EBUSY 16 /* Device or resource busy */#define EEXIST 17 /* File exists */#define EXDEV 18 /* Cross-device link */#define ENODEV 19 /* No such device */#define ENOTDIR 20 /* Not a directory */#define EISDIR 21 /* Is a directory */#define EINVAL 22 /* Invalid argument */#define ENFILE 23 /* File table overflow */#define EMFILE 24 /* Too many open files */#define ENOTTY 25 /* Not a typewriter */#define ETXTBSY 26 /* Text file busy */#define EFBIG 27 /* File too large */#define ENOSPC 28 /* No space left on device */#define ESPIPE 29 /* Illegal seek */#define EROFS 30 /* Read-only file system */#define EMLINK 31 /* Too many links */#define EPIPE 32 /* Broken pipe */#define EDOM 33 /* Math argument out of domain of func */#define ERANGE 34 /* Math result not representable */#define EDEADLK 35 /* Resource deadlock would occur */#define ENAMETOOLONG 36 /* File name too long */#define ENOLCK 37 /* No record locks available */#define ENOSYS 38 /* Function not implemented */#define ENOTEMPTY 39 /* Directory not empty */#define ELOOP 40 /* Too many symbolic links encountered */#define EWOULDBLOCK EAGAIN /* Operation would block */#define ENOMSG 42 /* No message of desired type */#define EIDRM 43 /* Identifier removed */#define ECHRNG 44 /* Channel number out of range */#define EL2NSYNC 45 /* Level 2 not synchronized */#define EL3HLT 46 /* Level 3 halted */#define EL3RST 47 /* Level 3 reset */#define ELNRNG 48 /* Link number out of range */#define EUNATCH 49 /* Protocol driver not attached */#define ENOCSI 50 /* No CSI structure available */#define EL2HLT 51 /* Level 2 halted */#define EBADE 52 /* Invalid exchange */#define EBADR 53 /* Invalid request descriptor */#define EXFULL 54 /* Exchange full */#define ENOANO 55 /* No anode */#define EBADRQC 56 /* Invalid request code */#define EBADSLT 57 /* Invalid slot */#define EDEADLOCK EDEADLK#define EBFONT 59 /* Bad font file format */#define ENOSTR 60 /* Device not a stream */#define ENODATA 61 /* No data available */#define ETIME 62 /* Timer expired */#define ENOSR 63 /* Out of streams resources */#define ENONET 64 /* Machine is not on the network */#define ENOPKG 65 /* Package not installed */#define EREMOTE 66 /* Object is remote */#define ENOLINK 67 /* Link has been severed */#define EADV 68 /* Advertise error */#define ESRMNT 69 /* Srmount error */#define ECOMM 70 /* Communication error on send */#define EPROTO 71 /* Protocol error */#define EMULTIHOP 72 /* Multihop attempted */#define EDOTDOT 73 /* RFS specific error */#define EBADMSG 74 /* Not a data message */#define EOVERFLOW 75 /* Value too large for defined data type */#define ENOTUNIQ 76 /* Name not unique on network */#define EBADFD 77 /* File descriptor in bad state */#define EREMCHG 78 /* Remote address changed */#define ELIBACC 79 /* Can not access a needed shared library */#define ELIBBAD 80 /* Accessing a corrupted shared library */#define ELIBSCN 81 /* .lib section in a.out corrupted */#define ELIBMAX 82 /* Attempting to link in too many shared libraries */#define ELIBEXEC 83 /* Cannot exec a shared library directly */#define EILSEQ 84 /* Illegal byte sequence */#define ERESTART 85 /* Interrupted system call should be restarted */#define ESTRPIPE 86 /* Streams pipe error */#define EUSERS 87 /* Too many users */#define ENOTSOCK 88 /* Socket operation on non-socket */#define EDESTADDRREQ 89 /* Destination address required */#define EMSGSIZE 90 /* Message too long */#define EPROTOTYPE 91 /* Protocol wrong type for socket */#define ENOPROTOOPT 92 /* Protocol not available */#define EPROTONOSUPPORT 93 /* Protocol not supported */#define ESOCKTNOSUPPORT 94 /* Socket type not supported */#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */#define EPFNOSUPPORT 96 /* Protocol family not supported */#define EAFNOSUPPORT 97 /* Address family not supported by protocol */#define EADDRINUSE 98 /* Address already in use */#define EADDRNOTAVAIL 99 /* Cannot assign requested address */#define ENETDOWN 100 /* Network is down */#define ENETUNREACH 101 /* Network is unreachable */#define ENETRESET 102 /* Network dropped connection because of reset */#define ECONNABORTED 103 /* Software caused connection abort */#define ECONNRESET 104 /* Connection reset by peer */#define ENOBUFS 105 /* No buffer space available */#define EISCONN 106 /* Transport endpoint is already connected */#define ENOTCONN 107 /* Transport endpoint is not connected */#define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */#define ETOOMANYREFS 109 /* Too many references: cannot splice */#define ETIMEDOUT 110 /* Connection timed out */#define ECONNREFUSED 111 /* Connection refused */#define EHOSTDOWN 112 /* Host is down */#define EHOSTUNREACH 113 /* No route to host */#define EALREADY 114 /* Operation already in progress */#define EINPROGRESS 115 /* Operation now in progress */#define ESTALE 116 /* Stale NFS file handle */#define EUCLEAN 117 /* Structure needs cleaning */#define ENOTNAM 118 /* Not a XENIX named type file */#define ENAVAIL 119 /* No XENIX semaphores available */#define EISNAM 120 /* Is a named type file */#define EREMOTEIO 121 /* Remote I/O error */#define EDQUOT 122 /* Quota exceeded */#define ENOMEDIUM 123 /* No medium found */#define EMEDIUMTYPE 124 /* Wrong medium type */&&&使用示例示例一 打印错误代码的含义[root@jfht ~]#&perror 1&OS error code&& 1:& Operation not permitted#define EPERM 1 /* Operation not permitted */&[root@jfht ~]#&perror 13 64&OS error code& 13:& Permission deniedOS error code& 64:& Machine is not on the network#define EACCES 13 /* Permission denied */#define ENONET 64 /* Machine is not on the network */&[root@jfht ~]#补充一个:既是操作系统errno,也是MySQL错误代码的。[root@jfht ~]#&perror 121&OS error code 121:& Remote I/O errorMySQL error code 121: Duplicate key on write or update&示例二 打印不存在的错误代码的含义要注意的是,perror似乎并不能打印SQL执行时所报的错误代码的含义。mysql&&&ERROR ): You have an error in your SQL check the manual that corresponds to your MySQL server version&for the right syntax to use near '' at line 1、&[root@jfht ~]#&perror 42000&Illegal error code: 42000[root@jfht ~]#&perror 1064&Illegal error code: 1064[root@jfht ~]#&示例三 枚举所有错误代码[root@jfht ~]#&for i in $(seq 1 10000); do perror $i; done &10000.txt 2&/dev/null&[root@jfht ~]#&cat 10000.txt&OS error code&& 1:& Operation not permittedOS error code&& 2:& No such file or directoryOS error code&& 3:& No such processOS error code&& 4:& Interrupted system callOS error code&& 5:& Input/output errorOS error code&& 6:& No such device or addressOS error code&& 7:& Argument list too longOS error code&& 8:& Exec format errorOS error code&& 9:& Bad file descriptorOS error code& 10:& No child processesOS error code& 11:& Resource temporarily unavailableOS error code& 12:& Cannot allocate memoryOS error code& 13:& Permission deniedOS error code& 14:& Bad addressOS error code& 15:& Block device requiredOS error code& 16:& Device or resource busyOS error code& 17:& File existsOS error code& 18:& Invalid cross-device linkOS error code& 19:& No such deviceOS error code& 20:& Not a directoryOS error code& 21:& Is a directoryOS error code& 22:& Invalid argumentOS error code& 23:& Too many open files in systemOS error code& 24:& Too many open filesOS error code& 25:& Inappropriate ioctl for deviceOS error code& 26:& Text file busyOS error code& 27:& File too largeOS error code& 28:& No space left on deviceOS error code& 29:& Illegal seekOS error code& 30:& Read-only file systemOS error code& 31:& Too many linksOS error code& 32:& Broken pipeOS error code& 33:& Numerical argument out of domainOS error code& 34:& Numerical result out of rangeOS error code& 35:& Resource deadlock avoidedOS error code& 36:& File name too longOS error code& 37:& No locks availableOS error code& 38:& Function not implementedOS error code& 39:& Directory not emptyOS error code& 40:& Too many levels of symbolic linksOS error code& 42:& No message of desired typeOS error code& 43:& Identifier removedOS error code& 44:& Channel number out of rangeOS error code& 45:& Level 2 not synchronizedOS error code& 46:& Level 3 haltedOS error code& 47:& Level 3 resetOS error code& 48:& Link number out of rangeOS error code& 49:& Protocol driver not attachedOS error code& 50:& No CSI structure availableOS error code& 51:& Level 2 haltedOS error code& 52:& Invalid exchangeOS error code& 53:& Invalid request descriptorOS error code& 54:& Exchange fullOS error code& 55:& No anodeOS error code& 56:& Invalid request codeOS error code& 57:& Invalid slotOS error code& 59:& Bad font file formatOS error code& 60:& Device not a streamOS error code& 61:& No data availableOS error code& 62:& Timer expiredOS error code& 63:& Out of streams resourcesOS error code& 64:& Machine is not on the networkOS error code& 65:& Package not installedOS error code& 66:& Object is remoteOS error code& 67:& Link has been severedOS error code& 68:& Advertise errorOS error code& 69:& Srmount errorOS error code& 70:& Communication error on sendOS error code& 71:& Protocol errorOS error code& 72:& Multihop attemptedOS error code& 73:& RFS specific errorOS error code& 74:& Bad messageOS error code& 75:& Value too large for defined data typeOS error code& 76:& Name not unique on networkOS error code& 77:& File descriptor in bad stateOS error code& 78:& Remote address changedOS error code& 79:& Can not access a needed shared libraryOS error code& 80:& Accessing a corrupted shared libraryOS error code& 81:& .lib section in a.out corruptedOS error code& 82:& Attempting to link in too many shared librariesOS error code& 83:& Cannot exec a shared library directlyOS error code& 84:& Invalid or incomplete multibyte or wide characterOS error code& 85:& Interrupted system call should be restartedOS error code& 86:& Streams pipe errorOS error code& 87:& Too many usersOS error code& 88:& Socket operation on non-socketOS error code& 89:& Destination address requiredOS error code& 90:& Message too longOS error code& 91:& Protocol wrong type for socketOS error code& 92:& Protocol not availableOS error code& 93:& Protocol not supportedOS error code& 94:& Socket type not supportedOS error code& 95:& Operation not supportedOS error code& 96:& Protocol family not supportedOS error code& 97:& Address family not supported by protocolOS error code& 98:& Address already in useOS error code& 99:& Cannot assign requested addressOS error code 100:& Network is downOS error code 101:& Network is unreachableOS error code 102:& Network dropped connection on resetOS error code 103:& Software caused connection abortOS error code 104:& Connection reset by peerOS error code 105:& No buffer space availableOS error code 106:& Transport endpoint is already connectedOS error code 107:& Transport endpoint is not connectedOS error code 108:& Cannot send after transport endpoint shutdownOS error code 109:& Too many references: cannot spliceOS error code 110:& Connection timed outOS error code 111:& Connection refusedOS error code 112:& Host is downOS error code 113:& No route to hostOS error code 114:& Operation already in progressOS error code 115:& Operation now in progressOS error code 116:& Stale NFS file handleOS error code 117:& Structure needs cleaningOS error code 118:& Not a XENIX named type fileOS error code 119:& No XENIX semaphores availableOS error code 120:& Is a named type fileMySQL error code 120: Didn't find key on read or updateOS error code 121:& Remote I/O errorMySQL error code 121: Duplicate key on write or updateOS error code 122:& Disk quota exceededMySQL error code 122: Internal (unspecified) error in handlerOS error code 123:& No medium foundMySQL error code 123: Someone has changed the row since it was read (while the table was locked to prevent it)OS error code 124:& Wrong medium typeMySQL error code 124: Wrong index given to functionOS error code 125:& Operation canceledMySQL error code 125: Undefined handler error 125OS error code 126:& Required key not availableMySQL error code 126: Index file is crashedOS error code 127:& Key has expiredMySQL error code 127: Record file is crashedOS error code 128:& Key has been revokedMySQL error code 128: Out of memory in engineOS error code 129:& Key was rejected by serviceMySQL error code 129: Undefined handler error 129OS error code 130:& Owner diedMySQL error code 130: Incorrect file formatOS error code 131:& State not recoverableMySQL error code 131: Command not supported by databaseMySQL error code 132: Old database fileMySQL error code 133: No record read before updateMySQL error code 134: Record was already deleted (or record file crashed)MySQL error code 135: No more room in record fileMySQL error code 136: No more room in index fileMySQL error code 137: No more records (read after end of file)MySQL error code 138: Unsupported extension used for tableMySQL error code 139: Too big rowMySQL error code 140: Wrong create optionsMySQL error code 141: Duplicate unique key or constraint on write or updateMySQL error code 142: Unknown character set used in tableMySQL error code 143: Conflicting table definitions in sub-tables of MERGE tableMySQL error code 144: Table is crashed and last repair failedMySQL error code 145: Table was marked as crashed and should be repairedMySQL error code 146: L Retry transactionMySQL error code 147: L& Restart program with a larger locktableMySQL error code 148: Updates are not allowed under a read only transactionsMySQL error code 149: L Retry transactionMySQL error code 150: Foreign key constraint is incorrectly formedMySQL error code 151: Cannot add a child rowMySQL error code 152: Cannot delete a parent rowMySQL error code 153: No savepoint with that nameMySQL error code 154: Non unique key block sizeMySQL error code 155: The table does not exist in engineMySQL error code 156: The table already existed in storage engineMySQL error code 157: Could not connect to storage engineMySQL error code 158: Unexpected null pointer found when using spatial indexMySQL error code 159: The table changed in storage engineMySQL error code 160: There's no partition in table for the given valueMySQL error code 161: Row-based binlogging of row failedMySQL error code 162: Index needed in foreign key constraintMySQL error code 163: Upholding foreign key constraints would lead to a duplicate key error in some other tableMySQL error code 164: Table needs to be upgraded before it can be usedMySQL error code 165: Table is read onlyMySQL error code 166: Failed to get next auto increment valueMySQL error code 167: Failed to set row auto increment valueMySQL error code 168: Unknown (generic) error from engineMySQL error code 169: Record is the sameMySQL error code 170: It is not possible to log this statementMySQL error code 171: The event was corrupt, leading to illegal data being readMySQL error code 172: The table is of a new format not supported by this versionMySQL error code 173: The event could not be processed no other hanlder error happenedMySQL error code 174: Got a fatal error during initialzaction of handlerMySQL error code 175: F Expected more data in fileMySQL error code 176: Read page with wrong checksumMySQL error code 177: Too many active concurrent transactions[root@jfht ~]#&示例四 perror大体的C语言代码[root@centos55 ~]#&perror&-bash: perror: command not found[root@centos55 ~]#[root@centos55 ~]#&cat &perror.c&#include &stdio.h&#include &errno.h&#include &stdlib.h&#include &string.h&int main(int argc, char* argv[]){&&&&&&& if (argc == 1)&&&&&&& {&&&&&&&&&&&&&&& fprintf(stderr, &usage: %s &errno& [&errno& ...]/n&, argv[0]);&&&&&&&&&&&&&&& exit(1);&&&&&&& }&&&&&&& else&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& for (i = 1; i & ++i)&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&&&&&& int ec = strtoul(argv[i], 0, 0);&&&&&&&&&&&&&&&&&&&&&&& fprintf(stdout, &OS error code %d: %s/n&, ec, strerror(ec));&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& exit(0);&&&&&&& }}&[root@centos55 ~]#&gcc -Wall -o /usr/bin/perror perror.c&[root@centos55 ~]#&perror 1&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&OS error code 1: Operation not permitted[root@centos55 ~]#注:该源代码无法对非法的错误代码进行判断。perror程序在提供的错误代码非法时会在标准错误设备上输出信息,而不是标准输出设备。&问题思考相关资料【1】MySQL中文手册& perror — Explain Error Codes/doc/refman/5.0/en/perror.html预处理器标识#error的目的是什么啊?
空指令,无任何效果
包含一个源代码文件
取消已定义的宏
如果给定条件为真,则编译下面代码
如果宏已经定义,则编译下面代码
如果宏没有定义,则编译下面代码
如果前面的#if给定条件不为真,当前条件为真,则编译下面代码
结束一个#if&&#else条件编译块
停止编译并显示错误信息
#error是放到异常代码段处理才写的。。比如说。if else 如果你的程序进了else 就表示出错了。
那么else里面可以打印一下错误信息。方便你自己调试用。
定义#if #else 等预定义的时候做提示,一般放在*.H头文件中。
#error预处理指令的作用是,编译程序时,只要遇到#error就会生成一个编译错误提示消息,并停止编译。其语法格式为:
#error error-message
注意,宏串error-message不用双引号包围。遇到#error指令时,错误信息被显示,可能同时还显示编译程序作者预先定义的其他内容。系统所支持的error-message请查找相关信息获得!
  assert宏的原型定义在&assert.h&中,其作用是如果它的条件返回错误,则终止程序执行,原型定义:
#include &assert.h&void assert( int expression );
  assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。请看下面的程序清单badptr.c:
#include &stdio.h&#include &assert.h&#include &stdlib.h&int main( void ){
fp = fopen( "test.txt", "w" );//以可写的方式打开一个文件,如果不存在就创建一个同名文件
assert( fp );
//所以这里不会出错
fclose( fp );
fp = fopen( "noexitfile.txt", "r" );//以只读的方式打开一个文件,如果不存在就打开文件失败
assert( fp );
//所以这里出错
fclose( fp );
//程序永远都执行不到这里来
return 0;}
[root@localhost error_process]# gcc badptr.c&[root@localhost error_process]# ./a.out&a.out: badptr.c:14: main: Assertion `fp' failed.
  已放弃使用assert()的缺点是,频繁的调用会极大的影响程序的性能,增加额外的开销。在调试结束后,可以通过在包含#include &assert.h&的语句之前插入 #define NDEBUG 来禁用assert调用,示例代码如下:
#include &stdio.h&#define NDEBUG#include &assert.h&
用法总结与注意事项:
  1)在函数开始处检验传入参数的合法性如:
int resetBufferSize(int nNewSize){  //功能:改变缓冲区大小,  //参数:nNewSize 缓冲区新长度  //返回值:缓冲区当前长度   //说明:保持原信息内容不变
nNewSize&=0表示清除缓冲区  assert(nNewSize &= 0);  assert(nNewSize &= MAX_BUFFER_SIZE);  ...}
  2)每个assert只检验一个条件,因为同时检验多个条件时,如果断言失败,无法直观的判断是哪个条件失败,如:
  不好:
assert(nOffset&=0 && nOffset+nSize&=m_nInfomationSize);
assert(nOffset &= 0);assert(nOffset+nSize &= m_nInfomationSize);
  3)不能使用改变环境的语句,因为assert只在DEBUG个生效,如果这么做,会使用程序在真正运行时遇到问题,如:
  错误:
assert(i++ & 100);
  这是因为如果出错,比如在执行之前i=100,那么这条语句就不会执行,那么i++这条命令就没有执行。
  正确:
assert(i & 100); i++;
  4)assert和后面的语句应空一行,以形成逻辑和视觉上的一致感。  5)有的地方,assert不能代替条件过滤。
assert() 宏用法
注意:assert是宏,而不是函数。在C的assert.h 头文件中。
assert宏的原型定义在&assert.h&中,其作用是如果它的条件返回错误,则终止程序执行,原型定义:
#include &assert.h&
void assert( int expression );
assert的作用是先计算表达式 expression ,如果其值为假(即为0),那么它先向标准错误流stderr打印一条出错信息,然后通过调用 abort 来终止程序运行;否则,assert()无任何作用。宏assert()一般用于确认程序的正常操作,其中表达式构造无错时才为真值。完成调试后,不必从源代码中删除assert()语句,因为宏NDEBUG有定义时,宏assert()的定义为空。[1]
请看下面的程序清单badptr.c:
#include &stdio.h&
#include &assert.h&
#include &stdlib.h&
int main( void )
fp = fopen( "test.txt", "w" );//以可写的方式打开一个文件,如果不存在就创建一个同名文件
assert( fp ); //所以这里不会出错
fclose( fp );
fp = fopen( "noexitfile.txt", "r" );//以只读的方式打开一个文件,如果不存在就打开文件失败
assert( fp ); //所以这里出错
fclose( fp ); //程序永远都执行不到这里来
[root@localhost error_process]# gcc badptr.c
[root@localhost error_process]# ./a.out
a.out: badptr.c:14: main: Assertion `fp' failed.
使用assert的缺点是,频繁的调用会极大的影响程序的性能,增加额外的开销。
在调试结束后,可以通过在包含#include &assert.h&的语句之前插入 #define NDEBUG 来禁用assert调用,示例代码如下:
#include &stdio.h&
#define NDEBUG
#include &assert.h&
用法总结与注意事项:
1)在函数开始处检验传入参数的合法性
int resetBufferSize(int nNewSize)
//功能:改变缓冲区大小,
//参数:nNewSize 缓冲区新长度
//返回值:缓冲区当前长度
//说明:保持原信息内容不变 nNewSize&=0表示清除缓冲区
assert(nNewSize &= 0);
assert(nNewSize &= MAX_BUFFER_SIZE);
2)每个assert只检验一个条件,因为同时检验多个条件时,如果断言失败,无法直观的判断是哪个条件失败
不好: assert(nOffset&=0 && nOffset+nSize&=m_nInfomationSize);
好: assert(nOffset &= 0);
assert(nOffset+nSize &= m_nInfomationSize);
3)不能使用改变环境的语句,因为assert只在DEBUG个生效,如果这么做,会使用程序在真正运行时遇到问题
错误: assert(i++ & 100)
这是因为如果出错,比如在执行之前i=100,那么这条语句就不会执行,那么i++这条命令就没有执行。
正确: assert(i & 100)
4)assert和后面的语句应空一行,以形成逻辑和视觉上的一致感
5)有的地方,assert不能代替条件过滤
注意:当对于浮点数:
#include&assert.h&
// float pi=3.14;
// assert(pi==3.14); //
float pi=3.14f;
assert (pi==3.14f);
---------------------------------------------------------
在switch语句中总是要有default子句来显示信息(Assert)。
int number = SomeMethod();
switch(number)
Trace.WriteLine("Case 1:");
Trace.WriteLine("Case 2:");
Debug.Assert(false);
C语言错误处理策略
今天写程序时,发现自己平时不爱做异常处理。(C语言的异常处理比起java的来总觉得好费劲啊~)
上网google了一下看见了一位大牛的解释,好厉害,膜拜一下
1. 返回值方式:用函数的返回值标志函数是否执行成功。比如成功返回1,失败返回0。这种方式的好处是简单方便,而且不影响效率,保持了c语言的高效率。但是仍然有问题,一个问题是代码可读性的问题,如果每个函数都有这样的返回值的话,为了保持程序的正确运行,我们必须对每个函数进行正确性验证,就是在调用函数的时候检查他的返回值,这样程序代码很大一部分就可能花费在错误处理上。第二个问题就是函数的返回值冲突的问题。假设strlen函数也可能会出错,使用这种错误处理策略他的返回值应该标志它是否执行成功,但是函数计算的字符串的长度值如何自然地传递出来?最后一个问题可能是最重要的:它不强制你处理错误,而且在不进行处理的情况下,程序仍然能够运行,但结果是不可预知的。
返回值可以判定正确错误,或则增加一些简单的错误类型判定. 其缺点在于一旦函数修改后返回值回有变化的话就很麻烦,当然最重要的还在于无法明确获知错误信息;当然优势很明显,那就是简单.
总的来说一般返回值只是做简单的正确错误判定,而不能作为获知具体错误的手段.
这里还涉及一个使用习惯的问题,一般来说返回0位正确,非0为错误,但是也有颠倒的,这里和个人编码习惯有关,所以要特别注意就是了.
2. 全局errno方式:就是在出现错误的时候,将错误代码记录到一个全局变量errno中。比如waitpid()函数在被信号中断的情况下,将errno设置为EINTR(一宏定义常量)。这种方式解决了返回值方式遇到的返回值冲突问题,而且效率方面也是非常令人愉悦的。但是它要求用户在调用函数后检查errno的值,这种保证是脆弱的,程序仍然有可能在不处理那些errno的情况下&安然&地运行,导致未定义的结果。另一个问题出在多线程方面,errno不是线程安全的,多个线程操作同一个errno会造成混乱。
在windows下errno是线程安全的,不知道linux下是线程安全还是进程安全.
在ANSI C中有定义一些基本的errno,并且操作系统也会扩展一部分,但是依然无法改变其对错误描述的匮乏.
相对windows下GetLastError组合FormatMessage的组合则显得丰满很多,可以详细了解错误信息.
作为一种补充的方式,这种全局错误代码的方式也是相当不错的,和第1种方法搭配处理可以解决90%以上的错误处理.
这里特别要推荐在windows下开发,处理错误应该尽量使用GetLastError组合FormatMessage,而非errno.
3. 错误封装:就是将每个有错误返回值的函数分别用一个函数包起来,比如waitpid()函数可以封装成Waitpid()(首字母大写),在这个函数中处理相应的错误。这种错误处理方法可以很好的解决很多问题,应该说效果很好,但是有几个方面需要商榷,一是,并不是每个函数的错误都以一种方式进行处理,另一方面,听说c语言的函数调用开销相对很高,在函数外面再包上一层会影响性能。
封装向来都是由针对性的,对于错误处理,封装的难度非常大,通用型的封装是不适用的,更多的时候是要根据具体业务来处理的.
举例个不是很恰当的例子,比如用CreateFile去打开一个文件,A程序如果打开失败就退出,那么也许可以直接封装一个函数MyOpenFile产生错了,就呼叫exit退出程序. 但是B程序去要在打开错误后尝试新建一个文件,那么MyOpenFile在这里就不适用了.
当然一些基础功能的系统函数还是可以尝试进行通用的错误封装,例如内存操作、内核对象操作等,因为这些错误产生后基本上就只能做同一件事情了,具体封装方法就是呼叫一个全局的资源清理函数,然后exit. 全局清理函数为一个引出的函数指针,由具体的AP来实现,这样就不需要担心资源在退出后无法被释放的问题了.
至于说函数呼叫的开销,除非你现在是在做MCU级别的开发,否则请丢掉这个想法吧. 何况错误处理有一个原则,是允许大部分错误产生后消耗更多的时间去处理的,错误本身就需要付出代价.
4. 异常:关于异常的说明和实现可以参考http:///admin/show/94540,它的优点是能模拟实现c++中异常的一些优点。但是这个异常机制很脆弱,使用时要注意很多问题,而且它的性能开销肯定也会不小。
既然是用C就不要试图去模仿C++的异常处理,本省异常处理就是C++中一个极其难实现的部分,很多情况都只能依赖于操作系统去有效完整的实现.
你的这个帖子我看了,作为一种模拟方法是不错,但是既然用C还在担心函数调用开销问题,那么就简单得使用setjmp longjmp来处理吧,作为一种利用c stack特性的回滚机制还是相当好用的,可以很容易的将错误回滚到一个点上,开销也不大.
5. Goto语句:,当发生错误时,利用goto语句跳到相应的错误处理函数中。因为一直以来对goto语句的偏见,和goto语句本身对程序结构性的影响,所以本人一直以来没有用过这种方式,也不知道这种方式会有什么优劣。
goto的确破坏代码结构性,但是却十分有效,有不少人对goto嗤之以鼻,那么我只能对这些人嗤之以鼻了.不能因为刀会伤人就不用刀了吧. 合理使用goto会使得错误处理更加简洁明了. 我是比较推辞使用goto来集中错误处理的,这样代码会简洁不少.
总的来说,每个方式都不是尽善尽美的,不知道大家遇到这些问题是怎么处理的?
另外希望还可以讨论如下问题:
1. C语言中的函数调用是不是像一些人说的那样成本很高?
呼叫函数是相对成本高,主要在于处理stack数据,所以如果真的要抠到这个程度,那就尽可能减少函数参数,用指针代替结构等降低stack操作开销. 如果要说到有些cpu call消耗的时钟周期较长或则cache命中失败等问题,那么...你还能写程序吗?
2. 错误处理应该如何区分用户错误和应用程序错误?
当然要区分,用户错误是给用户看的,一般都是用户操作的问题,要让用户了解自己做错了什么,这样可以有效降低以后服务的成本. 而程序自身的错误则是给程序员看的,用来排除程序的bug,这些错误一般情况不应该让客户知道,因为有时候这些错误往往携带了危险数据.
所以一般可以区分error和internal两种大的错误类型,分别处理.
3. 如果采用错误封装的方法(方法3),错误处理需要程序结束的地方应该使用exit还是abort?
前面第3种方法有说一部分,无论哪种错误,退出后一定要释放系统无法回收的资源,虽然这个难度有点大.
4. 最好的方法应该是混合使用吧,(比如返回值的方法和全局errno的方法经常混合使用)该用返回值的时候用返回值,该异常的时候异常,但是什么时候该用返回,什么时候该用异常?
其实最好使用统一的处理方法,否则很不容易维护的...
异常处理,对于做面向对象开发的开发者来说是再熟悉不过了,例如在C#中有
catch( Exception e){...}
在C++中,我们常常会使用
块来进行异常处理。
说了那么多,那么到底什么是异常处理呢?
异常处理(又称为错误处理)功能提供了处理程序运行时出现的任何意外或异常情况的方法。
异常处理一般有两种模型,一种是"终止模型",一种是"恢复模型"
"终止模型":在这种模型中,将假设错误非常关键,将以致于程序无法返回到异常发生的地方继续执行.一旦异常被抛出,就表明错误已无法挽回,也不能回来继续执行.
"恢复模型":异常处理程序的工作是修正错误,然后重新尝试调动出问题的方法,并认为的二次能成功. 对于恢复模型,通常希望异常被处理之后能继续执行程序.在这种情况下,抛出异常更像是对方法的调用--可以在Java里用这种方法进行配置,以得到类似恢复的行为.(也就是说,不是抛出异常,而是调用方法修正错误.)或者,把try块放在while循环里,这样就可以不断的进入try块,直到得到满意的结果.
二 面向对象中的异常处理
大致了解了什么是异常处理后,由于异常处理在面向对象语言中使用的比较普遍,我们就先以C++为例,做一个关于异常处理的简单例子:
问题:求两个数相除的结果。
这里,隐藏这一个错误,那就是当除数为0时,会出现,所以,我们得使用异常处理来捕捉这个异常,并抛出异常信息。
具体看代码:
1 #include &iostream&
2 #include &exception&
3 using namespace
4 class DivideError:public exception
DivideError::DivideError():exception(){}
const char* what(){
return "试图去除一个值为0的数字";
13 double quotion(int numerator,int denominator)
if(0==denominator)
//当除数为0时,抛出异常
throw DivideError();
return static_cast&double&(numerator)/
19 int main()
int number1;
//第一个数字
int number2;
//第二个数字
cout&&"请输入两个数字:" ;
while(cin&&number1&&number2){
result=quotion(number1,number2);
cout&&"结果是 :"&&result&&
catch(DivideError &divException){
cout&&"产生异常:"
&&divException.what()&&
在这个例子中,我们使用了&expection&头文件中的exception类,并使DivideError类继承了它,同时重载了虚方法what(),以给出特定的异常信息。
而C#中的异常处理类则封装的更有全面,里面封装了常用的异常处理信息,这里就不多说了。
三 C语言中的异常处理
在C语言中异常处理一般有这么几种方式:
1.使用标准C库提供了abort()和exit()两个函数,它们可以强行终止程序的运行,其声明处于&stdlib.h&头文件中。
2.使用assert(断言)宏调用,位于头文件&assert.h&中,当程序出错时,就会引发一个abort()。
3.使用errno全局变量,由C运行时库函数提供,位于头文件&errno.h&中。
4.使用goto语句,当出错时跳转。
5.使用setjmp,longjmp进行异常处理。
接下来,我们就依次对这几种方式来看看到底是怎么做的:
我们仍旧以前面处理除数为0的异常为例子。
1.使用exit()函数进行异常终止:
1 #include &stdio.h&
2 #include &stdlib.h&
3 double diva(double num1,double num2)
//两数相除函数
re=num1/num2;
9 int main()
double a,b,
printf("请输入第一个数字:");
scanf("%lf",&a);
printf("请输入第二个数字:");
scanf("%lf",&b);
//如果除数为0终止程序
exit(EXIT_FAILURE);
18 result=diva(a,b);
printf("相除的结果是: %.2lf\n",result);
20 return 0;
其中exit的定义如下:
_CRTIMP void __cdecl __MINGW_NOTHROW exit (int) __MINGW_ATTRIB_NORETURN;
exit的函数原型:void exit(int)由此,我们也可以知道EXIT_FAILURE宏应该是一个整数,exit()函数的传递参数是两个宏,一个是刚才看到的EXIT_FAILURE,还有一个是EXIT_SUCCESS从字面就可以看出一个是出错后强制终止程序,而一个是程序正常结束。他们的定义是:
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
到此,当出现异常的时候,程序是终止了,但是我们并没有捕获到异常信息,要捕获异常信息,我们可以使用注册终止函数atexit(),它的原型是这样的:intatexit(atexit_t func);
具体看如下程序:
1 #include &stdio.h&
2 #include &stdlib.h&
3 void Exception(void)
//注册终止函数,通过挂接到此函数,捕获异常信息
printf("试图去除以一个为0的数字,出现异常!\n");
7 int main()
double a,b,
printf("请输入第一个数字:");
scanf("%lf",&a);
printf("请输入第二个数字:");
scanf("%lf",&b);
//如果除数为0终止程序 ,并挂接到模拟异常捕获的注册函数
atexit(Exception);
exit(EXIT_FAILURE);
result=diva(a,b);
printf("相除的结果是: %.2lf\n",result);
22 return 0;
这里需要注意的是,atexit()函数总是被执行的,就算没有exit()函数,当程序结束时也会被执行。并且,可以挂接多个注册函数,按照堆栈结构进行执行。abort()函数与exit()函数类似,当出错时,能使得程序正常退出,这里就不多说了。
2.使用assert()进行异常处理:
assert()是一个调试程序时经常使用的宏,切记,它不是一个函数,在程序运行时它计算括号内的表达式,如果表达式为FALSE
程序将报告错误,并终止执行。如果表达式不为0,则继续执行后面的语句。这个宏通常原来判断程序中是否出现了明显非法的数据,如果出现了终止程序以免导致严重后果,同时也便于查找错误。
另外需要注意的是:assert只有在Debug版本中才有效,如果编译为Release版本则被忽略。
我们就前面的问题,使用assert断言进行异常终止操作:构造可能出现出错的断言表达式:assert(number!=0)这样,当除数为0的时候,表达式就为false,程序报告错误,并终止执行。
代码如下:
#include &stdio.h&
#include &assert.h&
double diva(double num1,double num2)
//两数相除函数
re=num1/num2;
int main()
printf("请输入第一个数字:");
scanf("%lf",&a);
printf("请输入第二个数字:");
scanf("%lf",&b);
assert(0!=b);
//构造断言表达式,捕获预期异常错误
result=diva(a,b);
printf("相除的结果是: %.2lf\n",result);
3.使用errno全局变量,进行异常处理:
errno全局变量主要在调式中,当系统API函数发生异常的时候,将errno变量赋予一个整数值,根据查看这个值来推测出错的原因。
其中的各个整数值都有一个相应的宏定义,表示不同的异常原因:
#define EPERM
/* Operation not permitted */
/* No such file or directory */
/* No such process */
/* Interrupted function call */
/* Input/output error */
/* No such device or address */
/* Arg list too long */
/* Exec format error */
/* Bad file descriptor */
/* No child processes */
/* Resource temporarily unavailable */
/* Not enough space */
/* Permission denied */
/* Bad address */
/* 15 - Unknown Error */
/* strerror reports "Resource device" */
/* File exists */
/* Improper link (cross-device link?) */
/* No such device */
/* Not a directory */
/* Is a directory */
/* Invalid argument */
/* Too many open files in system */
/* Too many open files */
/* Inappropriate I/O control operation */
/* 26 - Unknown Error */
/* File too large */
/* No space left on device */
/* Invalid seek (seek on a pipe?) */
/* Read-only file system */
/* Too many links */
/* Broken pipe */
/* Domain error (math functions) */
/* Result too large (possibly too small) */
/* 35 - Unknown Error */
/* Resource deadlock avoided (non-Cyg) */
/* 37 - Unknown Error */
ENAMETOOLONG
/* Filename too long (91 in Cyg?) */
/* No locks available (46 in Cyg?) */
/* Function not implemented (88 in Cyg?) */
/* Directory not empty (90 in Cyg?) */
/* Illegal byte sequence */
这里我们就不以前面的除数为0的例子来进行异常处理了,因为我不知道如何定义自己特定错误的errno,如果哪位知道,希望能给出方法。我以一个网上的例子来说明它的使用方法:
#include &errno.h&
#include &math.h&
#include &stdio.h&
int main(void)
errno = 0;
if (NULL == fopen("d:\\1.txt", "rb"))
printf("%d", errno);
printf("%d", errno);
这里试图打开一个d盘的文件,如果文件不存在,这是查看errno的值,结果是2、
当文件存在时,errno的值为初始值0。然后查看值为2的错误信息,在宏定义那边#define
/* No such file or directory */
便知道错误的原因了。
4.使用goto语句进行异常处理:
goto语句相信大家都很熟悉,是一个跳转语句,我们还是以除数为0的例子,来构造一个异常处理的例子:
#include &stdio.h&
double diva(double num1,double num2)
//两数相除函数
re=num1/num2;
int main()
int tag=0;
double a,b,
if(1==tag)
printf("除数为0,出现异常\n");
printf("请输入第一个数字:");
scanf("%lf",&a);
printf("请输入第二个数字:");
scanf("%lf",&b);
//捕获异常(或许这么说并不恰当,暂且这么理解)
//抛出异常
result=diva(a,b);
printf("%d\n",errno);
printf("相除的结果是: %.2lf\n",result);
5.使用setjmp和longjmp进行异常捕获与处理:
setjmp和longjmp是非局部跳转,类似goto跳转作用,但是goto语句具有局限性,只能在局部进行跳转,当需要跳转到非一个函数内的地方时就需要用到setjmp和longjmp。setjmp函数用于保存程序的运行时的堆栈环境,接下来的其它地方,你可以通过调用longjmp函数来恢复先前被保存的程序堆栈环境。异常处理基本方法:
使用setjmp设置一个跳转点,然后在程序其他地方调用longjmp跳转到该点(抛出异常).
代码如下所示:
#include &stdio.h&
#include &setjmp.h&
void Exception(void)
longjmp(j,1);
double diva(double num1,double num2)
//两数相除函数
re=num1/num2;
int main()
double a,b,
printf("请输入第一个数字:");
scanf("%lf",&a);
printf("请输入第二个数字:");
if(setjmp(j)==0)
scanf("%lf",&b);
Exception();
result=diva(a,b);
printf("相除的结果是: %.2lf\n",result);
printf("试图除以一个为0的数字\n");
}四 总结:
除了以上几种方法之外,另外还有使用信号量等等方法进行异常处理。当然在实际开发中每个人都有各种调式的技巧,而且这文章并不是说明异常处理一定要这样做,这只是对一般做法的一些总结,也不要乱使用异常处理,如果弄的不好就严重影响了程序的效率和结构,就像设计模式一样,不能胡乱使用。
作者:Skiper
出处:http:///vimsk
错误处理 & (Error Handling)
错误代号与讯息 & (errno, strerror, perror) 与错误代号相关的讯息输出方法。
错误讯息列表 & (strerror) 用 strerror 列出所有内建的错误讯息。
档案错误 & (ferror,clearerr) 档案读取写入错误,清除错误,继续执行下去。
短程跳跃 & (goto) 函数内的跳跃,不可跨越函数。
长程跳跃 & (setjump 与 longjump) 在错误发生时,储存行程状态,执行特定程式的方法
讯号机制 & (signal) 拦截中断讯号的处理机制。
模拟 try ... catch & 使用跳跃机制 (setjump, longjump) 模拟 try & catch 的错误
捕捉机制。
阅读(...) 评论()

我要回帖

更多关于 quot 转译回来 的文章

 

随机推荐