hi,
Code:
matrix = (struct field_struct **) calloc(1,rows*sizeof(struct field_struct));
also works but
Code:
matrix = (struct field_struct **) calloc(1,rows*sizeof(struct field_struct *));
allocates less memory, right? changed it and works, too
using
Code:
sizeof(struct field_struct)
seems the best way considering possible changes of the struct
The reason I'm using calloc is the initialization of all variables in every field. I thought that it would be the right way to do it this way :/ First allocate memory in "main memory" and do other things with the variable optionally and then copy the "result" to the shared memory area where other processes can use it...
I read a tutorial and found it the other way round means first map the shared memory and then alter it / write in it directly
But the correct assignment I'm not sure of.
So here is what I'm thinking:
Code:
//create memory area
// "5478" is a "random" number, size is matrixdimension*size of the struct, 0666 read and write for other processes...
int shID=shmget(5478,rows*columns*sizeof(struct field_struct,0666);
// pointer to the matrix?! right? or two times "*"?
struct field_struct *myPtr;
// ID vof shmget, 0 for free assignment of memory, 0 for read/write
myPtr = shmat(shID, 0, 0);
// adress from pointer equals the matrix adress?!
matrix =myPtr;
// matrix
struct field_struct ** matrix;
matrix = (struct field_struct **) calloc(1,rows*sizeof(struct field_struct *));
if (matrix != NULL)
{
for(i=0;i<rows;i++)
{
matrix[i] = (struct field_struct*) calloc(1,columns*sizeof(struct field_struct));
}
}
As you can see the order in which the assignments have to be used is not clear to me
So the aim is following:
one function creates a matrix and another variable structure
these should be stored in a shared memory area
so that another function can access the data and use it for calculations and then alter the shared memory area
Can anyone help me?
Alex