Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Map 2D array to contiguous memory?

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-16-2012
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.
 
Reply With Quote
 
 
 
 
boltar2003@boltar.world
Guest
Posts: n/a
 
      07-16-2012
On Mon, 16 Jul 2012 12:34:28 +0100
Ben Bacarisse <> 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


 
Reply With Quote
 
Anders Wegge Keller
Guest
Posts: n/a
 
      07-16-2012
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.*
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-16-2012
On 7/16/2012 5:23 AM, 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
d


 
Reply With Quote
 
Heikki Kallasjoki
Guest
Posts: n/a
 
      07-16-2012
On 2012-07-16, Eric Sosman <> wrote:
> On 7/16/2012 5:23 AM, 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
 
Reply With Quote
 
boltar2003@boltar.world
Guest
Posts: n/a
 
      07-16-2012
On Mon, 16 Jul 2012 08:38:14 -0400
Eric Sosman <> wrote:
>On 7/16/2012 5:23 AM, 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


 
Reply With Quote
 
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 <> wrote:
>> On 7/16/2012 5:23 AM, 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
d


 
Reply With Quote
 
boltar2003@boltar.world
Guest
Posts: n/a
 
      07-16-2012
On Mon, 16 Jul 2012 12:49:42 GMT
Heikki Kallasjoki <fis+> wrote:
>On 2012-07-16, Eric Sosman <> wrote:
>> On 7/16/2012 5:23 AM, 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


 
Reply With Quote
 
Heikki Kallasjoki
Guest
Posts: n/a
 
      07-16-2012
On 2012-07-16, <> wrote:
> On Mon, 16 Jul 2012 12:49:42 GMT
> Heikki Kallasjoki <fis+> wrote:
>>On 2012-07-16, Eric Sosman <> wrote:
>>> On 7/16/2012 5:23 AM, 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
contiguous files =?Utf-8?B?ZGhvb3Zlcg==?= Microsoft Certification 4 12-28-2005 05:21 AM
Routing Question - Non-contiguous IP Blocks Edumac Cisco 1 09-01-2005 04:41 PM
Are array members guaranteed to be contiguous in physical memory? Olumide C Programming 9 11-30-2004 02:54 PM
2D arrays: are all elements in a 2d array guarenteed to be contiguous? Paul C++ 1 11-24-2004 08:07 AM
Is the storage for a std::vector<T> guaranteed to be contiguous? (from FAQ) Newsgroup - Ann C++ 0 08-15-2003 01:42 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57