473,511 Members | 17,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically-allocated Multi-dimensional Arrays

The C-FAQ describes some techniques here: http://c-faq.com/aryptr/dynmuldimary.html

I was using something slightly different from the C-FAQ and I was
wondering if it was legal.

Say I want a two-dimensional array, like this:

int x[2][3];

but I want it dynamically-allocated, and I want expressions that refer
to its elements to use the common subscript syntax.

int i = x[0][1];

I also want it to be one contiguous piece of memory, to avoid overhead
and complications.

Is this legal?

int (*x)[3] = (int (*)[3]) malloc(sizeof(int) * 2 * 3);

x[0][0] = 1;
x[0][1] = ...;
x[1][1] = ...;

I don't see anything in the standard that wouldn't allow this. It is
also cleaner than what the C-FAQ mentions, which is:

int (*x)[2][3] = (int (*)[2][3]) malloc(sizeof(int) * 2 * 3);

(*x)[0][0] = 1;
(*x)[0][1] = ...;
(*x)[1][1] = ...;

which I want to avoid.

Thanks for any input. If this is illegal, a pointer into the standard
would be appreciated.

if this is legal, I will look into updating the C-FAQ.

-Frank

Mar 16 '07 #1
7 8707
On Mar 15, 8:12 pm, "Serpent" <fwallingf...@gmail.comwrote:
The C-FAQ describes some techniques here:http://c-faq.com/aryptr/dynmuldimary.html

I was using something slightly different from the C-FAQ and I was
wondering if it was legal.

Say I want a two-dimensional array, like this:

int x[2][3];

but I want it dynamically-allocated, and I want expressions that refer
to its elements to use the common subscript syntax.

int i = x[0][1];

I also want it to be one contiguous piece of memory, to avoid overhead
and complications.
The second way listed in the FAQ is what you want:

int **array2 = malloc(nrows * sizeof(int *));
array2[0] = malloc(nrows * ncolumns * sizeof(int));
for(i = 1; i < nrows; i++)
array2[i] = array2[0] + i * ncolumns;

It happens to also be more or less the way that BLAS does it, if I
remember correctly.

Brent

Mar 16 '07 #2
I also want it to be one contiguous piece of memory,
to avoid overhead and complications.

The second way listed in the FAQ is what you want:

int **array2 = malloc(nrows * sizeof(int *));
array2[0] = malloc(nrows * ncolumns * sizeof(int));
for(i = 1; i < nrows; i++)
array2[i] = array2[0] + i * ncolumns;
This looks like two pieces of memory to me. It also looks like
additional overhead.

I'm looking for a solution that uses one piece, like the one I
suggested. Is my suggestion legal C? It compiles and executes with no
out-of-bounds accesses on the platforms and compilers I've tested, and
I can't find any contradicting paragraphs in the standard, but I may
have missed something.

-Frank

Mar 16 '07 #3
Serpent wrote:
>
The C-FAQ describes some techniques here: http://c-faq.com/aryptr/dynmuldimary.html

I was using something slightly different from the C-FAQ and I was
wondering if it was legal.

Say I want a two-dimensional array, like this:

int x[2][3];

but I want it dynamically-allocated, and I want expressions that refer
to its elements to use the common subscript syntax.

int i = x[0][1];

I also want it to be one contiguous piece of memory, to avoid overhead
and complications.

Is this legal?

int (*x)[3] = (int (*)[3]) malloc(sizeof(int) * 2 * 3);

x[0][0] = 1;
x[0][1] = ...;
x[1][1] = ...;

I don't see anything in the standard that wouldn't allow this.
The only thing wrong with it,
is that only one of the dimensions can be a variable.
If that's OK with you, then there's nothing wrong with it.

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

#define XX 2
#define YY 3

int main(void)
{
int x, y;
int (*array)[YY];

x = XX;
array = malloc(x * sizeof *array);
if (array == NULL) {
puts("array == NULL");
exit(EXIT_FAILURE);
}
while (x-- != 0) {
for (y = 0; y != YY; ++y) {
array[x][y] = x + y;
}
}
for (x = 0; x != XX; ++x) {
for (y = 0; y != YY; ++y) {
printf("%d\n", array[x][y]);
}
}
free(array);
return 0;
}

/* END new.c */

--
pete
Mar 16 '07 #4
On Mar 15, 9:30 pm, "Serpent" <fwallingf...@gmail.comwrote:
I also want it to be one contiguous piece of memory,
to avoid overhead and complications.
The second way listed in the FAQ is what you want:
int **array2 = malloc(nrows * sizeof(int *));
array2[0] = malloc(nrows * ncolumns * sizeof(int));
for(i = 1; i < nrows; i++)
array2[i] = array2[0] + i * ncolumns;

This looks like two pieces of memory to me. It also looks like
additional overhead.
You have to allocate the pointers for *array2[nrows]. A two-
dimensional array in C looks like a pointer to an array of pointers.

The reason that technique is useful is that you can dynamically
allocate an array of any dimensions at runtime.

You can also, for efficiency, use array2[0] as if it were a one-
dimensional array of size rows*columns.
I'm looking for a solution that uses one piece, like the one I
suggested. Is my suggestion legal C?
Now that I thought through what your code does, you do end up with an
array that's laid out like a statically allocated array. The
disadvantage is, you have to declare the number of columns at compile
time.

int (*x)[3] = (int (*)[3]) malloc((sizeof int) * nrows * 3);

is legal.

int (*x)[ncols] = (int (*)[ncols]) malloc((sizeof int) * nrows *
ncols);

is not.

And now that I've thought it through, I remember there's a long
discussion of these matters in Peter van Der Linden's _Expert C
Programming: Deep C Secrets_.

Brent

Mar 16 '07 #5
br************@gmail.com wrote:
int (*x)[3] = (int (*)[3]) malloc((sizeof int) * nrows * 3);
is legal.
int (*x)[ncols] = (int (*)[ncols]) malloc((sizeof int) * nrows *
ncols);
is not.
Isn't the second form legal as of C99 ?
--
pa at panix dot com
Mar 16 '07 #6
On Mar 16, 1:34 pm, p...@see.signature.invalid (Pierre Asselin) wrote:
brent.buesc...@gmail.com wrote:
int (*x)[3] = (int (*)[3]) malloc((sizeof int) * nrows * 3);
is legal.
int (*x)[ncols] = (int (*)[ncols]) malloc((sizeof int) * nrows *
ncols);
is not.

Isn't the second form legal as of C99 ?

--
pa at panix dot com
I don't know. So you have a pointer to an array of ncols ints. C99
should let you allocate that, right? And OK, it ends up getting
pointed to a contiguous block of ints on the heap that's ncols*nnrows
big. Seems like it should work.

Note that in that case you could just write

int x[ncols][nrows];

couldn't you? Or does that approach gives you an Iliffe vector like
in the example I pulled from the FAQ? It does apparently allocate the
array on the stack, which you might want to avoid.

As an aside, is it safe to trust real-world implementations of C99
variable-size arrays yet?

Brent

Mar 16 '07 #7
In article <11*********************@l77g2000hsb.googlegroups. com>
Serpent <fw**********@gmail.comwrote:
>The C-FAQ describes some techniques here:
http://c-faq.com/aryptr/dynmuldimary.html

I was using something slightly different from the C-FAQ and I was
wondering if it was legal.

Say I want a two-dimensional array, like this:

int x[2][3];

but I want it dynamically-allocated, and I want expressions that refer
to its elements to use the common subscript syntax.

int i = x[0][1];

I also want it to be one contiguous piece of memory, to avoid overhead
and complications.

Is this legal?

int (*x)[3] = (int (*)[3]) malloc(sizeof(int) * 2 * 3);
Yes, but it is better to write, e.g.:

int (*x)[3];
...
x = malloc(n * sizeof *x);

where "n" is the number of rows to use. If the malloc() succeeds,
you can then do:

for (i = 0; i < n; i++)
for (j = 0; j < 3; j++)
... operate on x[i][j] ...

Note that the number of columns is fixed: it must always be exactly
three. If you want the number of columns to be variable as well,
you have a problem.

In C99 (but not older versions of C), there is a new feature called
a "variable length array" or VLA. This new feature allows you to
change the number of columns, as well as the number of rows. Here,
you might write, e.g.:

void do_something(size_t m, size_t n) {
int arr[m][n];
size_t i, j;

for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
... operate on arr[i][j] ...

/* "arr" is automatic, so it vanishes when we return */
}

If the array needs to have "allocated" storage duration, i.e., come
from malloc(), rather than having automatic storage duration, you
can still do that:

void do_more(size_t m, size_t n) {
int (*p)[n];
size_t i, j;

p = malloc(m * sizeof *p);
if (p == NULL)
... handle error ...

for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
... operate on p[i][j] ...
}

but now there is an annoying problem: the type of "p" is "pointer
to VLA (of size n) of int", where n is determined by the call to
do_more(). So you must give up some degree of type-safety in order
to do something useful with the value in p (such as store it in a
return value, or store it in some "global" variable, or whatever)
before returning. It then becomes very easy to store the pointer
in a VLA of different "shape" (e.g., "pointer to VLA (of size 99)
of int" instad of "pointer to VLA (of size 33) of int"). If you
do this, tragedy will ensue. Hence, while VLAs are quite useful,
they are not a panacea.

If you do not have C99 (most people still do not, or at least not a
full version) and/or are unwilling to depend on having at least the
VLA feature, you have just two options.

A) Use a minimum of two malloc() calls: one to create the space
for the array, and one to create the space for a vector of
"row pointers". This technique is shown in the FAQ. It is
tempting to use one malloc() to create both regions, but to
do so invites the Gods of Alignment to strike your program
down when you least expect it. :-) (More seriously: some
machines have particular alignment constraints, and getting
both the row-vector *and* the data-area aligned requires
machine dependent trickery. The malloc() function contains
exactly this trickery, and calling it twice makes both areas
properly aligned.)

B) Use one malloc() call to allocate the data area, then do your
own "manual" calculations:

int *do_it_with_C89(size_t m, size_t n) {
int *p;
size_t i, j;

p = malloc(m * n * sizeof *p);
if (p == NULL)
... handle error ...
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
... operate on p[i * n + j] ...
return p;
}

Method (B) works in C99 as well, of course. While it is type-safe,
it is easy to make mistakes in remembering "m" and/or "n" and/or
doing the subscript calculation. You can minimize the chances of
such problems by using a structure to encapsulate everything:

/* in some header */
struct int_matrix {
size_t m;
size_t n;
int *p;
};
#ifndef NDEBUG
#define INT_MAT_ELEM(mp, i, j) \
(assert((i) < (mp)->m && (j) < (mp)->n), (mp)->p[(i) * (mp)->n + (j)])
#else
#define INT_MAT_ELEM(mp, i, j) ((mp)->p[(i) * (mp)->n + (j)])
#endif

/* in some source file that includes the above header, plus <stdlib.h*/
struct int_matrix *int_mat_create(size_t m, size_t n) {
struct int_matrix *mp;
size_t i, end;

mp = malloc(sizeof *mp);
if (mp == NULL)
return NULL;
mp->p = malloc(m * n * sizeof *mp->p);
if (mp->p == NULL) {
free(mp);
return NULL;
}
mp->m = m;
mp->n = n;

/* init to all-zeros */
for (i = 0, end = m * n; i < end; i++)
mp->p[i] = 0;

return mp;
}

void int_mat_destroy(struct int_matrix *mp) {
free(mp->p);
free(mp);
}

/* in some file that uses the integer matrix */
...
...
struct int_matrix *p;

p = int_mat_create(m, n);
if (p == NULL)
... handle error ...
...
INT_MAT_ELEM(p, i, j) = 42;
... other operations on INT_MAT_ELEM(p, i, j) ...

It is a little peculiar to have a function-like macro that expands
to an lvalue (so that you can assign to INT_MAT_ELEM(p,i,j) as in
the example above), but it should work.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Mar 17 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3247
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically...
8
4297
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
2
2910
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
1
2556
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
1
1022
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
7
6755
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
2
2441
by: Pete Moss | last post by:
During a postback event, I am having trouble retrieving the state of a CheckBox Control that I am dynamically adding to a DataGrid Control using ASP.NET 1.1. I have no trouble adding the...
9
7026
by: netasp | last post by:
hi all, how can I populate one aspx form when page is loading based on page ID? for example: loading page A (to search for VB code) would display labels and texboxes, dropdown lists all related...
4
2953
by: mohaaron | last post by:
I can think of a lot of reasons why this might need to be done but as far as I can tell it's not possible. I've been looking for a way to add HtmlTableRows to a table using a button click for a...
7
6633
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.