C_C++语言源代码缺陷类别与名称
- 格式:docx
- 大小:28.99 KB
- 文档页数:3
C语言常见错误代码释义错误代码及错误信息错误释义error 1: Out of memory 存溢出error 2: Identifier expected 缺标识符error 3: Unknown identifier 未定义的标识符error 4: Duplicate identifier 重复定义的标识符error 5: Syntax error 语法错误error 6: Error in real constant 实型常量错误error 7: Error in integer constant 整型常量错误error 8: String constant exceeds line 字符串常量超过一行error 10: Unexpected end of file 文件非正常结束error 11: Line too long 行太长error 12: Type identifier expected 未定义的类型标识符error 13: Too many open files 打开文件太多error 14: Invalid file name 无效的文件名error 15: File not found 文件未找到error 16: Disk full 磁盘满error 17: Invalid compiler directive 无效的编译命令error 18: Too many files 文件太多error 19: Undefined type in pointer def 指针定义中未定义类型error 20: Variable identifier expected 缺变量标识符error 21: Error in type 类型错误error 22: Structure too large 结构类型太长error 23: Set base type out of range 集合基类型越界error 24: File components may not be files or objectsfile分量不能是文件或对象error 25: Invalid string length 无效的字符串长度error 26: Type mismatch 类型不匹配error 27:error 27:Invalid subrange base type 无效的子界基类型error 28:Lower bound greater than upper bound 下界超过上界error 29:Ordinal type expected 缺有序类型error 30:Integer constant expected 缺整型常量error 31:Constant expected 缺常量error 32:Integer or real constant expected 缺整型或实型常量error 33:Pointer Type identifier expected 缺指针类型标识符error 34:Invalid function result type 无效的函数结果类型error 35:Label identifier expected 缺标号标识符error 36:BEGIN expected 缺BEGINerror 37:END expected 缺ENDerror 38:Integer expression expected 缺整型表达式error 39:Ordinal expression expected 缺有序类型表达式error 40:Boolean expression expected 缺布尔表达式error 41:Operand types do not match 操作数类型不匹配error 42:Error in expression 表达式错误error 43:Illegal assignment 非法赋值error 44:Field identifier expected 缺域标识符error 45:Object file too large 目标文件太大error 46:Undefined external 未定义的外部过程与函数error 47:Invalid object file record 无效的OBJ文件格式error 48:Code segment too large 代码段太长error 49:Data segment too large 数据段太长error 50:DO expected 缺DOerror 51:Invalid PUBLIC definition 无效的PUBLIC定义error 52:Invalid EXTRN definition 无效的EXTRN定义error 53: Too many EXTRN definitions 太多的EXTRN定义error 54:OF expected 缺OFerror 55:INTERFACE expected 缺INTERFACEerror 56:Invalid relocatable reference 无效的可重定位引用error 57:THEN expected 缺THENerror 58:TO or DOWNTO expected 缺TO或DOWNTOerror 59:Undefined forward 提前引用未经定义的说明error 61:Invalid typecast 无效的类型转换error 62:Division by zero 被零除error 63:Invalid file type 无效的文件类型error 64:Cannot read or write variables of this type 不能读写此类型变量error 65:Pointer variable expected 缺指针类型变量error 66:String variable expected 缺字符串变量error 67:String expression expected 缺字符串表达式error 68:Circular unit reference 单元UNIT部件循环引用error 69:Unit name mismatch 单元名不匹配error 70:Unit version mismatch 单元版本不匹配error 71:Internal stack overflow 部堆栈溢出error 72:Unit file format error 单元文件格式错误error 73:IMPLEMENTATION expected 缺IMPLEMENTATIONerror 74:Constant and case types do not match 常量和CASE类型不匹配error 75:Record or object variable expected 缺记录或对象变量error 76:Constant out of range 常量越界error 77:File variable expected 缺文件变量error 78:Pointer expression expected 缺指针表达式error 79:Integer or real expression expected 缺整型或实型表达式error 80:Label not within current block 标号不在当前块error 81:Label already defined 标号已定义error 82:Undefined label in preceding statement part 在前面未定义标号error 83:Invalid argument 无效的参数error 84:UNIT expected 缺UNITerror 85: ";" expected 缺“;”error 86:":" expected 缺“:”error 87:"," expected 缺“,”error 88:"(" expected 缺“(”error 89:")" expected 缺“)”error 90:"=" expected 缺“=”error 91:":=" expected 缺“:=”error 92:"[" or "(." Expected 缺“[”或“(.”error 93: "]" or ".)" expected 缺“]”或“.)”error 94:"." expected 缺“.”error 95: ".." expected 缺“..”error 96:Too many variables 变量太多error 97:Invalid FOR control variable 无效的FOR循环控制变量error 98:Integer variable expected 缺整型变量error 99:Files and procedure types are not allowed here 该处不允许文件和过程类型error 100:String length mismatch 字符串长度不匹配error 101:Invalid ordering of fields 无效域顺序error 102:String constant expected 缺字符串常量error 103:Integer or real variable expected 缺整型或实型变量error 104:Ordinal variable expected 缺有序类型变量error 105:INLINE error INLINE错误error 106:Character expression expected 缺字符表达式error 107:Too many relocation items 重定位项太多error 108:Overflow in arithmetic operation 算术运算溢出error 112:CASE constant out of range CASE常量越界error 113:Error in statement 表达式错误error 114:Cannot call an interrupt procedure 不能调用中断过程error 116:Must be in 8087 mode to compile this 必须在8087模式编译error 117:T arget address not found 找不到目标地址error 118:Include files are not allowed here 该处不允许INCLUDE文件error 119:No inherited methods are accessible here 该处继承方法不可访问error 121:Invalid qualifier 无效的限定符error 122:Invalid variable reference 无效的变量引用error 123:Too many symbols 符号太多error 124:Statement part too large 语句体太长error 126:Files must be var parameters 文件必须是变量形参error 127:Too many conditional symbols 条件符号太多error 128:Misplaced conditional directive 条件指令错位error 129:ENDIF directive missing 缺ENDIF指令error 130:Error in initial conditional defines 初始条件定义错误error 131:Header does not match previous definition 和前面定义的过程或函数不匹配error 133:Cannot evaluate this expression 不能计算该表达式error 134:Expression incorrectly terminated 表达式错误结束error 135:Invalid format specifier 无效格式说明符error 136:Invalid indirect reference 无效的间接引用error 137:Structured variables are not allowed here 该处不允许结构变量error 138:Cannot evaluate without System unit 没有System单元不能计算error 139:Cannot access this symbol 不能存取符号error 140:Invalid floating point operation 无效的符号运算error 141:Cannot compile overlays to memory 不能编译覆盖模块至存error 142:Pointer or procedural variable expected 缺指针或过程变量error 143:Invalid procedure or function reference 无效的过程或函数调用error 144:Cannot overlay this unit 不能覆盖该单元error 146:File access denied 不允许文件访问error 147:Object type expected 缺对象类型error 148:Local object types are not allowed 不允许局部对象类型error 149:VIRTUAL expected 缺VIRTUALerror 150: Method identifier expected 缺方法标识符error 151:Virtual constructors are not allowed 不允许虚构造函数error 152:Constructor identifier expected 缺构造函数标识符error 153:Destructor identifier expected 缺析构函数标识符error 154:Fail only allowed within constructors 只能在构造函数使用Fail标准过程error 155:Invalid combination of opcode and operands 操作数与操作符无效组合error 156:Memory reference expected 缺存引用指针error 157:Cannot add or subtract relocatable symbols 不能加减可重定位符号error 158:Invalid register combination 无效寄存器组合error 159:286/287 instructions are not enabled 未激活286/287指令error 160:Invalid symbol reference 无效符号指针error 161:Code generation error 代码生成错误error 162:ASM expected 缺ASMerror 166:Procedure or function identifier expected 缺过程或函数标识符error 167:Cannot export this symbol 不能输出该符号error 168:Duplicate export name 外部文件名重复error 169:Executable file header toerror 170:Too many segments 段太多。
C语言程序设计中的常见错误和解决在C语言程序设计中,很多初学者经常会遇到一些常见的错误。
这些错误可能是语法错误、逻辑错误或者是其他一些常见问题导致的。
本文将介绍一些经常出现的C语言程序设计错误,并提供相应的解决方法,帮助读者更好地理解和掌握C语言编程。
一、语法错误1. 括号未匹配括号未匹配是C语言中常见的语法错误之一。
在编写代码时,经常会使用到括号,如if语句、for循环等。
如果不小心忘记关闭某个括号,就会导致括号未匹配的错误。
解决方法:仔细检查代码中的括号是否匹配,并确保每个左括号都有相应的右括号。
2. 分号丢失分号丢失是C语言中另一个常见的语法错误。
在C语言中,分号用于分隔语句,如果遗漏了分号,编译器就无法识别出语句的结束。
解决方法:检查每一行语句的末尾,确保每个语句都以分号结尾。
3. 变量未声明在使用变量之前,必须先声明该变量。
如果没有先声明变量就直接使用,编译器将无法识别该变量,从而报错。
解决方法:在使用变量之前,先进行变量声明。
二、逻辑错误1. 循环控制条件错误在编写循环语句时,控制条件的错误是常见的逻辑错误。
如果循环的控制条件错误,循环可能无法正常结束或者根本无法执行。
解决方法:仔细检查循环的控制条件,确保条件符合预期,并在循环体内更新循环变量。
2. 数组越界访问在C语言中,数组的索引从0开始,通过索引访问数组元素时,如果索引超出了数组的范围,就会导致数组越界访问的错误。
解决方法:确保数组索引在正确的范围内,避免越界访问。
如果需要遍历数组,可以使用循环来控制数组的访问范围。
三、其他常见问题及解决方法1. 变量类型不匹配在赋值操作或者表达式计算时,如果不同类型的变量之间进行操作,则会导致变量类型不匹配的错误。
解决方法:确保操作的变量类型相同或者进行强制类型转换,以保证变量类型的匹配。
2. 内存泄漏在C语言中,手动分配内存的操作是常见的。
如果分配了内存空间,但在使用完毕后未及时释放,就会导致内存泄漏。
C语⾔错误⼤全及中⽂解释1: Ambiguous operators need parentheses — 不明确的运算需要⽤括号括起2: Ambiguous symbol xxx — 不明确的符号3: Argument list syntax error — 参数表语法错误4: Array bounds missing — 丢失数组界限符5: Array size toolarge — 数组尺⼨太⼤6: Bad character in paramenters — 参数中有不适当的字符7: Bad file name format in include directive — 包含命令中⽂件名格式不正确8: Bad ifdef directive synatax — 编译预处理ifdef有语法错9: Bad undef directive syntax — 编译预处理undef有语法错10: Bit field too large — 位字段太长11: Call of non-function — 调⽤未定义的函数12: Call to function with no prototype — 时没有函数的说明13: Cannot modify a const object — 不允许修改常量对象14: Case outside of switch — 漏掉了case 语句15: Case syntax error — Case 语法错误16: Code has no effect — 代码不可能执⾏到17: Compound statement missing{ — 分程序漏掉"{"18: Conflicting type modifiers — 不明确的类型说明符19: Constant expression required — 要求常量表达式20: Constant out of range in comparison — 在⽐较中常量超出范围21: Conversion may lose significant digits — 转换时会丢失意义的数字22: Conversion of near pointer not allowed — 不允许转换近指针23: Could not find file xxx — 找不到XXX⽂件24: Declaration missing ; — 说明缺少";"25: Declaration syntax error — 说明中出现语法错误26: Default outside of switch — Default 出现在switch语句之外27: Define directive needs an identifier — 定义编译预处理需要标识符28: Division by zero — ⽤零作除数29: Do statement must have while — Do-while语句中缺少while部分30: Enum syntax error — 语法错误31: Enumeration constant syntax error — 枚举常数语法错误32: Error directive :xxx — 错误的编译预处理命令33: Error writing output file — 写输出⽂件错误34: Expression syntax error — 表达式语法错误35: Extra parameter in call — 调⽤时出现多余错误36: File name too long — ⽂件名太长37: Function call missing ) — 缺少右括号38: Fuction definition out of place — 函数定义位置错误39: Fuction should return a value — 函数必需返回⼀个值40: Goto statement missing label — 没有标号41: Hexadecimal or octal constant too large — 或8进制常数太⼤42: Illegal character x — ⾮法字符x43: Illegal initialization — ⾮法的初始化44: Illegal octal digit — ⾮法的8进制数字 A45: Illegal pointer subtraction — ⾮法的指针相减46: Illegal structure operation — ⾮法的结构体操作47: Illegal use of floating point — ⾮法的浮点运算48: Illegal use of pointer — 指针使⽤⾮法49: Improper use of a typedefsymbol — 类型定义符号使⽤不恰当50: In-line assembly not allowed — 不允许使⽤⾏间汇编51: Incompatible storage class — 存储类别不相容52: Incompatible type conversion — 不相容的53: Incorrect number format — 错误的数据格式54: Incorrect use of default — Default使⽤不当55: Invalid indirection — ⽆效的间接运算56: Invalid pointer addition — 指针相加⽆效57: Irreducible expression tree — ⽆法执⾏的表达式运算58: Lvalue required — 需要逻辑值0或⾮0值59: Macro argument syntax error — 宏参数语法错误60: Macro expansion too long — 宏的扩展以后太长61: Mismatched number of parameters in definition — 定义中参数个数不匹配62: Misplaced break — 此处不应出现63: Misplaced continue — 此处不应出现continue语句64: Misplaced decimal point — 此处不应出现⼩数点65: Misplaced elif directive — 不应编译预处理elif66: Misplaced else — 此处不应出现else67: Misplaced else directive — 此处不应出现编译预处理else68: Misplaced endif directive — 此处不应出现编译预处理endif69: Must be addressable — 必须是可以编址的70: Must take address of memory location — 必须存储定位的地址71: No declaration for function xxx — 没有函数xxx的说明72: No stack — 缺少堆栈73: No type information — 没有类型信息74: Non-portable pointer assignment — 不可移动的指针(地址常数)赋值75: Non-portable pointer comparison — 不可移动的指针(地址常数)⽐较76: Non-portable pointer conversion — 不可移动的指针(地址常数)转换77: Not a valid expression format type — 不合法的表达式格式78: Not an allowed type — 不允许使⽤的类型79: Numeric constant too large — 数值常太⼤80: Out of memory — 内存不够⽤81: Parameter xxx is never used — 能数xxx没有⽤到82: Pointer required on left side of -> — 符号->的左边必须是指针83: Possible use of xxx before definition — 在定义之前就使⽤了xxx(警告)84: Possibly incorrect assignment — 赋值可能不正确85: Redeclaration of xxx — 重复定义了xxx86: Redefinition of xxx is not identical — xxx的两次定义不⼀致87: Register allocation failure — 寄存器定址失败88: Repeat count needs an lvalue — 重复计数需要逻辑值89: Size of structure or array not known — 结构体或数给⼤⼩不确定90: Statement missing ; — 语句后缺少";"91: Structure or union syntax error — 结构体或联合体语法错误92: Structure size too large — 结构体尺⼨太⼤93: Sub scripting missing ] — 下标缺少右⽅括号94: Superfluous & with function or array — 函数或数组中有多余的"&"95: Suspicious pointer conversion — 可疑的指针转换96: Symbol limit exceeded — 符号超限97: Too few parameters in call — 时的实参少于函数的参数不98: Too many default cases — Default太多(switch语句中⼀个)99: Too many error or warning messages — 错误或警告信息太多100: Too many type in declaration — 说明中类型太多101: Too much auto memory in function — 函数⽤到的局部存储太多102: Too much global data defined in file — ⽂件中全局数据太多103: Two consecutive dots — 两个连续的句点104: Type mismatch in parameter xxx — 参数xxx类型不匹配105: Type mismatch in redeclaration of xxx — xxx重定义的类型不匹配106: Unable to create output file xxx — ⽆法建⽴输出⽂件xxx107: Unable to open include file xxx — ⽆法打开被包含的⽂件xxx108: Unable to open input file xxx — ⽆法打开输⼊⽂件xxx109: Undefined label xxx — 没有定义的标号xxx110: Undefined structure xxx — 没有定义的结构xxx111: Undefined symbol xxx — 没有定义的符号xxx112: Unexpected end of file in comment started on line xxx — 从xxx⾏开始的注解尚未结束⽂件不能结束113: Unexpected end of file in conditional started on line xxx — 从xxx 开始的条件语句尚未结束⽂件不能结束114: Unknown assemble instruction — 未知的汇编结构115: Unknown option — 未知的操作116: Unknown preprocessor directive: xxx — 不认识的预处理命令xxx117: Unreachable code — ⽆路可达的代码118: Unterminated string or character constant — 字符串缺少引号119: User break — ⽤户强⾏中断了程序120: Void functions may not return a value — Void类型的函数不应有返回值121: Wrong number of arguments — 的参数数⽬错122: xxx not an argument — xxx不是参数123: xxx not part of structure — xxx不是结构体的⼀部分124: xxx statement missing ( — xxx语句缺少左括号125: xxx statement missing ) — xxx语句缺少右括号126: xxx statement missing ; — xxx缺少分号127: xxx declared but never used — 说明了xxx但没有使⽤128: xxx is assigned a value which is never used — 给xxx赋了值但未⽤过。
C#源代码缺陷说明1.1 资源管理类1.1.1 资源泄漏缺陷描述:实现Idisposable接口的对象使用结束后未调用Dispose方法。
缺陷危害:资源泄漏可能导致非托管资源长时间被占用而无法释放,进而可能导致资源耗尽而系统不可用。
修复或避错建议:实现Idisposable接口的对象使用结束后调用Dispose方法释放非托管资源或者将实现Idisposable接口的对象放到using语句中。
示例1:不规范用法public void ExampleFun(string name){StreamReader reader = new StreamReader(name);//使用reader对象...} //方法退出前未释放reader对象的资源示例2:规范用法public void ExampleFun(string name){StreamReader reader = new StreamReader(name);try{//使用reader对象...}catch (Exception e){...}finally{reader.Close(); //Close方法调用了Dispose方法释放reader对象的资源}}示例3:规范用法public void ExampleFun(string name){using(StreamReader reader = new StreamReader(name)){//使用reader对象...}}1.2 解引用类1.2.1 null解引用缺陷描述:对于值可能为null的引用类型变量,未检查变量是否为null而直接解引用该变量。
缺陷危害:null解引用会导致程序抛出NullReferenceException类型的异常。
修复或避错建议:在解引用变量前检查变量是否为null并对变量值为null的情况进行处理。
示例4:不规范用法public void ExampleFun(){string s1 = null;string s2 = s1.Trim(); //null解引用}示例5:规范用法public void ExampleFun(){string s1 = null;if (s1 != null){string s2 = s1.Trim();}}1.3 控制语句类1.3.1 用于控制程序流程的条件表达式为常量缺陷描述:用于控制程序流程的条件表达式的结果恒为真或恒为假。
C语⾔编程中常见的五种错误及对应解决⽅案⽬录1. 未初始化的变量2. 数组越界3. 字符串溢出4. 重复释放内存5. 使⽤⽆效的⽂件指针前⾔:C 语⾔有时名声不太好,因为它不像近期的编程语⾔(⽐如 Rust)那样具有内存安全性。
但是通过额外的代码,⼀些最常见和严重的 C 语⾔错误是可以避免的。
即使是最好的程序员也⽆法完全避免错误。
这些错误可能会引⼊安全漏洞、导致程序崩溃或产⽣意外操作,具体影响要取决于程序的运⾏逻辑。
下⽂讲解了可能影响应⽤程序的五个错误以及避免它们的⽅法:1. 未初始化的变量程序启动时,系统会为其分配⼀块内存以供存储数据。
这意味着程序启动时,变量将获得内存中的⼀个随机值。
有些编程环境会在程序启动时特意将内存“清零”,因此每个变量都得以有初始的零值。
程序中的变量都以零值作为初始值,听上去是很不错的。
但是在 C 编程规范中,系统并不会初始化变量。
看⼀下这个使⽤了若⼲变量和两个数组的⽰例程序:#include <stdio.h>#include <stdlib.h>intmain(){int i, j, k;int numbers[5];int *array;puts("These variables are not initialized:");printf(" i = %d\n", i);printf(" j = %d\n", j);printf(" k = %d\n", k);puts("This array is not initialized:");for (i = 0; i < 5; i++) {printf(" numbers[%d] = %d\n", i, numbers[i]);}puts("malloc an array ...");array = malloc(sizeof(int) * 5);if (array) {puts("This malloc'ed array is not initialized:");for (i = 0; i < 5; i++) {printf(" array[%d] = %d\n", i, array[i]);}free(array);}/* done */puts("Ok");return 0;}这个程序不会初始化变量,所以变量以系统内存中的随机值作为初始值。
常数中有换行符字符串常数不能继续到第二行,除非进行下列操作:•用反斜杠结束第一行。
•用一个双引号结束第一行上的字符串,并在下一行用另一个双引号开始该字符串。
用 \n 结束第一行是不够的。
编译器错误 C2002错误消息无效的宽字符常数多字节字符常数是非法的。
通过检查下面的可能原因进行修复1.宽字符常数包含的字节比需要的多。
2.未包括标准头文件STDDEF.h。
3.宽字符不能与一般字符串连接。
4.宽字符常数之前必须是字符“L”:编译器错误 C2003错误消息应输入“defined id”标识符必须跟在预处理器关键字之后。
编译器错误 C2004错误消息应为“defined(id)”标识符必须出现在预处理器关键字之后的括号中。
也可能由于为 Visual Studio .NET 2003 进行的编译器一致性工作生成此错误:在预处理器指令中缺少括号。
如果预处理器指令缺少右括号,则编译器将生成一个错误。
编译器错误 C2005错误消息#line 应跟一个行号,却找到“token”#line 指令后面必须跟行号。
编译器错误 C2006错误消息“directive”应输入文件名,却找到“token”诸如 #include 或 #import 等指令需要文件名。
若要解决该错误,请确保 token 是一个有效文件名。
并且将该文件名放在双引号或尖括号中。
编译器错误 C2007错误消息#define 语法#define 后未出现标识符。
若要解决该错误,请使用标识符。
编译器错误 C2008错误消息“character”: 宏定义中的意外该字符紧跟在宏名之后。
若要解决该错误,宏名之后必须有一个空格。
编译器错误 C2009错误消息宏形式“identifier”重复使用宏定义的形参表多次使用该标识符。
宏的参数列表中的标识符必须是唯一的。
编译器错误 C2010错误消息“character”: 宏形参表中的意外该字符在宏定义的形参表中使用不正确。
C语⾔常见错误分析C语⾔常见错误分析:错误分类:语法错、逻辑错、运⾏错。
0.忘记定义变量:main(){x=3;y=6;printf(“%d\n”,x+y);1.C语⾔的变量⼀定要先定义才能使⽤;2.输⼊输出的数据的类型与所⽤格式说明符不⼀致int a=3;float b=4.5;printf(“%f%d\n”,a,b);它们并不是按照赋值的规则进⾏转换(如把4.5转换为4),⽽是将数据在存储单元中的形式按格式符的要求组织输出(如b占4个字节,只把最后两个字节的数据按%d,作为整数输出) 3.未注意int型数据的数值范围int型数据的数值范围(-32768~32768)int num=89101;printf(“%d”,num);会将超过低16位的数截去从⽽得到23563注意:定义了long型,⽽在输出时仍⽤”%d”说明符,仍会出现以上错误4.输⼊变量时忘记使⽤地址符scanf(“%d%d”,a,b);5.输⼊时数据的组织与要求不符对scanf函数中格式字符串中除了格式说明符外,对其他字符必须按原样输⼊6.误把”=“作为”等于”⽐较符“=“为附值运算符“==“为⽐较运算符7.语句后⾯漏分号{t=a;a=b;b=t}它是pascal的语法8.不该加分号的地⽅加了分号if(a>b);printf(“a is larger than b\n”);for(i=0;i<10;i++);scanf(“%d”,&x);printf(“%d\n”,x*x);}9.对应该有花括弧的复合语句,忘记加花括弧sum=0;i=1;while(i<=100)sum=sum+1;i++;10.括弧不配对while((c=getchar()!=‘#’)putchar(c);11.在⽤标识时,忘记了⼤写字母和⼩写字母的区别{int a,b,c;a=2;b=3;C=A+B;printf(“%d+%d=%D”,A,B,C);}12.引⽤数组元素时误⽤发圆括弧{int i,a(10);for(i=0;i<10;i++)scanf(“%d”,&a(i));}13.在定义数组时,将定义的”元素个数”误认为是”可使⽤的最⼤下标值{ int a[10]={1,2,3,4,5,6,7,8,9,10};int i;for(i=1;i<=10;i++)printf(“%d”,a[i]);}14.对⼆维或多维数组的定义和引⽤的⽅法不对{int a[5,4];printf(“%d”,a[1+2,2+2]);…}15.误以为数组名代表数组中全部元素{int a[4]={1,2,3,4};printf(“%d%d%d%d”,a);}16.混淆字符数组与字符指针的区别main(){char str[40];str=“Computer and c”;printf(“%s\n”,str);}17.在引⽤指针变量之前没有对它赋予确定的值{ char *p;scanf(“%s”,p);}{char *p,c[20];p=c;scanf(“%s”,p);}18.switch语句的各分⽀中漏写 break语句混淆字符和字符串的表⽰形式…char sex;sex=“M”;…19.使⽤⾃加(++)和⾃减(--)运算符时出的错误{int *p,a[5]={1,3,5,7,9};p=a;printf(“%d”,*p++);注意于*(++p)的区别;20.所调⽤的函数在调⽤语句之后才定义,⽽⼜在调⽤前未加说明main() {float x,y,z;x=3.5;y=-7.6;z=max(x,y);printf(“%f”,z);}float max(float x,float y){return (x>y?x:y);}21.误认为形参值的改变会影响实参的值swap(int x,int y){int t;t=x;x=y;y=t;}main(){int a,b;a=3;b=4;swap(a,b);printf(“%d,%d\n”,a,b);}22.函数的实参和形参类型不⼀致fun(float x,float y)main(){int a=3,b=4;c=fun(a,b);…}23.不同类的型指针混⽤{int i=3,*p1;float a=1.5,*p2;p1=&i;p2=&a;printf(“%d,%d\n”,*p1,*p2);}24.没有注意函数参数的求值顺序int i=3;prinft(“%d,%d,%d\n”,i,++i,++i);结果为5,5,4因为VC++是采取⾃右⾄左的顺序求函数的值C标准没有具体规定函数参数求值的顺序25.混淆数组名与指针变量的区别{ int i,a[5];for(i=0;i<5;i++)scanf(“%d”,a++);}{int a[5],*p;p=a;for(int i=0;i<5;i++)scanf(“%d”,p++)}{int a[5],*p;for(p=a;pscanf(“%d”,p);}26.混淆结构体类型与结构体变量的区别struct worker{long int num;char name[20];char sex;int age;};worker.num=187045;strcpy(/doc/449e33ccda38376baf1faefa.html ,”ZhangFun”);worker.sex=‘M’;worker.age=18;27.使⽤⽂件时忘记打开,⽤只读⽅式打开,却企图向该⽂件输出数据if(fp=fopen(“test”,”r”))==NULL){printf(“cannot open this file\n”);exit(0);ch=fgetc(fp);while(ch!=‘#’){ch=ch+4;fputc(ch,fp);ch=fgetc(fp);}C语⾔编程常见问题分析1.2.1 参数校验问题在C语⾔的函数中,⼀般都要对函数的参数进⾏校验,但是有些情况下不在函数内进⾏校验,⽽由调⽤者在外部校验,到底什么情况下应该在函数内进⾏校验,什么情况下不需要在函数内进⾏校验呢?下列原则可供读者参考。
C语言常见错误代码释义错误代码及错误信息错误释义error 1: Out of memory 内存溢出error 2: Identifier expected 缺标识符error 3: Unknown identifier 未概念的标识符error 4: Duplicate identifier 重复概念的标识符error 5: Syntax error 语法错误error 6: Error in real constant 实型常量错误error 7: Error in integer constant 整型常量错误error 8: String constant exceeds line 字符串常量超过一行error 10: Unexpected end of file 文件非正常终止error 11: Line too long 行太长error 12: Type identifier expected 未概念的类型标识符error 13: Too many open files 打开文件太多error 14: Invalid file name 无效的文件名error 15: File not found 文件未找到error 16: Disk full 磁盘满error 17: Invalid compiler directive无效的编译命令error 18: Too many files 文件太多error 19: Undefined type in pointer def 指针概念中未概念类型error 20: Variable identifier expected 缺变量标识符error 21: Error in type 类型错误error 22: Structure too large 结构类型太长error 23: Set base type out of range 集合基类型越界error 24: File components may not be files or objectsfile分量不能是文件或对象error 25: Invalid string length 无效的字符串长度error 26: Type mismatch 类型不匹配error 27:error 27:Invalid subrange base type 无效的子界基类型error 28:Lower bound greater than upper bound 下界超过上界error 29:Ordinal type expected 缺有序类型error 30:Integer constant expected 缺整型常量error 31:Constant expected 缺常量error 32:Integer or real constant expected 缺整型或实型常量error 33:Pointer Type identifier expected缺指针类型标识符error 34:Invalid function result type 无效的函数结果类型error 35:Label identifier expected 缺标号标识符error 36:BEGIN expected 缺BEGINerror 37:END expected 缺ENDerror 38:Integer expression expected 缺整型表达式error 39:Ordinal expression expected 缺有序类型表达式error 40:Boolean expression expected 缺布尔表达式error 41:Operand types do not match 操作数类型不匹配error 42:Error in expression 表达式错误error 43:Illegal assignment 非法赋值error 44:Field identifier expected 缺域标识符error 45:Object file too large 目标文件太大error 46:Undefined external 未概念的外部进程与函数error 47:Invalid object file record 无效的OBJ文件格式error 48:Code segment too large 代码段太长error 49:Data segment too large数据段太长error 50:DO expected 缺DOerror 51:Invalid PUBLIC definition 无效的PUBLIC概念error 52:Invalid EXTRN definition 无效的EXTRN概念error 53: Too many EXTRN definitions 太多的EXTRN概念error 54:OF expected 缺OFerror 55:INTERFACE expected 缺INTERFACEerror 56:Invalid relocatable reference 无效的可重定位引用error 57:THEN expected 缺THENerror 58:TO or DOWNTO expected 缺TO或DOWNTOerror 59:Undefined forward 提早引用未经概念的说明error 61:Invalid typecast 无效的类型转换error 62:Division by zero 被零除error 63:Invalid file type 无效的文件类型error 64:Cannot read or write variables of this type 不能读写此类型变量error 65:Pointer variable expected 缺指针类型变量error 66:String variable expected缺字符串变量error 67:String expression expected 缺字符串表达式error 68:Circular unit reference 单元UNIT部件循环引用error 69:Unit name mismatch 单元名不匹配error 70:Unit version mismatch 单元版本不匹配error 71:Internal stack overflow 内部堆栈溢出error 72:Unit file format error 单元文件格式错误error 73:IMPLEMENTATION expected 缺IMPLEMENTATIONerror 74:Constant and case types do not match 常量和CASE类型不匹配error 75:Record or object variable expected 缺记录或对象变量error 76:Constant out of range 常量越界error 77:File variable expected 缺文件变量error 78:Pointer expression expected 缺指针表达式error 79:Integer or real expression expected 缺整型或实型表达式error 80:Label not within current block 标号不在当前块内error 81:Label already defined 标号已概念error 82:Undefined label in preceding statement part在前面未概念标号error 83:Invalid @ argument 无效的@参数error 84:UNIT expected 缺UNITerror 85: ";" expected 缺“;”error 86: ":" expected 缺“:”error 87: "," expected 缺“,”error 88: "(" expected 缺“(”error 89: ")" expected 缺“)”error 90: "=" expected 缺“=”error 91: ":=" expected 缺“:=”error 92: "[" or "(." Expected 缺“[”或“(.”error 93: "]" or ".)" expected 缺“]”或“.)”error 94: "." expected 缺“.”error 95: ".." expected 缺“..”error 96:Too many variables 变量太多error 97:Invalid FOR control variable 无效的FOR循环操纵变量error 98:Integer variable expected缺整型变量error 99:Files and procedure types are not allowed here 该处不许诺文件和进程类型error 100:String length mismatch 字符串长度不匹配error 101:Invalid ordering of fields 无效域顺序error 102:String constant expected 缺字符串常量error 103:Integer or real variable expected 缺整型或实型变量error 104:Ordinal variable expected 缺有序类型变量error 105:INLINE error INLINE错误error 106:Character expression expected 缺字符表达式error 107:Too many relocation items 重定位项太多error 108:Overflow in arithmetic operation 算术运算溢出error 112:CASE constant out of range CASE常量越界error 113:Error in statement 表达式错误error 114:Cannot call an interrupt procedure 不能挪用中断进程error 116:Must be in 8087 mode to compile this 必需在8087模式编译error 117:Target address not found 找不到目标地址error 118:Include files are not allowed here该处不许诺INCLUDE文件error 119:No inherited methods are accessible here 该处继承方式不可访问error 121:Invalid qualifier 无效的限定符error 122:Invalid variable reference 无效的变量引用error 123:Too many symbols 符号太多error 124:Statement part too large 语句体太长error 126:Files must be var parameters 文件必需是变量形参error 127:Too many conditional symbols 条件符号太多error 128:Misplaced conditional directive 条件指令错位error 129:ENDIF directive missing 缺ENDIF指令error 130:Error in initial conditional defines 初始条件概念错误error 131:Header does not match previous definition 和前面概念的进程或函数不匹配error 133:Cannot evaluate this expression 不能计算该表达式error 134:Expression incorrectly terminated 表达式错误终止error 135:Invalid format specifier 无效格式说明符error 136:Invalid indirect reference 无效的间接引用error 137:Structured variables are not allowed here该处不许诺结构变量error 138:Cannot evaluate without System unit 没有System单元不能计算error 139:Cannot access this symbol 不能存取符号error 140:Invalid floating point operation 无效的符号运算error 141:Cannot compile overlays to memory 不能编译覆盖模块至内存error 142:Pointer or procedural variable expected 缺指针或进程变量error 143:Invalid procedure or function reference 无效的进程或函数挪用error 144:Cannot overlay this unit 不能覆盖该单元error 146:File access denied 不许诺文件访问error 147:Object type expected 缺对象类型error 148:Local object types are not allowed 不许诺局部对象类型error 149:VIRTUAL expected 缺VIRTUALerror 150: Method identifier expected 缺方式标识符error 151:Virtual constructors are not allowed 不许诺虚构造函数error 152:Constructor identifier expected 缺构造函数标识符error 153:Destructor identifier expected 缺析构函数标识符error 154:Fail only allowed within constructors只能在构造函数内利用Fail标准进程error 155:Invalid combination of opcode and operands 操作数与操作符无效组合error 156:Memory reference expected 缺内存引用指针error 157:Cannot add or subtract relocatable symbols 不能加减可重定位符号error 158:Invalid register combination 无效寄放器组合error 159:286/287 instructions are not enabled 未激活286/287指令error 160:Invalid symbol reference 无效符号指针error 161:Code generation error 代码生成错误error 162:ASM expected 缺ASMerror 166:Procedure or function identifier expected 缺进程或函数标识符error 167:Cannot export this symbol 不能输出该符号error 168:Duplicate export name 外部文件名重复error 169:Executable file header toerror 170:Too many segments 段太多。
C/C++语言源代码性能缺陷类别与名称
为方便项目组开展源代码缺陷检测工作,以下给出按检测工具错误代码列表翻译得到的
所有C/C++缺陷的类别和名称:
a) 内存管理类
1) 使用已释放的内存
2) 重复释放内存
3) 缓冲区溢出
4) 不匹配的内存释放
5) 释放非堆内存
6) 释放未分配的内存
7) 内存泄漏
8) 资源泄漏
9) 为指针分配的内存大小与类型不匹配
10) 包含动态分配内存的成员变量的类没有定义赋值操作符或拷贝构造函数
11) 指针未对齐
b) 指针使用类
1) 空指针解引用
2) 增强的指针数据加密类
c) 控制语句类
1) 用于控制程序流程的条件表达式为常量
2) 条件判别成立时相应分支无执行语句
3) switch语句为空或只包含default语句
4) 不一致的case标签
5) switch语句缺少default分支
6) switch语句存在相同的case
7) switch语句分支break缺失
d) 运算处理类
1) 非法计算
2) boolean类型变量的加法或减法运算
3) 有符号的一位位域
4) 精度丢失
5) 在条件表达式中使用赋值运算符
e) 比较判断类
1) 无效的无符号值比较
2) 无效的函数地址检查
3) 常量超出比较范围
4) Char类型变量与EOF常量比较
f) 类型转换类
1) 主机字节顺序与网络字节顺序的错误转换
2) 在浮点数和非浮点数间进行类型转换
3) 指针类型和非指针类型间进行类型转换
4) 指向浮点数的指针和指向非浮点数的指针间进行类型转换
5) 指针转换为一个可能不兼容其大小的类型
6) 表达式转换为可能不同大小的类型
7) 忽略采用typedef定义的抽象类型
8) 可疑的数组到指针的转换
g) 声明定义类
1) 函数传递参数类型错误
2) 参数被局部变量隐藏
3) 宏定义参数时括号缺失
h) 调用返回类
1) 函数返回局部变量的地址
2) 非void函数不返回值返回void值
i) 冗余类
1) 不可达代码
2) 自我赋值
3) 语句没有作用
4) 变量赋值之后未被使用
5) 空的case标签
6) 无用的标签
j) COM字符串使用类
1) 字符类型指针强制转换为BSTR类型
2) SysAllocString或SysAllocStringLen函数的第一个参数为BSTR类型
3) SysFreeString函数的参数不为BSTR类型
4) SysStringLen或SysStringByteLen函数的参数为字符类型的指针
5) SysReAllocString或SysReAllocStringLen函数的参数类型错误
6) 对BSTR类型的变量进行地址运算
7) 错误地比较BSTR类型变量的值
8) BSTR类型变量与字符类型指针进行相等或不等比较
9) 在条件运算中使用CComBSTR类型的字符串作为条件
k) 初始化类
1) 使用未初始化的堆内存
2) 未初始化的数组
3) 未初始化的变量
4) 类成员变量未初始化
5) 初始化成员的顺序不正确
l) 其他类
1) 过时的implicit int
2) 对非函数使用inline
3) 在typedef中使用packed属性
4) 结构体中使用位域
5) 没有检查字节顺序就使用自定义的字节交换宏
6) 在网络转换宏中使用不兼容的类型
7) 函数参数赋值
8) 布尔类型变量赋值错误
9) 使用缺乏一致性定义的类型
10) 内置数字类型的宏
11) 不确定的存储结果
12) char数据类型未指定是否有符号
13) 变量在同一个表达式中被使用两次且至少其中一次具有副作用
14) 死锁
15) 迭代时使用不正确的容器
16) 使用无效的迭代器
17) 引用end迭代器
18) 删除有虚方法但无虚析构函数的对象
19) 使用函数地址代替函数调用
20) 缺失typedef名称
21) 可疑的分号位置
22) 类只定义了拷贝构造函数或只定义了赋值操作符
23) 函数覆盖失败
24) 函数参数值传递过大
25) 异常对象值传递过大
26) 无限递归
27) 实参与形参不一致
参考工具:Klocwork9.2,C++Test v9.0