code, software architect, articles and novels. 代码,软件架构,博客和小说
Trickies and Bugs in C
Posted onEdited onWord count in article: 1.8kReading time ≈2 mins.
the macro #define is the biggest hidden bug in c. You should always remember macro-function is like code paster. And it will slow down your system in some special situations, such as macro recursion. If you macro define some function use BIG characters.
1 2 3 4 5 6 7 8 9 10 11 12
#define FUN1(a,b) a*b #define FUN2(a) a++;\ a*=a;
int main(){ int a = 2; int b = 3; FUN1(a,5+1); // => 2*5+1 if(a>2)FUN2(a); //=> if(a>2) a++;a*=a;
return 0; }
strcpy() will copy “\0”, and it is dangerous when you copy two points.
// There are 5 segments in codes, DATA, BSS, TEXT, heap, stack // DATA stores all globel variables, and those variables are defined. // BSS stores all globel variables, but those variables are not defined. // const variables are in TEXT, and codes are in this segment also. // heap store (malloc, calloc,realloc, new) data // stack store function params, local variables.
int a; // a is in BSS int b = 3; // b is in DATA int fun(int c){ // c is in stack, when the function is called. return 0; } int main(){ const int c = 4; // c is in TEXT a = 4; // move to DATA int x = 4; // in stack int * p = malloc(sizeof(int)); // *p is in heap. but p is in stack. return 0; }