linux无法获取共享内存,Linux C共享内存无法在struct中获取字符串数组

我在struct push到共享内存中有动态数组 . 客户端链接此共享内存,我无法检索共享内存中的数组内容 .

Server.c

#include

#include

#include

#include

#include

#include

#include

#define MAX_CHAR 65

typedef struct motcptable{

char **Address;

char **Value;

int Count;

int ThreadExit;

int ThreadId;

char Motcp_Ip[20];

int Motcp_Port;

int Motcp_Id;

int Motcp_Timeout_Sec;

int Motcp_Sleep;

int Motcp_Retry;

int Motcp_Start_Address;

int Motcp_Coil_Read_Max;

int Motcp_Coil_Write_Max;

int Motcp_Holding_Read_Max;

int Motcp_Holding_Write_Max;

} MotcpTable;

struct motcptable Table[64];

void iniTable(void) {

for(int i = 0; i < 64; i++) {

Table[i].Address = NULL;

Table[i].Value = NULL;

Table[i].Count = 0;

Table[i].ThreadExit = 0;

Table[i].ThreadId = i;

Table[i].Motcp_Timeout_Sec = 3;

Table[i].Motcp_Sleep = 500;

Table[i].Motcp_Retry = 10;

}

}

void Add(int Index, char *Address_buf) {

Table[Index].Address = (char **) realloc(Table[Index].Address, ((Table[Index].Count + 1) * sizeof(char *)));

Table[Index].Value = (char **) realloc(Table[Index].Value, ((Table[Index].Count + 1) * sizeof(char *)));

Table[Index].Address[Table[Index].Count] = NULL;

Table[Index].Value[Table[Index].Count] = NULL;

for(int i = 0; i <= Table[Index].Count; i++) {

Table[Index].Address[i] = (char *) realloc(Table[Index].Address[i], MAX_CHAR * sizeof(char));

Table[Index].Value[i] = (char *) realloc(Table[Index].Value[i], MAX_CHAR * sizeof(char));

}

sprintf(Table[Index].Address[Table[Index].Count], "%s", Address_buf);

sprintf(Table[Index].Value[Table[Index].Count], "%s", Address_buf);

Table[Index].Count++;

}

void Dispose(void) {

for(int i = 0; i < 63; i++) {

for(int j = 0; j < Table[i].Count; j++) {

free(Table[i].Address[j]);

free(Table[i].Value[j]);

}

if(Table[i].Count > 0) {

free(Table[i].Address);

free(Table[i].Value);

}

}

}

int main(int argc, char* argv[])

{

int shmid;

key_t key;

struct motcptable *shm;

char s;

int i, size;

char ch2[65];

char* ch1;

key = 1000;

iniTable();

sprintf(Table[0].Motcp_Ip,"192.168.200.199");

Table[0].Motcp_Port = 502;

Table[0].Motcp_Id = 1;

Table[0].Motcp_Start_Address = 1;

Table[0].Motcp_Timeout_Sec = 3;

for(i = 0; i < 2; i++) {

sprintf(ch2, "%d", i + 10000);

Add(0, ch2);

}

size = size + (sizeof(char **) * Table[0].Count);

shmid = shmget(key, size, IPC_CREAT | 0666);

if (shmid < 0) {

perror("shmget");

exit(1);

}

shm = (struct motcptable*) shmat(shmid, NULL, 0);

shm[0].Address = (char **) realloc(shm[0].Address, ((Table[0].Count + 1) * sizeof(char *)));

shm[0].Value = (char **) realloc(shm[0].Value, ((Table[0].Count + 1) * sizeof(char *)));

shm[0].Address[0] = Table[0].Address[0];

shm[0].Address[1] = Table[0].Address[1];

shm[0].Count = Table[0].Count;

printf("\t Asssign Address : %s\n", shm[0].Address[0]);

if (shm == (struct motcptable *)-1) {

perror("shmat");

exit(1);

}

while (shm[63].ThreadExit != 1) {

sleep(1);

}

Dispose();

shmctl(shmid, IPC_RMID, NULL); //destory shared memory

return 0;

}

这是client.c

#include

#include

#include

#include

#include

#include

typedef struct motcptable{

char **Address;

char **Value;

int Count;

int ThreadExit;

int ThreadId;

char Motcp_Ip[20];

int Motcp_Port;

int Motcp_Id;

int Motcp_Timeout_Sec;

int Motcp_Sleep;

int Motcp_Retry;

int Motcp_Start_Address;

int Motcp_Coil_Read_Max;

int Motcp_Coil_Write_Max;

int Motcp_Holding_Read_Max;

int Motcp_Holding_Write_Max;

} MotcpTable;

struct motcptable Table[64];

int main(int argc, char* argv[])

{

int shmid;

key_t key;

struct motcptable* shm;

int size;

key = 1000;

size = sizeof(Table) + (sizeof(char **) * 2);

shmid = shmget(key, size, 0666);

if (shmid < 0) {

perror("shmget");

exit(1);

}

shm = (struct motcptable*) shmat(shmid, NULL, 0);

if (shm == (struct motcptable *)-1) {

perror("shmat");

exit(1);

}

printf("\n\t %s \n", shm[0].Address[0]); // Segmentation fault (core dumped)

printf("\n\t %d\n",shm[0].Count);

shm[63].ThreadExit = 1;

return 0;

}

我可以在数组中读取和写入struct的其他成员,除了字符串数组'Address'和'Value' . 我认为客户端通过shmat不包括服务器端内存分配结构 . 那么,我如何获得分配内存的shmat .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值