From sjain@cs.nmsu.edu Wed Feb 9 20:42:22 2005 From: Samat Jain To: Shaun Cooper Subject: Re: double array offsets Date: Wed, 9 Feb 2005 20:42:22 -0700 User-Agent: KMail/1.7.2 Cc: Raymond Lara , cs474-01@nmsu.edu References: <20050210030519.GA71669@nmsu.edu> In-Reply-To: <20050210030519.GA71669@nmsu.edu> X-KMail-Link-Message: 153533 X-KMail-Link-Type: reply X-KMail-Identity: 30369234 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200502092042.22361.sjain@cs.nmsu.edu> Status: RO X-Status: RS X-KMail-EncryptionState: X-KMail-SignatureState: X-KMail-MDN-Sent: X-Length: 2498 X-UID: 1607 On Wednesday 09 February 2005 20:05, Shaun Cooper wrote: > On Wed, Feb 09, 2005 at 07:46:51PM -0700, Raymond Lara wrote: > > Is there a smart way of declaring a shared memory segment so I dont have > > to do elaborate offsetting with my double array? I'd hate to have to go > > through my program and change every matrix[x][y] to *((matrix*x)+y). I > > mean I will if i have to but I KNOW there must be an easier way. This is why I had asked if there was a more intelligent matrix representation in class Tuesday--I had already spent the time converting all my matrix references to using pointer arithmetic. It is things like this that make me wish I was using C++ so I could have a "Matrix" class and abstract all this... By the way, though I'm not sure what your variables are/mean, I think the equivalent of: matrix[x][y] would be (provided you knew the number of columns): *(matrix+num_cols*x+y) Also some sweet elegant method someone told me about after I spent rewriting all the above uses pointer-pointers (no guarantees against errors in this pseudocode): int **matrix int *shared_mem_segment shared_mem_segment = allocate_shared_memory(num_rows*num_columns*sizeof(int)) matrix = malloc(rows*sizeof(int *)) // does this need to be shared too? matrix[0] = shared_mem_segment for ( row=1; row < num_rows; row++) matrix[row] =matrix[row-1] + num_cols and then you could continue to use matrix[x][y] without any issue (supposedly). Again, I've not tried this. Good luck, Samat -- Samat Jain Watson's Law: The reliability of machinery is inversely proportional to the number and significance of any persons watching it. -- Anonymous (552)