甚么让你对C/C++如此恐惧?
晦涩的语法?还是优秀IDE的欠缺?
我想那都不是问题,最多的多是1个类似这样的毛病:
段毛病(Segmentation fault)
这是新手没法避免的毛病,也是老手极力躲避也常常遇到的毛病。
本篇,试图简略地剖析1段会引发这个毛病的程序,带来1些启发。
先看两份代码,1份是毛病的.
毛病代码
#include "string.h"
#include
#include
void func1(char ** dest,char * src,int n) {
(*dest) = (char*)malloc(sizeof(char)*n);
strcpy(*dest,src);
}
int main(int argc,char** args) {
char ** p = NULL;
char str[] = "foreach_break";
int len = sizeof(str);
printf("%d
",len);
func1(p,str,len);
printf("%s
",*