473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Not fully comprehending arrays and dynamic memory.

I'd like to allocate some memory for a two dimensional array.

The problem is that whenever I try to use pointer arithmetic I screw up.
I just want to use conventional array type statements to get at the memory.

Is there a way to do this?

--
Mark Healey
marknews(at)healeyonline(dot)com

Apr 13 '06 #1
4 1594
Mark Healey <di*@spammer.die> writes:
I'd like to allocate some memory for a two dimensional array.

[...]

<http://www.c-faq.com/aryptr/dynmuldimary.html>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 13 '06 #2
On 2006-04-13, Mark Healey <di*@spammer.die> wrote:
I'd like to allocate some memory for a two dimensional array.

The problem is that whenever I try to use pointer arithmetic I screw up.
I just want to use conventional array type statements to get at the memory.

Is there a way to do this?


It can be done, but it's a bit tricky, and even easier to screw up I
would say.

You can allocate one like this:

int (*d)[3][3] = malloc(sizeof *d);

And use it like this:

(*d)[2][1] = 100;

...

free(d);

etc.

It might be easier to use a typedef:

typedef int matrix_t[3][3];
matrix_t *m = malloc(sizeof *m);

...

(*m)[2][1] = 100;

...

free(m);

With this way of doing it both d and m are pointers to a single block of
memory, which only has to be freed once. Other solutions involve making
arrays of pointers (one pointer for each row), and potentially calling
malloc to allocate each row. It's important to be clear which you're
doing and to call free the right number of times.
Apr 13 '06 #3
Groovy hepcat Ben C was jivin' on 13 Apr 2006 09:45:02 GMT in
comp.lang.c.
Re: Not fully comprehending arrays and dynamic memory.'s a cool scene!
Dig it!
On 2006-04-13, Mark Healey <di*@spammer.die> wrote:
I'd like to allocate some memory for a two dimensional array.

The problem is that whenever I try to use pointer arithmetic I screw up.
I just want to use conventional array type statements to get at the memory.

Is there a way to do this?


It can be done, but it's a bit tricky, and even easier to screw up I
would say.

You can allocate one like this:

int (*d)[3][3] = malloc(sizeof *d);

And use it like this:

(*d)[2][1] = 100;


Or, even easier, like this:

#define ROWS 3
#define COLS 3
int (*d)[COLS] = malloc(ROWS * sizeof *d);
.... /* Checking for errors, etc. */
d[2][1] = 100;

Or, slightly trickier (because you have to handle multiple
allocations and deallocations), like so:

#define ROWS 3
#define COLS 3
int i;
int **d = malloc(ROWS * sizeof *d);
.... /* Checking for errors, etc. */
for(i = 0; i < ROWS; i++)
{
d[i] = malloc(COLS * sizeof *d[i]);
... /* Checking for errors, etc. */
}

d[2][1] = 100;

Or, even better still, simpler (because there are only two
allocations):

#define ROWS 3
#define COLS 3
int i;
int **d = malloc(ROWS * sizeof *d);
.... /* Checking for errors, etc. */
*d = malloc(ROWS * COLS * sizeof *d);
.... /* Checking for errors, etc. */
for(i = 1; i < ROWS; i++)
{
d[i] = d[i - 1] + COLS;
}

d[2][1] = 100;

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Apr 16 '06 #4
On 2006-04-16, Peter "Shaggy" Haywood <ph******@alphalink.com.au.NO.SPAM> wrote:
Groovy hepcat Ben C was jivin' on 13 Apr 2006 09:45:02 GMT in
comp.lang.c.
Re: Not fully comprehending arrays and dynamic memory.'s a cool scene!
Dig it!
On 2006-04-13, Mark Healey <di*@spammer.die> wrote:
I'd like to allocate some memory for a two dimensional array.

The problem is that whenever I try to use pointer arithmetic I screw up.
I just want to use conventional array type statements to get at the memory.

Is there a way to do this?


It can be done, but it's a bit tricky, and even easier to screw up I
would say.

You can allocate one like this:

int (*d)[3][3] = malloc(sizeof *d);

And use it like this:

(*d)[2][1] = 100;

Or, even easier, like this:

#define ROWS 3
#define COLS 3
int (*d)[COLS] = malloc(ROWS * sizeof *d);
... /* Checking for errors, etc. */
d[2][1] = 100;
This is certainly nicer, and makes sense because after all when you
allocate a one-dimensional array of N ints, you usually do it with:

int *p = malloc(N * sizeof *p);

not with:

int (*p)[N] = malloc(sizeof *p);
Or, slightly trickier (because you have to handle multiple allocations
and deallocations), like so: [...]


I would say it's better to avoid multiple allocations except when you
need them (when the rows are different lengths, say).
Apr 16 '06 #5

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

Similar topics

3
by: meyousikmann | last post by:
The following code just sets up and fills a dynamic array of integers. #include <cstdlib> int main() { int* intArray = NULL; int count; count = 20;
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
15
by: Paul Morrison | last post by:
Hi all, I need to come up with some differences between arrays in Java and C, I have searched Google and so far all I have found is the following: Arrays in Java are reference types with...
1
by: KioKrofov | last post by:
Hello, I am trying to find out how Dynamic Arrays are actually stored in memory. Really, I want to know how Vectors are stored in memory, but I deduce they are stored the same way. If you...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.