Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shared memory

Status
Not open for further replies.

saidolsol

Programmer
May 31, 2001
10
ES
hi all,

if i want to use pointers inside shared memory, how do i have to get and attach it. For example...

struct shm {
int *numbers;
int i1;
int i2;
int i3;
} *pshm;

Say i have N numbers to put into the array, then...

IDshm = shmget(KEY, (4*N)+4+4+4, IPC_CREAT | 0666)

then attach...

pshm = (struct shm*)shmat(IDshm, 0, 0)

Then i have a pointer to the shared memory segment.
To get the pointer to the array inside the structure...

pshm->numbers = (int *)pshm[4]; ( 4 ----> size of the pointer )

Is this ok? My application throws a segmentation fault and i think it could be the problem. The program is not so simple as above so this could not be the wrong thing.

I think that my explanation is not quite well ( as my english ), but i hope someone will help me...

thanx.
gracias.
gràcies.
a10.
 
Are you using Windows? John Fill
1c.bmp


ivfmd@mail.md
 
I dont fully understand what you are trying to do but here is what i see

IDshm = shmget(KEY, (4*N)+4+4+4, IPC_CREAT | 0666)

this line here... it looks like you are getting a chunk of shared memory? And it looks like you are trying to use the (4*N)+4+4+4 as the size of the shared memory to create. This is where i think the problem is coming from. If by

"Say i have N numbers to put into the array, then..."

You mean you have N numbers you want to store in shared memory as well as i1,i2 & i3 I would suggest declaring your struct as follows

struct shmem{
int i1;
int i2;
int i3;

int* numbers;
};


I am assuming that either i1, i2 or i3 will tell you how many numbers are stored in the array "numbers", if not, having a value to tell you that will be helpful. For this example lets say i1 has the count of numbers;

now... when you create your shared memory... you know you want N numbers

pshm->i1 = N;

now, the easiest way i can see doing this is trough a loop. When the block of shared memory is created, i1,i2 & i3 are at the beginning, and at the end is all this allocated shared memory of size 4*N which is equivalent to sizeof(int)*N which i would suggest using instead. Now you can treat numbers as an array of N integers but as I am not 100% sure the [] operator on the array will work (first i would test it and see if I got a seg fault) i would also try it as follows

for(int i = 0;i<n;i++)
*(numbers+i)=next_int_value;

and see where that gets you.

Let me know if this was any help or if i mis-interpreted what you were asking.

Matt


 
hi Matt,
you have not mis-interpreted my question. Later i will test with that new struct, and with the array references you suggested.

bye.
Adiós.
Adéu.
a10.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top