簡體   English   中英

C:打印共享內存中聲明的數組時獲取垃圾字符

[英]C: Getting garbage characters when printing an array declared in shared memory

我試圖將字符串數組聲明為共享內存。 server.c創建共享內存,client.c填充數組“ tab”並打印它。它在客戶端工作正常。 但是在運行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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM