標籤:style blog color 使用 os io 資料 for
1.變數:一段儲存地區,每個變數都有一個地址,指向變數的儲存位置.
變數名:變數名對應變數的地址.(編譯器會將程式中的變數地址和變數名,建立一個"符號表",編寫代碼時對變數的操作,實際編譯器會拿 符號表中變數名對應的
地址中儲存的資料進行操作.)
2.指標是個變數,變數裡面儲存的值用來當作地址.
char *p;
p是變數,儲存的值是一個地址.(p本身也有地址,&p)
3.二維指標:
char **p;
p儲存的是一個指標的地址.(p本身也有地址,&p)
4.指標和數組
char a[10];
char *p;
區別: a == &a;
p != &p;
5.u/cos ii 的指標的運用.
OS_MEM *OSMemCreate (void *addr, INT32U nblks, INT32U blksize, INT8U *perr){ OS_MEM *pmem; INT8U *pblk; void **plink; INT32U loops; INT32U i; // ... OS_ENTER_CRITICAL(); pmem = OSMemFreeList; /* Get next free memory partition */ if (OSMemFreeList != (OS_MEM *)0) { /* See if pool of free partitions was empty */ OSMemFreeList = (OS_MEM *)OSMemFreeList->OSMemFreeList; } OS_EXIT_CRITICAL(); if (pmem == (OS_MEM *)0) { /* See if we have a memory partition */ *perr = OS_ERR_MEM_INVALID_PART; return ((OS_MEM *)0); } plink = (void **)addr; /* Create linked list of free memory blocks */ pblk = (INT8U *)addr; loops = nblks - 1u; for (i = 0u; i < loops; i++) { pblk += blksize; /* Point to the FOLLOWING block */ *plink = (void *)pblk; /* Save pointer to NEXT block in CURRENT block */ plink = (void **)pblk; /* Position to NEXT block */ } *plink = (void *)0; /* Last memory block points to NULL */ pmem->OSMemAddr = addr; /* Store start address of memory partition */ pmem->OSMemFreeList = addr; /* Initialize pointer to pool of free blocks */ pmem->OSMemNFree = nblks; /* Store number of free blocks in MCB */ pmem->OSMemNBlks = nblks; pmem->OSMemBlkSize = blksize; /* Store block size of each memory blocks */ *perr = OS_ERR_NONE; return (pmem);}
摘取一段:
plink = (void **)addr; /* Create linked list of free memory blocks */ pblk = (INT8U *)addr; loops = nblks - 1u; for (i = 0u; i < loops; i++) { pblk += blksize; /* Point to the FOLLOWING block */ *plink = (void *)pblk; /* Save pointer to NEXT block in CURRENT block */ plink = (void **)pblk; /* Position to NEXT block */ } *plink = (void *)0; /* Last memory block points to NULL */
其中
plink = (void **)addr; pblk = (INT8U *)addr;
乍一看,很高深的樣子,addr本身是一維的,怎麼既作為一維指標,又作為二維指標使用.
但看看後面plink的使用,plink只是取一維的深度來用,也就是說等價以下代碼:
void *plink;plink = (void *)addr; /* Create linked list of free memory blocks */pblk = (INT8U *)addr;loops = nblks - 1u;for (i = 0u; i < loops; i++) { pblk += blksize; /* Point to the FOLLOWING block */ *plink = (void *)pblk; /* Save pointer to NEXT block in CURRENT block */ plink = (void *)pblk; /* Position to NEXT block */}*plink = (void *)0; /* Last memory block points to NULL */