# indent 可以改變你的程式風格
GNU STYLE: -gnu or
-nbad -bap -nbc -bbo -bl -bli2 -bls -ncdb -nce -cp1 -cs -di2 -ndj -nfc1 -nfca -hnl -i2 -ip5 -lp -pcs -nprs -psl -saf -sai -saw -nsc -nsob
-nbad: Do not force a blank line after every block of declarations.
-bap: forces a blank line after every procedure body.
-nbc: Do not Force newline after commas in declaration.
-bbo: Prefer to break long lines before boolean operators.
-bl: Put braces on line after if.
-blin: Indent braces n spaces.
-bls: Put braces on the line after struct declaration lines.
-ncdb: Do not put comment delimiters on blank lines.
-nce: Do not cuddle } and else.
-cp1: Put comments to the right of #else and #endif statements in column n.
-cs: Put a space after a cast operator.
-di2: Put variables in column n.
-ndj:
-nfc1: Do not format comments in the first column as normal.
-nfca: Do not format any comments.
-hnl: Prefer to break long lines at the position of newlines in the input.
-i2: Set indentation level to n spaces.
-ip5: Indent parameter types in old-style function definitions by n space.
-lp: Line up continued lines at parentheses.
-pcs: put space after the function in function calls.
-nprs: Do not put a space after every '(' and before every ')'.
-psl: Put the type of a procedure on the line before its name.
-saf: put a space after every for.
-sai: put a space after every if.
-saw: put a space after every while.
-nsc: Do not put the `*´ character at the left of comments.
-nsob: Do not swallow optional blank lines.
2009年1月14日 星期三
2009年1月9日 星期五
Big Array
看看下面的例子
#include <stdio.h>
int main()
{
char buf1[3145728];
char buf2[3145728];
char buf3[3145728];
char buf4[3145728];
printf("ok\n");
return 0;
}
程式是沒有錯的,但是能執行嗎?
電腦會跟你說 Segmentation fault !!!
但是假如只有buf1及buf2就能順利執行。
這樣的問題就發生在我們建立的buf是在memory的stack,而stack並不夠大到可以容納我們的array。這就是所謂的stack overflow.
但假如我們使用global variable or dynamic memory allocate的方式就解決了,如下面的例子:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *buf1;
char *buf2;
char *buf3;
char *buf4;
char *buf5;
char *buf6;
if ((buf1 = (char *) malloc (3145728)) == NULL) {
printf("buf1 error\n");
return 9;
}
printf("buf1 ok\n");
if ((buf2 = (char *) malloc (3145728)) == NULL) {
printf("buf2 error\n");
return 9;
}
printf("buf2 ok\n");
.
.
.
if ((buf6 = (char *) malloc (3145728)) == NULL) {
printf("buf6 error\n");
return 9;
}
printf("buf6 ok\n");
free(buf1);
free(buf2);
free(buf3);
free(buf4);
free(buf5);
free(buf6);
return 0;
}
這時你的Array配置在memory的heap,可以讓你有很大的空間使用。
所以在function中,local variable勿無限制的使用(雖然它也有優點),尤其是多線程很容易忽略它,不然就使用malloc()的方式,使用完後也記得要清理free()。
更多的知識可查詢stack、heap
訂閱:
文章 (Atom)