Paulina L
Paulina L

Reputation: 11

How to allocate a struct to shared memory i c++?

I have a vague idea of how to work with shared memory in C and I'm trying to use the same approach in a C++ program. I want to share a struct:

typedef struct 
{
    string passw;
    string encoded;
    string tries;
    char steps;
}gameInfo;

gameInfo *gI;

And this is how I'm trying to do it:

int memory;
memory = shmget(IPC_PRIVATE, sizeof(gameInfo)*max_players, IPC_CREAT | 0600);
if(memory==-1)
    {printf("Shared memory error");}

(Later)

*gI = shmat(memory,NULL,0);

And I get "error: no match for ‘operator=’ (operand types are ‘gameInfo’ and ‘void*’)" errors. What would be the quickest fix?

Upvotes: 0

Views: 247

Answers (1)

Even if you managed to understand how to use placement new, you would not be able to get your example running on Linux using POSIX shared memory from shm_overview(7).

Because std::string is not a POD class and contains internal pointers whose role and behavior is not specified.

Upvotes: 2

Related Questions