过年继续卷!掌握这些嵌入式软件开发技巧

01
02
03
--style=allman -S -U -t -n -K -p -s4 -j -q -Y -xW -xV fileName
{
if (isBar)
{
bar();
return1;
}
else {
return0;
}
}
04
05
5.1 if
(flag)
// 表示 flag 为真 if (!flag)
// 表示 flag 为假 (flag == TRUE)
if (flag ==
1 )
if (flag == FALSE)
if (flag ==
0)
(value ==
0)
if (value !=
0)
(value)
// 会让人误解 value 是布尔变量 if (!value)
(x ==
0.0)
// 隐含错误的比较,错误 EPSINON =
0.00001if ((x>=-EPSINON) && (x<=EPSINON))
//其中 EPSINON 是允许的误差(即精度),即x无限趋近于0.0 (p ==
NULL)
// p 与 NULL 显式比较,强调 p 是指针变量 if (p !=
NULL)
(p ==
0)
// 容易让人误解 p 是整型变量 if (p !=
0)
if (p)
// 容易让人误解 p 是布尔变量 if (!p)
5.2 for
(row=
0; row<
100; row++)
{
for ( col=
0; col<
5; col++ )
{
sum = sum + a[row][col];
}
}
//微信公众号:嵌入式系统 较高效率for (col=
0; col<
5; col++ )
{
for (row=
0; row<
100; row++)
{
sum = sum + a[row][col];
}
}
5.3 switch
5.4 goto
{
char *p1,*p2;
p1=(
char *)
malloc(
100);
p1=(
char *)
malloc(
200);
if(
0)
{
//do somethinggotoexit;
}
elseif(
0)
{
//do somethinggotoexit;
}
//do something//...exit:
free(p1);
free(p2);
}
intmain(){
goto_test();
return0;
}
06
PI =
3.14159;
SIZE =
100;
intarray[SIZE];
// 有的编译器认为是错误,这就必须用define了07
7.1参数的规则
;
// 良好的风格 voidset_size(int, int);
// 不良的风格 intget_size(void);
// 良好的风格 intget_size();
// 不良的风格 ;
7.2 返回值的规则
7.3 函数内部实现的规则
*
Func(void){
char str[] = “hello world”;
// str 的内存位于栈上 …
return str;
// 将导致错误 }
7.4 断言
*
memcpy(void *pvTo, constvoid *pvFrom, size_t size){
assert((pvTo !=
NULL) && (pvFrom !=
NULL));
// 【使用断言】 byte *pbTo = (byte *) pvTo;
// 防止改变 pvTo 的地址 byte *pbFrom = (byte *) pvFrom;
// 防止改变 pvFrom 的地址 while(size -- >
0 )
*pbTo ++ = *pbFrom ++ ;
return pvTo;
}
8 内存管理
8.1 内存分配方式
8.2 内存错误及其对策
8.3 指针与数组的对比
a[] = “hello”;
a[
0] = ‘X’;
cout << a <<
endl;
char *p = “world”;
// 注意 p 指向常量字符串 p[
0] = ‘X’;
// 编译器不能发现该错误 cout << p <<
endl;
a[] =
"hello";
char b[
10];
strcpy(b, a);
// 不能用 b = a; if(
strcmp(b, a) ==
0 )
// 不能用 if ( b == a) // 指针int len =
strlen(a);
char *p = (
char *)
malloc(
sizeof(
char)*(len+
1));
strcpy(p,a);
// 不要用 p = a; if(
strcmp(p, a) ==
0)
// 不要用 if (p == a) a[] =
"hello world";
char *p = a;
cout<<
sizeof(a) <<
endl;
// 12 字节 cout<<
sizeof(p) <<
endl;
// 4 字节 {
cout<<
sizeof(a) <<
endl;
// 4 字节而不是 100 字节 }
{
p = (
char *)
malloc(
sizeof(
char) * num);
}
voidtest(void){
char *str =
NULL;
get_memory(str,
100);
// str 仍然为 NULL strcpy(str,
"hello");
// 运行错误 }
{
*p = (
char *)
malloc(
sizeof(
char) * num);
}
voidtest2(void){
char *str =
NULL;
get_memory2(&str,
100);
// 注意参数是 &str,而不是 str strcpy(str,
"hello");
free(str);
}
*
get_memory3(int num){
char *p = (
char *)
malloc(
sizeof(
char) * num);
return p;
}
voidtest3(void){
char *str =
NULL;
str = get_memory3(
100);
//建议增加str指针是否为NULL判断,并清零内容strcpy(str,
"hello");
free(str);
}
*
get_string(void){
char p[] =
"hello world";
return p;
// 编译器将提出警告 }
voidtest4(void){
char *str =
NULL;
str = get_string();
// str 的内容是随机垃圾}
*
get_string2(void){
char *p =
"hello world";
return p;
}
voidtest5(void){
char *str =
NULL;
str = get_string2();
}
*p = (
char *)
malloc(
100);
strcpy(p, “hello”);
free(p);
// p 所指的内存被释放,但是 p 所指的地址仍然不变 if(p !=
NULL)
// 没有起到防错作用 {
strcpy(p, “world”);
// 出错 }
{
char *p = (
char *)
malloc(
100);
// 动态内存会自动释放吗? }
波士顿动力机器人双手会搬砖了!能抓能抬,还能搭桥扔工具包。
在航天院做程序员,是一种什么体验?
什么,这个 C 语言大坑你没见过?
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。