Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 访问共享内存段时出现分段错误_C_Segmentation Fault_Shared Memory - Fatal编程技术网

C 访问共享内存段时出现分段错误

C 访问共享内存段时出现分段错误,c,segmentation-fault,shared-memory,C,Segmentation Fault,Shared Memory,我需要实现一个在不同进程之间共享信息的程序 但当我试图访问共享结构的一个成员时,它会产生一个分段错误 我怎样才能修好它?请看下面我的代码 源文件: #include <string.h> #include <stdio.h> #include "ShM.h" #define SHM_SIZE 1024 int main(){ stablishMemory(); Deck *deck = obtainMemory(); strncpy(dec

我需要实现一个在不同进程之间共享信息的程序

但当我试图访问共享结构的一个成员时,它会产生一个分段错误

我怎样才能修好它?请看下面我的代码

源文件:

#include <string.h>
#include <stdio.h>
#include "ShM.h"

#define SHM_SIZE 1024 

int main(){

    stablishMemory();
    Deck *deck = obtainMemory();
    strncpy(deck->cards,"carlos",SHM_SIZE);
    unlinkMemory();
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <unistd.h>

int idMemory;

typedef struct {
    char *cards;
}Deck;

Deck* letra;
#define SHM_SIZE 1024 

void stablishMemory(){
    idMemory = shmget (obtainkey(), SHM_SIZE, 0777| IPC_CREAT);
    letra = (Deck* )shmat (idMemory, NULL,0);
}

key_t obtainkey(){
    return ftok("/bin/ls",24);
}

void unlinkMemory(){
    shmdt((Deck*)letra);

}

Deck* obtainMemory(){
    return letra;
}

void destroyMemory(){
    shmctl(idMemory, IPC_RMID, (struct shmid_ds*)NULL);
    unlink(" ");
}
#包括
#包括
#包括“ShM.h”
#定义SHM_大小1024
int main(){
建立记忆();
Deck*Deck=获取内存();
strncpy(牌组->卡片,“卡洛斯”,SHM_尺寸);
取消链接内存();
返回0;
}
头文件(ShM.h):

#include <string.h>
#include <stdio.h>
#include "ShM.h"

#define SHM_SIZE 1024 

int main(){

    stablishMemory();
    Deck *deck = obtainMemory();
    strncpy(deck->cards,"carlos",SHM_SIZE);
    unlinkMemory();
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <unistd.h>

int idMemory;

typedef struct {
    char *cards;
}Deck;

Deck* letra;
#define SHM_SIZE 1024 

void stablishMemory(){
    idMemory = shmget (obtainkey(), SHM_SIZE, 0777| IPC_CREAT);
    letra = (Deck* )shmat (idMemory, NULL,0);
}

key_t obtainkey(){
    return ftok("/bin/ls",24);
}

void unlinkMemory(){
    shmdt((Deck*)letra);

}

Deck* obtainMemory(){
    return letra;
}

void destroyMemory(){
    shmctl(idMemory, IPC_RMID, (struct shmid_ds*)NULL);
    unlink(" ");
}
#包括
#包括
#包括
#包括
int-idMemory;
类型定义结构{
字符*卡片;
}甲板;
甲板*莱特拉;
#定义SHM_大小1024
void stablishMemory(){
idMemory=shmget(获取键(),SHM|u大小,0777|IPC|u创建);
letra=(Deck*)shmat(idMemory,NULL,0);
}
密钥\u t获得密钥(){
返回ftok(“/bin/ls”,24);
}
void unlinkMemory(){
shmdt((甲板*)letra);
}
Deck*获取内存(){
返回莱特拉;
}
空内存(){
shmctl(idMemory,IPC_RMID,(struct shmid_ds*)NULL);
取消链接(“”);
}

此结构不是独立的。结构可能在共享内存中,但指针卡可以指向任何地方

typedef struct {
    char *cards;  // the memory that cards points to could be outside the segment
} Deck;
您必须分配卡(它是一个悬空指针),但更重要的是,您还必须从共享区域分配卡,或使其成为一个内联缓冲区(在结构内),如:

或者:

deck = allocSharedMem(sizeof(*deck));
deck->cards = allocSharedMem(52);
或者将其内联:

typedef struct {
    char cards[52];
} Deck;

您没有检查任何错误。@o11c什么类型的错误?诸如
ftok
shmget
之类的函数都可能返回错误。检查错误不是可选的。这非常有效,我添加了方括号并对代码进行了一些修改。谢谢@codenheim