Memory & Pointer trio@seu.edu.cn
2.1 2.1.1 1 int *p int a 0x00C7 0x00C7 0x00C7 2.1.2 2 int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++; 2.1.3 1. 2. 3. 3 int A, *pa, **ppa; pa = &A; ppa = &pa; &ppa ppa *ppa **ppa &pa pa *pa &A A
2.1.4 CPU 8 CPU 1~2 51 1~3 16 CPU 2 MSP430 32 CPU 4 Intel 80386 2.1.5 p int* p; 0x3FF0073D 0x3FF0073D!!! 0x3FF0073D Windows Windows 2.2 1. C 2. 4 : int a[4], *p; p = a; // p =&a[0]; *(a+2) = 0; // a[2] = 0; p[2] = 0; // a[2] = 0;
a 5 int a[4], *p; p = a; a = p; p++; a++; // // // // 2.3 (1). 0 0 0 NULL p = NULL; // NUL NUL \0 p = 0;
(2). (void *) void int n, *p; double *q; void *gp1, *gp2; gp1 = (void *) &n; p = (int *)gp1; 2.4 2.4.1 < > (* < >)(...) int (*func)(void ) func int *func(void) int (*func)(void) int *func(void); // int (*func)(void) //
2.4.2 6 int int fn(int); int (*fp)(int); fp = fn; // fn fp fp = fn(5); // fp fp = & // 1 2 3 2.4.2 1. (polymorphism) 2. (multithreading) API Win32 CreateThread(...pF...) 3. (call-back) (event-driven) Win32 WinProc
2.4.3 7 switch(oper){ case ADD: result=add(op1,op2); break; case SUB: result=sub(op1,op2); break;... } switch double add(double,double); double sub(double,double);... double (*oper_func[])(double,double)={add,sub,...}; 2 switch result=oper_func[oper](op1,op2); oper ASIX Window typedef struct window_class { U8 wndclass_id; STATUS (*create)(char *caption, U32 style, U16 x, U16 y, U16 width, U16 hight, U32 wndid, U32 menu, void **ctrl_str, void *exdata); STATUS (*destroy)(void *ctrl_str); STATUS (*msg_proc)( U32 win_id, U16 asix_msg, U32 lparam, void *data, U16 wparam, void *reserved); STATUS (*msg_trans)(void *ctrl_str, U16 msg_type, U32 areaid, P_U16 data, U32 size, PMSG trans_msg); STATUS (*repaint)(void *ctrl_str, U32 lparam); STATUS (*move)(void *ctrl_str, U16 x, U16 y, U16 width, U16 hight, void *reserved); STATUS (*enable)(void *ctrl_str, U8 enable); STATUS (*caption)(void *ctrl_str, char *caption, void *exdata); STATUS (*information)(void *ctrl_str, struct asix_window *wndinfo); } WNDCLASS;
ASIX Window WNDCLASS WindowClass[] = { {WNDCLASS_WIN, wn_create, wn_destroy, wn_msgproc,wn_msgtrans, wn_repaint, NULL,NULL,wn_caption, NULL}, {WNDCLASS_BUTTON,Btn_create,Btn_destroy,Btn_msg_proc,Btn_msg_trans,Btn_repaint,NULL,Btn_enable,Btn_caption, NULL}, {WNDCLASS_SELECT,sl_create, sl_destroy, sl_msg_proc, sl_msg_trans, sl_repaint,null, sl_enable, sl_caption, NULL}, {WNDCLASS_SELECTCARD,NULL, NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}, {WNDCLASS_MENU,menu_create, menu_destroy, menu_msgproc, menu_msgtrans, mn_repaint, NULL, NULL,NULL,NULL}, {WNDCLASS_LIST, Lbox_create, Lbox_destroy, Lbox_msgproc, Lbox_msgtrans, lb_repaint, NULL, NULL,NULL,NULL}, {WNDCLASS_KEYBD, kbd_create,kbd_destroy, kbd_msgproc, kbd_msgtrans, kbd_repaint, NULL, NULL,NULL,NULL}, {WNDCLASS_SCROLL,sb_create, sb_destroy, sb_msgproc, sb_msgtrans, sb_repaint, NULL,sb_enable, NULL,NULL}, {WNDCLASS_KEYBAR,kb_create, kb_destroy, kb_msgproc, kb_msgtrans,null,null, NULL,NULL, NULL}, #ifdef ASIX_DEBUG {WNDCLASS_TEST,tst_create, tst_destroy, tst_msgproc, tst_msgtrans, NULL,NULL,NULL,NULL,NULL} #endif };
char *DoSomething( ) { char i[32*1024]; memset(i,0,32*1024); return i; { 1 2 void DoSomething( ) { int i; int j; int k; memset(&k,0,3*sizeof(int) ); 3 1 { I,j,k 2 I, j, k ( ( ) 3 K K Data Aboart
, Pixle 2 bit Char *buffer; buffer = malloc (x*y/4); Buffer = malloc(x*y/4 + 1);
char *DoSomething( ) { char *p, *q; if ( (p = malloc(1024)) == NULL ) return NULL; if ( (q = malloc(2048)) == NULL ) return NULL; return p; { q p NULL void FreeWindowsTree(windows *Root) { if(root!= NULL) { window *pwnd; /* pwndroot.. */ for(pwnd = Root->Child;pwnd!= NULL;pwnd = pwnd->sibling) FreeWindowTree(pwnd); if(root->strwndtitle!= NULL) FreeMemory(Root->strWndTitle); FreeMemory(Root); } } Pwnd for
struct sizeof Free malloc() ANSI C Free
Mem Free Size 1 8 Free Free Size 7 BASE BASE Allocp Allocp First First 16 Size 1 Size 4 Size 3 BASE BASE Allocp Allocp First First Ptr Free
Allocp First Size 1 Size 4 Size 1 Size 4 Size 1 Size 9 Size 2 Size=3 Size 5
union header { struct { union header *; unsigned long size; } s; char c[8];// For debugging; also ensure size is 8 bytes }; typedef union header HEADER;
struct sheader { struct sheader unsigned long }; *; size; #define ABLKSIZE (sizeof (HEADER)) /* 1*/ #define BTOU(nb) ((((nb) + ABLKSIZE - 1) / ABLKSIZE) + 1)
VC Just Try It