C:打印在共享内存中声明的数组时获取垃圾字符
C: Getting garbage characters when printing an array declared in shared memory
我正在尝试将一个字符串数组声明到共享内存中。
server.c 创建共享内存,client.c 填充数组 "tab" 并打印 it.And 它在客户端工作得很好。
但是当 运行 server.c !
时我得到垃圾字符
如有任何帮助,我们将不胜感激!
server.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
typedef struct People
{
char *tab[5];
} Person;
int i;
int main()
{
system("clear");
Person aaron;
Person *p_aaron;
int id;
int key = 12345;
p_aaron = &aaron;
if ((id = shmget(key,1000, IPC_CREAT | 0666)) < 0)
{
perror("SHMGET");
exit(1);
}
printf("server: size: %d \n", sizeof(aaron));
if((p_aaron = shmat(id, NULL, 0)) == (Person *) -1)
{
perror("SHMAT");
exit(1);
}
for(i=0;i<5;i++) {
printf("server tab: %s \n", p_aaron->tab[i]);
}
return 0;
}
client.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
typedef struct People
{
char *tab[5];
} Person;
int test;
int i;
int main()
{
system("clear");
Person aaron;
Person *p_aaron;
int id;
int key = 12345;
p_aaron = &aaron;
id = shmget(key,1000, IPC_CREAT | 0666);
p_aaron = shmat(id, NULL, 0);
p_aaron->tab[0]="abnb";
p_aaron->tab[1]="b";
p_aaron->tab[2]="c";
p_aaron->tab[3]="d";
p_aaron->tab[4]="e";
for(i=0;i<5;i++) {
printf("client tab: %s \n", p_aaron->tab[i]);
}
return 0;
}
server.c输出
server tab: �
server tab: �
server tab: �
server tab: �
server tab: �
client.c输出
client tab: abnb
client tab: b
client tab: c
client tab: d
client tab: e
您用来初始化指针的文字字符串不在共享内存中,对服务器不可见。最简单的解决方案是声明一个字符串数组 char tab[5][MAXLEN]
并使其共享(MAXLEN 是最长元素的长度 + 1)。
扩展@DYZ 的回答
正如 DYZ 所建议的那样,您的字符串文字对服务器不可见。尝试使用如下数组:
common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#define MAX_CHAR 10
typedef struct People
{
char tab[5][MAX_CHAR];
} Person;
#endif /* _COMMON_H_ */
client.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
int test;
int i;
int main()
{
Person aaron;
Person *p_aaron;
int id;
int key = 12345;
p_aaron = &aaron;
id = shmget(key,1000, IPC_CREAT | 0666);
p_aaron = shmat(id, NULL, 0);
snprintf(p_aaron->tab[0],MAX_CHAR,"%s","abnb");
snprintf(p_aaron->tab[1],MAX_CHAR,"%s","bcde");
snprintf(p_aaron->tab[2],MAX_CHAR,"%s","c");
snprintf(p_aaron->tab[3],MAX_CHAR,"%s","d");
snprintf(p_aaron->tab[4],MAX_CHAR,"%s","e");
for(i=0;i<5;i++) {
printf("client tab: %s \n", p_aaron->tab[i]);
}
return 0;
}
server.c
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
int i;
int main()
{
system("clear");
Person aaron;
Person *p_aaron;
int id;
int key = 12345;
p_aaron = &aaron;
if ((id = shmget(key,1000, IPC_CREAT | 0666)) < 0)
{
perror("SHMGET");
exit(1);
}
printf("server: size: %lu \n", sizeof(aaron));
if((p_aaron = shmat(id, NULL, 0)) == (Person *) -1)
{
perror("SHMAT");
exit(1);
}
for(i=0;i<5;i++) {
printf("server tab: %s \n", p_aaron->tab[i]);
}
return 0;
}
我正在尝试将一个字符串数组声明到共享内存中。 server.c 创建共享内存,client.c 填充数组 "tab" 并打印 it.And 它在客户端工作得很好。 但是当 运行 server.c !
时我得到垃圾字符如有任何帮助,我们将不胜感激!
server.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
typedef struct People
{
char *tab[5];
} Person;
int i;
int main()
{
system("clear");
Person aaron;
Person *p_aaron;
int id;
int key = 12345;
p_aaron = &aaron;
if ((id = shmget(key,1000, IPC_CREAT | 0666)) < 0)
{
perror("SHMGET");
exit(1);
}
printf("server: size: %d \n", sizeof(aaron));
if((p_aaron = shmat(id, NULL, 0)) == (Person *) -1)
{
perror("SHMAT");
exit(1);
}
for(i=0;i<5;i++) {
printf("server tab: %s \n", p_aaron->tab[i]);
}
return 0;
}
client.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
typedef struct People
{
char *tab[5];
} Person;
int test;
int i;
int main()
{
system("clear");
Person aaron;
Person *p_aaron;
int id;
int key = 12345;
p_aaron = &aaron;
id = shmget(key,1000, IPC_CREAT | 0666);
p_aaron = shmat(id, NULL, 0);
p_aaron->tab[0]="abnb";
p_aaron->tab[1]="b";
p_aaron->tab[2]="c";
p_aaron->tab[3]="d";
p_aaron->tab[4]="e";
for(i=0;i<5;i++) {
printf("client tab: %s \n", p_aaron->tab[i]);
}
return 0;
}
server.c输出
server tab: �
server tab: �
server tab: �
server tab: �
server tab: �
client.c输出
client tab: abnb
client tab: b
client tab: c
client tab: d
client tab: e
您用来初始化指针的文字字符串不在共享内存中,对服务器不可见。最简单的解决方案是声明一个字符串数组 char tab[5][MAXLEN]
并使其共享(MAXLEN 是最长元素的长度 + 1)。
扩展@DYZ 的回答 正如 DYZ 所建议的那样,您的字符串文字对服务器不可见。尝试使用如下数组:
common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#define MAX_CHAR 10
typedef struct People
{
char tab[5][MAX_CHAR];
} Person;
#endif /* _COMMON_H_ */
client.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
int test;
int i;
int main()
{
Person aaron;
Person *p_aaron;
int id;
int key = 12345;
p_aaron = &aaron;
id = shmget(key,1000, IPC_CREAT | 0666);
p_aaron = shmat(id, NULL, 0);
snprintf(p_aaron->tab[0],MAX_CHAR,"%s","abnb");
snprintf(p_aaron->tab[1],MAX_CHAR,"%s","bcde");
snprintf(p_aaron->tab[2],MAX_CHAR,"%s","c");
snprintf(p_aaron->tab[3],MAX_CHAR,"%s","d");
snprintf(p_aaron->tab[4],MAX_CHAR,"%s","e");
for(i=0;i<5;i++) {
printf("client tab: %s \n", p_aaron->tab[i]);
}
return 0;
}
server.c
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
int i;
int main()
{
system("clear");
Person aaron;
Person *p_aaron;
int id;
int key = 12345;
p_aaron = &aaron;
if ((id = shmget(key,1000, IPC_CREAT | 0666)) < 0)
{
perror("SHMGET");
exit(1);
}
printf("server: size: %lu \n", sizeof(aaron));
if((p_aaron = shmat(id, NULL, 0)) == (Person *) -1)
{
perror("SHMAT");
exit(1);
}
for(i=0;i<5;i++) {
printf("server tab: %s \n", p_aaron->tab[i]);
}
return 0;
}