- 论坛徽章:
- 0
|
- #define SIZE 10000
- #define MODE 0600 /*r/w*/
- #define NUM 10
- #include<stdio.h>
- #include<stdlib.h>
- #include<sys/ipc.h>
- #include<sys/shm.h>
- #include<sys/types.h>
- #include<string.h>
- //#include
- struct student{
- char name[20];
- int age;
- };
- int main(){
- int shmid;
- struct student *ptr;
- struct student *student_array=ptr;
- int i=0;
- if((shmid=shmget(IPC_PRIVATE,SIZE,MODE))<0){
- printf("1.error\n");
- exit(-1);
- }
- if ((ptr=(struct student*)shmat(shmid,0,0))==(void*)-1){
- printf("2.error\n");
- exit(-1);
- }
- memset(ptr,0,SIZE);
- printf("sizeof(struct student):%d:%x\n",sizeof(struct student));
- for(i=0;i<NUM;i++){
- printf("%lx\n",student_array+i);
- memcpy(student_array[i].name,"LuYuan",6);///用于测试所以就写6,
- student_array[i].age=i;
- }
- for(i=0;i<NUM;i++){
- printf("name=%s\n",student_array[i].name);//这行的输出会有乱码
- printf("age=%d\n",student_array[i].age);
- }
- }
复制代码
这个程序输出会有乱码。
我想问的是既然我都已经给共享内存初始化为0了,怎么可能会有乱码呢,
试验环境是
linux 2。6。15
gcc version 4。0。3 |
|