- 论坛徽章:
- 1
|
回复 10# stephen_du
试过了,不行!
用了barrier就可以解决问题了,我的linux是2.6.18-308.16.1.el5,在usr/linux下面没有找到对应的头文件,只好
#define barrier() __asm__ __volatile__("": : :"memory"- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //#include <stddef.h>
- //#pragma pack(8)
- #define SETBIT(a,i) ((a)[(i) / 8] |= (1 << (7 - (i) % 8)))
- #define barrier() __asm__ __volatile__("": : :"memory")
- typedef struct _testStru{
- volatile char chArr[8];
- char *pCh;
- }testStru;
- int main()
- {
- testStru* test=(testStru *)calloc(1, sizeof(testStru));
- printf("test.chArr1 is %x.\n", test->pCh);
- SETBIT(test->chArr, 127);
- barrier();
- printf("test.chArr2 is %p.\n", test->pCh);
- printf("test.chArr3 is %p.\n", test->pCh);
- return 0;
- }
复制代码 [***@**-build2-svr test]$ !gcc
gcc -O3 test.c
[***@**-build2-svr test]$ ./a.out
test.chArr1 is 0.
test.chArr2 is 0x100000000000000.
test.chArr3 is 0x100000000000000. |
|