I'm trying to declare an array of string into shared memory. server.c creates the shared memory and client.c fills the array "tab" and print it.And it's working just fine in the client side. but I get garbage characters when running the server.c !
我试图在共享内存中声明一个字符串数组。服务器。c创建共享内存和客户端。c填充数组“tab”并打印它。在客户端运行良好。但是我在运行服务器时得到了垃圾字符。c !
Any help would be appreciated!
如有任何帮助,我们将不胜感激!
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
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 output
服务器。c输出
server tab: �
server tab: �
server tab: �
server tab: �
server tab: �
client.c output
客户端。c输出
client tab: abnb
client tab: b
client tab: c
client tab: d
client tab: e
2 个解决方案
#1
1
Extending on @DYZ answer As DYZ suggested your string literals are not visible to the server. Try using an array as below:
根据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
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
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;
}
#2
3
The literal strings that you use to initialize the pointers are not in the shared memory and not visible to the server. The simplest solution is to declare an array of strings char tab[5][MAXLEN]
and make it shared (MAXLEN is the length of the longest element + 1).
用于初始化指针的文字字符串不在共享内存中,也不可见于服务器。最简单的解决方案是声明string char选项卡[5][MAXLEN]的数组并使其共享(MAXLEN是最长元素+ 1的长度)。
#1
1
Extending on @DYZ answer As DYZ suggested your string literals are not visible to the server. Try using an array as below:
根据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
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
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;
}
#2
3
The literal strings that you use to initialize the pointers are not in the shared memory and not visible to the server. The simplest solution is to declare an array of strings char tab[5][MAXLEN]
and make it shared (MAXLEN is the length of the longest element + 1).
用于初始化指针的文字字符串不在共享内存中,也不可见于服务器。最简单的解决方案是声明string char选项卡[5][MAXLEN]的数组并使其共享(MAXLEN是最长元素+ 1的长度)。