/ #if 0 /*堆和栈的差别*/ 1>管理方式 --->堆有程序猿控制,栈由编译器控制 2>产生碎片---->堆中因为频繁进行new和malloc操作,会产生大量的内存碎片,栈不会 3>生长的方向-->堆由低地址到高地址生长,栈由高地址向低地址生长 4>申请的大小-->在32位系统中,堆内存能够达到4G。栈内存在VC6中默觉得1M 5>申请的效率-->堆由程序猿分配,效率低。栈由系统自己主动分配,效率高 #endif / / /*new 和 malloc的差别*/ #if 0 1>new为c++的运算符,keyword。malloc为C语言的库函数 2>new会调用构造函数对申请的空间进行初始化。而malloc不能够
3>new的返回值为带类型。malloc的返回值为void *
4>new开辟类型的大小能够自己主动计算,而malloc必须自己计算
5>new能够重载。malloc不能够
6>当分配空间失败时,new会抛出异常,bad_malloc。而malloc返回空
7>new须要用delete释放,而malloc须要用free释放
#endif / / #if 0 /*自己实现itoa函数*/ char *myitoa(int value, char *string, int radix) { char s[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; char c; int i=0,j=0; int tmp = 0; do { tmp = value % radix; value = value / radix; string[i++] = s[tmp]; }while(value != 0); string[i] = '\0'; i--; while(j < i) { c = string[j]; string[j] = string[i]; string[i] = c; i--; j++; } return string; } int main() { int i = 3456; char string[20]; cout<<myitoa(i,string,16)<<endl; } #endif / / #if 0 /*自己实现atoi函数*/ int myatoi(const char *string) { long long num = 0; assert(string != NULL); bool flag = false; while( *string != '\0' ) { if ( *string == '+' ) string++; if ( *string == '-' ) { flag = true; string++; } if (*string>= '0' && *string <='9') { num = num*10+(*string - '0'); string++; } } return flag? (0-num):num; } int main() { char str[] = "123456"; int a = myatoi(str); cout<<a<<endl; return 0; } #endif / / /*用C语言实现多态*/ #if 0 struct A { struct A *A_this; void (*PFUN)(struct A *A_this); int a; int b; }; struct B { struct B *B_this; void (*PFUN)(struct B *B_this); int a; int b; int c; }; void A_FUN(struct A* A_this) { printf("it is A.a:"); printf("%d\n", A_this->a); } void B_FUN(struct B* B_this) { printf("it is B.a:"); printf("%d\n", B_this->a); } void A_Creat(struct A *p) { p->A_this = p; p->PFUN = A_FUN; p->a = 1; p->b = 2; } void B_Creat(struct B*p) { p->B_this = p; p->PFUN = B_FUN; p->a = 10; p->b = 20; p->c = 30; } int main() { struct A *ma, a; struct B *mb, b; A_Creat(&a); B_Creat(&b); ma = &a; mb = &b; ma = (struct A *)mb; ma->PFUN(ma); } #endif / / /*自己实现strcat函数*/ #if 0 char *mystrcat(char *dst, const char *src) { assert(dst != NULL && src != NULL); char *tmp = dst; while( *dst != '\0') ++dst; while( *dst++ = *src++) ; return tmp; } int main() { char dst[20] = "hello"; char src[20] = "world"; char *cat = mystrcat(dst,src); printf("%s\n", cat); } #endif / / /*自己实现strcmp函数*/ #if 0 int mystrcmp(const char *str1, const char *str2) { int i = 0; while(str1[i] != '\0' && str2[i] != '\0' && str1[i] == str2[i] ) i++; return (str1[i] == str2[i] ? 0 : (str1[i] > str2[i] ? 1 : -1)); } int main() { char str1[] = "wangyu"; char str2[] = "wangyu"; int j = mystrcmp(str1,str2); cout<<j<<endl; } #endif / / /*自己实现sizeof*/ #if 0 template<typename T> int mysizeof(T &v) { return (char *)(&v+1)-(char *)(&v); } int main() { char s[20] = "i am a boy!"; int b = 10; int a = mysizeof(s); cout<<a<<" "<<b<<endl; } #endif / / /*自己实现strlen()函数*/ #if 0 int mystrlen1(const char *str) { int length = 0; while( (*str++) != '\0' ) length++; return length; } int mystrlen2(const char *str) { if ( '\0' == *str ) return 0; return mystrlen2(str+1)+1; } int mystrlen3(const char *str) { return *str ? (mystrlen3(++str) + 1) : 0; } int main() { char s[20] = "i am a boy"; int length1 = mystrlen1(s); int length2 = mystrlen2(s); int length3 = mystrlen3(s); printf("%d %d %d\n", length1, length2, length3); } #endif / / /*自己实现strcpy()函数,可是没有考虑内存重叠*/ #if 0 char *mystrcpy(char *dst, const char *src) { assert(dst != NULL && src != NULL); char *pdst = dst; while( (*pdst++ = *src++) != '\0' ) ; return dst; } int main() { char a[20]; char s[] = "i am a teacher!"; mystrcpy(a,s); printf("%s\n", a); //返回值为char *类型,是为了实现链式表达式,例如以下: //int length = strlen(mystrcpy(a,s)); //printf("%d\n", length); } #endif / / /*自己实现strncpy()函数*/ #if 0 char *mystrncpy(char *dst, const char *src, int count) { assert(dst != NULL && src != NULL); char *pdst = dst; int i = 0; while( i++<count && (*pdst++ = *src++) != '\0' ) ; if ( *pdst != '\0' ) *pdst = '\0'; return dst; } int main() { char a[20]; char s[] = "iamateacher!"; mystrncpy(a,s, 5); printf("%s\n", a); } #endif / / /*自己实现memcpy()函数*/ /*库函数memcpy()没有考虑内存重叠,而memmove则考虑了内存重叠*/ #if 0 void *mymemcpy(void *dst, const void *src, int count) { assert(dst != NULL && src != NULL); char *pdst = (char *)dst; char *psrc = (char *)src; while(count--) *pdst++ = *psrc++; *pdst = '\0'; return dst; } int main() { char buffer[20] = "abcdefghi"; mymemcpy(buffer+100, buffer, 5); printf("%s\n", buffer+100); return 0; } #endif / / /*自己实现memmove函数*/ #if 0 void *mymemmove(void *dst, const void *src, int count) { char *pdst; char *psrc; assert(dst != NULL && src != NULL); if ( src < dst && ((char *)src+count) > (char *)dst ) { psrc = (char *)src + count - 1; pdst = (char *)dst + count - 1; while(count--) *pdst-- = *psrc--; } else { psrc = (char *)src; pdst = (char *)dst; while(count--) *pdst++ = *psrc++; *pdst = '\0'; } return dst; } int main() { char buffer[100] = "abcdefghi"; mymemcpy(buffer+3, buffer, 5); printf("%s\n", buffer+3); return 0; } #endif / / /*不同位的编译器不同的数据类型所相应的字节数*/ #if 0 32位编译器 64位编译器 类型 字节数 字节数 char 1 1 char * 4------------------8 short 2 2 int 4 4 unsigned int 4 4 float 4 4 double 8 8 long 4------------------8 long long 8 8 unsigned long 4------------------8 #endif / / /*结构体的字节对齐*/ #if 0 int main() { struct s { int a; short b; char c; float d; char e; short f; double g; short h; }; cout<<sizeof(s)<<endl; return 0; } #endif / / /*<1>指针数组和数组指针*/ #if 0 int main() { int c[4] = {1, 2, 3, 4}; int *a[4]; int (*b)[4]; b = &c; for(int i=0; i<4; i++) { a[i] = &c[i]; } cout<<"c[2] = "<<*a[2]<<endl; cout<<"c[3] = "<<(*b)[3]<<endl; } #endif /