Velocity Reviews > Map 2D array to contiguous memory?

# Map 2D array to contiguous memory?

boltar2003@boltar.world
Guest
Posts: n/a

 07-16-2012
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
Guest
Posts: n/a

 07-16-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

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
Guest
Posts: n/a

 07-16-2012
On Mon, 16 Jul 2012 12:34:28 +0100
Ben Bacarisse <(E-Mail Removed)> 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

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
Guest
Posts: n/a

 07-16-2012
(E-Mail Removed) 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
Guest
Posts: n/a

 07-16-2012
On 7/16/2012 5:23 AM, (E-Mail Removed) 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
(E-Mail Removed)d

Heikki Kallasjoki
Guest
Posts: n/a

 07-16-2012
On 2012-07-16, Eric Sosman <(E-Mail Removed)> wrote:
> On 7/16/2012 5:23 AM, (E-Mail Removed) 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
Guest
Posts: n/a

 07-16-2012
On Mon, 16 Jul 2012 08:38:14 -0400
Eric Sosman <(E-Mail Removed)> wrote:
>On 7/16/2012 5:23 AM, (E-Mail Removed) 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 )

B2003

Eric Sosman
Guest
Posts: n/a

 07-16-2012
On 7/16/2012 8:49 AM, Heikki Kallasjoki wrote:
> On 2012-07-16, Eric Sosman <(E-Mail Removed)> wrote:
>> On 7/16/2012 5:23 AM, (E-Mail Removed) 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
(E-Mail Removed)d

boltar2003@boltar.world
Guest
Posts: n/a

 07-16-2012
On Mon, 16 Jul 2012 12:49:42 GMT
Heikki Kallasjoki <(E-Mail Removed)> wrote:
>On 2012-07-16, Eric Sosman <(E-Mail Removed)> wrote:
>> On 7/16/2012 5:23 AM, (E-Mail Removed) 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
Guest
Posts: n/a

 07-16-2012
On 2012-07-16, (E-Mail Removed) <(E-Mail Removed)> wrote:
> On Mon, 16 Jul 2012 12:49:42 GMT
> Heikki Kallasjoki <(E-Mail Removed)> wrote:
>>On 2012-07-16, Eric Sosman <(E-Mail Removed)> wrote:
>>> On 7/16/2012 5:23 AM, (E-Mail Removed) 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