Skip to content
Advertisement

Segmentation fault error as the reader tries to read from the shared memory

I am trying to understand the System V shared memory APIs. I created a small program where one writes into the shared memory and another reads from the shared memory. But for some reason, I am getting an error :

JavaScript

as I try to read from the shared memory. I could not find the reason for it.

Here is what I have done:

The following program writes into the shared memory.

JavaScript

and the following code attempts to read from the shared memory.

JavaScript

The above reader program results in a Segmentation fault: 11 error. What could be the reason for this? What is it that I am doing wrong?

Advertisement

Answer

JavaScript

That code makes no sense. This puts a pointer in shared memory.

Your code passes a pointer to a string constant from one program to the other. Then it dereferences that pointer in the other program. But what sense does that make? It’s a pointer into the other program’s memory space that means nothing to a different program.

If you want to pass data from one program to another, you need to actually put the data you want to pass in shared memory. Putting a pointer to it in shared memory won’t do any good if the data itself isn’t in shared memory!

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement