您的位置:首页 > 编程语言

【读书笔记】c专家编程

2008-08-04 11:09 316 查看
// test_C.cpp :
定义控制台应用程序的入口点。
//

#include
"stdafx.h"
#include
<iostream>

using
namespace std;
void PrintChar(int
i);
const
int two = 2;
int order = 0;

void Gen_init(char
*string)//防止数组最后出现逗号(,)的方法
{

static
char sep = ' ' ;//static类型

cout << sep << " " << string << endl;

sep = ',';
}
int f()
{

cout << "f:" << ++order << endl;

return 1;
}
int g()
{

cout << "g:" << ++order << endl;

return 2;
}
int h()
{

cout << "h:" << ++order << endl;

return 3;
}

int g(int
a, int b)
{

if(a%b == 0)

return b;

else

g(b, a%b);
}
struct SA{

int a;

char b;

double c;
};//sizeof == 16
struct SB{

int a;

double c;

char b;
};// sizeof == 24

#pragma
pack(push)
//保存对齐状态
#pragma
pack(4)
//设定为字节对齐
struct SC
{

char m1;

double m4;

int m3;
};// sizeof == 16(n==4); sizeof == 24(n==8,16,24....)
#pragma
pack(pop)//恢复对齐状态

struct StructArray{//数组封装在结构体中的妙用

int a[10];
};
union BIT32{//联合的妙用

int i;

struct{char c0,c1,c2,c3;}byte;
};

typedef
struct StructArray Array;

bool StrEqu(const
char* const str1,
const char*
const str2)//将strcmp封装成这样更好
{

return !strcmp(str1,str2);
}

int p[10] = {1,2,3,4,5,6,7,8,9,0};

void AutoChangeType(float
i);

void AutoChangeType(float
i)
{

printf("%f/n",i);
}

int bss[100];
int data[100] = {1,2,3,4,5,6,7,8,9,0,11};

int _tmain(int
argc, _TCHAR* argv[])
{

AutoChangeType(100);

int *pc
= p;

cout << "指针本身的地址:"
<< &pc << endl

<< "指针所指的地址:"
<< pc << endl

<< "指针所指的数组第一个元素的地址:"
<< &pc[0] << endl

<< "指针所指的数组第一个元素的值:"
<< *pc << endl;

PrintChar(5);

if(StrEqu("abc","abc"))

cout << "Yes" << endl;

else

cout << "No" << endl;

BIT32 bb;

bb.i = 0x12345678;

cout << hex << bb.i << " "

<< (int)bb.byte.c0 <<
" "

<< (int)bb.byte.c1 <<
" "

<< (int)bb.byte.c2 <<
" "

<< (int)bb.byte.c3 << endl;

Array arr1, arr2;

for(int ii=0; ii<10; ++ii)

arr1.a[ii] = ii;

arr2 = arr1;

for(int ii=0; ii<10; ++ii)

cout << arr2.a[ii] << " ";

cout << endl;

cout << sizeof(SA) <<
" " <<
sizeof(SB) <<
" " << sizeof(SC) << endl;

int a,b;

//cin >> a >> b;

//cout << g(a,b) << endl;

int *pa =
new int(10);

int *pb =
new int(2);

*pa = *pa / *pb;// /后必须有空格

char line[10];

//gets(line);//小心溢出

//cout << line << endl;

int i=0;

//std::cin >> i;

switch(i)

{

case 1:

std::cout << 1 << std::endl;

break;

case two:

std::cout << 2 << std::endl;

break;

default:

;

}

printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"

"cccccccccccccccccccccccccccccccccccccccccccccccc/n");//3字符串自动合并成串

for(int i=0; i<3; ++i)

Gen_init("abc");

int x = f() + g() * h();//不同的编译器,fgh的调用顺序可能不同

cout << x << endl;

return 0;
}

//extern int* p;
extern
int p[];
void PrintChar(int
i);

void PrintChar(int
i)
{

cout << p << endl;

cout << &p[i] << endl;

cout << p[i] << endl;
}

#include
<stdio.h>

void AA(void)
{

printf("Oh my God, Dangerous!/n");
}

void ChangeReturn(int
i)
{

printf("%d/n",i);

__asm

{

lea edi, [esp+34h*4] // 获得函数返回地址在栈中的地址

mov ecx, [edi] //保存返回地址

mov [edi+4], ecx //将返回地址移到参数i在栈中的位置(覆盖参数)

lea esi, AA

mov [edi], esi//更改返回地址为函数AA

}
}

int main()
{

ChangeReturn(100);

printf("return to Main/n");

__asm

{

sub esp,4

}

return 0;
}

int main()
{

try{

int *pp =
new int[10];

pp[-1] = 10;

pp[20] = 20;

cout << pp[-1] << " " << pp[20] << endl;

strcpy(0,"aaa");

__asm

{

mov edi, 0;

mov [edi], 5;

}

union U{

char a[10];

int i;

}u;

cout << sizeof(U) << endl;

int *p = (int*)(&u.a[1]);

*p = 0xffffffff;

for(int i=0; i<100; ++i)

cout << (int)u.a[i] <<
" ";

cout << endl;

int MB = 0;

//while(malloc(1 << 20)) ++MB;

printf("Allocated %d MB total/n", MB);

}

catch(...)

{

cout << "Exception" << endl;

}

return 0;
}

__setjmp3:
1027F608
mov edx,dword ptr [esp+4]
1027F60C
mov dword ptr [edx],ebp
1027F60E
mov dword ptr [edx+4],ebx
1027F611
mov dword ptr [edx+8],edi
1027F614
mov dword ptr [edx+0Ch],esi
1027F617
mov dword ptr [edx+10h],esp
1027F61A
mov eax,dword ptr [esp]
1027F61D
mov dword ptr [edx+14h],eax
1027F620
mov dword ptr [edx+20h],56433230h
1027F627
mov dword ptr [edx+24h],0
1027F62E
mov eax,dword ptr fs:[00000000h]
1027F634
mov dword ptr [edx+18h],eax
1027F637
cmp eax,0FFFFFFFFh
1027F63A
jne _s3_get_count (1027F645h)
1027F63C
mov dword ptr [edx+1Ch],0FFFFFFFFh
1027F643
jmp _s3_done (1027F680h)

_longjmp:
10283554
push ebp
10283555
mov ebp,esp
10283557
sub esp,50h
1028355A
mov ebx,dword ptr [esp+58h]
1028355E
mov dword ptr [ebp-50h],80000026h
10283565
mov dword ptr [ebp-4Ch],0
1028356C
mov dword ptr [ebp-48h],0

10283573
mov dword ptr [ebp-44h],0
1028357A
mov dword ptr [ebp-40h],0
10283581
lea eax,[ebp-50h]
10283584
mov ebp,dword ptr [ebx]
10283586
mov esi,dword ptr [ebx+18h]
10283589
cmp esi,dword ptr fs:[0]
10283590
je _lj_local_unwind (102835A4h)
10283592
push ebx
10283593
push esi
10283594
push 0
10283596
push eax
10283597
push offset _lj_return (102835A2h)
1028359C
push esi
1028359D
call RtlUnwind (1023B78Ah)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: