Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Map 2D array to contiguous memory? (http://www.velocityreviews.com/forums/t948150-map-2d-array-to-contiguous-memory.html)

boltar2003@boltar.world 07-16-2012 09:23 AM

Map 2D array to contiguous memory?
 
This is probably a question thats come up before but google doesn't seem to
be my friend on this one.

Is there some hack whereby a 2D array can be mapped onto a dynamically
allocated block of contiguous memory in C? Its easily done with 1D arrays
but if you use a double pointer for the 2D the memory can't be contiguous
as you have to allocate each single point in turn.

What I'd like to do is something like:

struct mystruct **array;

array = (struct mystruct **)<some preallocated shared memory>
:
:
array[1][2].val = 123;

Now I know this will crash as soon as it tries to dereference the 1st
level of the array but is there some clever way to do it?

Thanks for any help.

B2003


Ben Bacarisse 07-16-2012 11:34 AM

Re: Map 2D array to contiguous memory?
 
boltar2003@boltar.world writes:

> This is probably a question thats come up before but google doesn't seem to
> be my friend on this one.


The C FAQ is useful here though I don't think it answers this exact
question. http://c-faq.com/

> Is there some hack whereby a 2D array can be mapped onto a dynamically
> allocated block of contiguous memory in C? Its easily done with 1D arrays
> but if you use a double pointer for the 2D the memory can't be contiguous
> as you have to allocate each single point in turn.
>
> What I'd like to do is something like:
>
> struct mystruct **array;
>
> array = (struct mystruct **)<some preallocated shared memory>
> :
> :
> array[1][2].val = 123;
>
> Now I know this will crash as soon as it tries to dereference the 1st
> level of the array but is there some clever way to do it?


You can make the memory contiguous[1] (at the expense of some alignment
issues) but I want to ask why you need to do this. Why are you trying a
hand behind your back?

I am not saying that there aren't reasons to prefer this data
arrangement over another, but it often proves to be a waste of time to
explain just one of these when it later turns out that another, simpler,
arrangement meets the requirements just as well. In sort, what is it
that you are doing?

[1] So you don't think I'm just withholding for the sake of it, here's
how. Allocate space for array of pointer and for the array itself in
one blob. You might need some padding between them to ensure that that
structs are properly aligned. You then need to fix up the pointers to
point into this blob.

--
Ben.

boltar2003@boltar.world 07-16-2012 11:58 AM

Re: Map 2D array to contiguous memory?
 
On Mon, 16 Jul 2012 12:34:28 +0100
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>You can make the memory contiguous[1] (at the expense of some alignment
>issues) but I want to ask why you need to do this. Why are you trying a
>hand behind your back?


I'm not. I want 2 programs to share multidimentional data via shared
memory which as you might be aware consists of contiguous blocks of
memory. I'm simply wondering if there's a way to do this using the standard
C array syntax.

>[1] So you don't think I'm just withholding for the sake of it, here's
>how. Allocate space for array of pointer and for the array itself in
>one blob. You might need some padding between them to ensure that that
>structs are properly aligned. You then need to fix up the pointers to
>point into this blob.


There must be an easier way than that.

B2003



Anders Wegge Keller 07-16-2012 12:16 PM

Re: Map 2D array to contiguous memory?
 
boltar2003@boltar.world writes:

> This is probably a question thats come up before but google doesn't seem to
> be my friend on this one.
>
> Is there some hack whereby a 2D array can be mapped onto a dynamically
> allocated block of contiguous memory in C? Its easily done with 1D arrays
> but if you use a double pointer for the 2D the memory can't be contiguous
> as you have to allocate each single point in turn.


You can map the 2D array onto a 1D array. Instead of allocating
Arr2[xmax][ymax], instead allocate Arr1[xmax*ymax], and access it
as Arr1[xmax*y + x].

--
/Wegge

Leder efter redundant peering af dk.*,linux.debian.*

Eric Sosman 07-16-2012 12:38 PM

Re: Map 2D array to contiguous memory?
 
On 7/16/2012 5:23 AM, boltar2003@boltar.world wrote:
> This is probably a question thats come up before but google doesn't seem to
> be my friend on this one.
>
> Is there some hack whereby a 2D array can be mapped onto a dynamically
> allocated block of contiguous memory in C? Its easily done with 1D arrays
> but if you use a double pointer for the 2D the memory can't be contiguous
> as you have to allocate each single point in turn.
>
> What I'd like to do is something like:
>
> struct mystruct **array;
>
> array = (struct mystruct **)<some preallocated shared memory>
> :
> :
> array[1][2].val = 123;
>
> Now I know this will crash as soon as it tries to dereference the 1st
> level of the array but is there some clever way to do it?


struct mystruct (*arrayptr)[18][99] = (void*)&whatever;
#define array (*arrayptr)
array[9][17].member= 123;

--
Eric Sosman
esosman@ieee-dot-org.invalid



Heikki Kallasjoki 07-16-2012 12:49 PM

Re: Map 2D array to contiguous memory?
 
On 2012-07-16, Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
> On 7/16/2012 5:23 AM, boltar2003@boltar.world wrote:
>> What I'd like to do is something like:
>>
>> struct mystruct **array;
>>
>> array = (struct mystruct **)<some preallocated shared memory>
>> :
>> :
>> array[1][2].val = 123;
>>
>> Now I know this will crash as soon as it tries to dereference the 1st
>> level of the array but is there some clever way to do it?

>
> struct mystruct (*arrayptr)[18][99] = (void*)&whatever;
> #define array (*arrayptr)
> array[9][17].member= 123;


Is there a particular reason why not simply

struct mystruct (*thing)[99] = (void*)&whatever;
thing[9][17].member = 123;

instead of the above? (Especially the #define looks really suspicious.)

--
Heikki Kallasjoki

boltar2003@boltar.world 07-16-2012 12:49 PM

Re: Map 2D array to contiguous memory?
 
On Mon, 16 Jul 2012 08:38:14 -0400
Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
>On 7/16/2012 5:23 AM, boltar2003@boltar.world wrote:
>> This is probably a question thats come up before but google doesn't seem to
>> be my friend on this one.
>>
>> Is there some hack whereby a 2D array can be mapped onto a dynamically
>> allocated block of contiguous memory in C? Its easily done with 1D arrays
>> but if you use a double pointer for the 2D the memory can't be contiguous
>> as you have to allocate each single point in turn.
>>
>> What I'd like to do is something like:
>>
>> struct mystruct **array;
>>
>> array = (struct mystruct **)<some preallocated shared memory>
>> :
>> :
>> array[1][2].val = 123;
>>
>> Now I know this will crash as soon as it tries to dereference the 1st
>> level of the array but is there some clever way to do it?

>
> struct mystruct (*arrayptr)[18][99] = (void*)&whatever;
> #define array (*arrayptr)
> array[9][17].member= 123;


Excellent , just what I was looking for - thank you. There's always a simple
way eventually thats obvious when you see it :o)

B2003



Eric Sosman 07-16-2012 01:05 PM

Re: Map 2D array to contiguous memory?
 
On 7/16/2012 8:49 AM, Heikki Kallasjoki wrote:
> On 2012-07-16, Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
>> On 7/16/2012 5:23 AM, boltar2003@boltar.world wrote:
>>> What I'd like to do is something like:
>>>
>>> struct mystruct **array;
>>>
>>> array = (struct mystruct **)<some preallocated shared memory>
>>> :
>>> :
>>> array[1][2].val = 123;
>>>
>>> Now I know this will crash as soon as it tries to dereference the 1st
>>> level of the array but is there some clever way to do it?

>>
>> struct mystruct (*arrayptr)[18][99] = (void*)&whatever;
>> #define array (*arrayptr)
>> array[9][17].member= 123;

>
> Is there a particular reason why not simply
>
> struct mystruct (*thing)[99] = (void*)&whatever;
> thing[9][17].member = 123;
>
> instead of the above? (Especially the #define looks really suspicious.)


<facepalm> Yes, that would work. </facepalm>

<lame_excuse> With mine, `sizeof array' works better. </lame_excuse>

--
Eric Sosman
esosman@ieee-dot-org.invalid



boltar2003@boltar.world 07-16-2012 01:17 PM

Re: Map 2D array to contiguous memory?
 
On Mon, 16 Jul 2012 12:49:42 GMT
Heikki Kallasjoki <fis+usenet@zem.fi> wrote:
>On 2012-07-16, Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
>> On 7/16/2012 5:23 AM, boltar2003@boltar.world wrote:
>>> What I'd like to do is something like:
>>>
>>> struct mystruct **array;
>>>
>>> array = (struct mystruct **)<some preallocated shared memory>
>>> :
>>> :
>>> array[1][2].val = 123;
>>>
>>> Now I know this will crash as soon as it tries to dereference the 1st
>>> level of the array but is there some clever way to do it?

>>
>> struct mystruct (*arrayptr)[18][99] = (void*)&whatever;
>> #define array (*arrayptr)
>> array[9][17].member= 123;

>
>Is there a particular reason why not simply
>
> struct mystruct (*thing)[99] = (void*)&whatever;
> thing[9][17].member = 123;
>
>instead of the above? (Especially the #define looks really suspicious.)


It'll compile but it won't work properly because it doesn't know how big
the second dimension of the array is.

B2003



Heikki Kallasjoki 07-16-2012 01:31 PM

Re: Map 2D array to contiguous memory?
 
On 2012-07-16, boltar2003@boltar.world <boltar2003@boltar.world> wrote:
> On Mon, 16 Jul 2012 12:49:42 GMT
> Heikki Kallasjoki <fis+usenet@zem.fi> wrote:
>>On 2012-07-16, Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
>>> On 7/16/2012 5:23 AM, boltar2003@boltar.world wrote:
>>>> What I'd like to do is something like:
>>>>
>>>> struct mystruct **array;
>>>>
>>>> array = (struct mystruct **)<some preallocated shared memory>
>>>> :
>>>> :
>>>> array[1][2].val = 123;
>>>>
>>>> Now I know this will crash as soon as it tries to dereference the 1st
>>>> level of the array but is there some clever way to do it?
>>>
>>> struct mystruct (*arrayptr)[18][99] = (void*)&whatever;
>>> #define array (*arrayptr)
>>> array[9][17].member= 123;

>>
>>Is there a particular reason why not simply
>>
>> struct mystruct (*thing)[99] = (void*)&whatever;
>> thing[9][17].member = 123;
>>
>>instead of the above? (Especially the #define looks really suspicious.)

>
> It'll compile but it won't work properly because it doesn't know how big
> the second dimension of the array is.


It will certainly work properly, if you use it properly:

struct mystruct (*p)[42] = malloc(33 * sizeof *p);
p[32][41].member = 123; /* furthest corner of the 33-times-42 array */

The above is completely analoguous to the one-dimensional case (except
that the elements above are 42-element arrays of struct mystruct,
whereas here they are single struct mystructs):

struct mystruct *p = malloc(33 * sizeof *p);
p[32].member = 123; /* last element of the 33-element array */

(As mentioned, the version using a pointer to a 33-times-42-sized array,
a struct mystruct (*p)[33][42], does have the advantage that sizeof *p
will yield the size of the entire array, possibly simplifying the
allocation.)

--
Heikki Kallasjoki


All times are GMT. The time now is 09:08 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.