473,407 Members | 2,312 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,407 software developers and data experts.

2-d dynamic arrays - what is the recommended method ?

While I am here, I just thought I'd ask if there is a recommended way
of handling 2d dynamic arrays.

I implement them using the following kind of code.

------------------------------------------------

int fname( int sizex, int sizey, <other inputs)
{

int *(*bitmap);

int cntx;

bitmap = malloc( sizeof( *bitmap ) * sizex );
if( bitmap == NULL )
{
printf("Unable to allocate enough RAM in fname\n");
exit(0);
}

for( cntx=0;cntx<sizex;cntx++)
{
bitmap[cntx] = malloc( sizeof( *(bitmap[cntx]) ) * sizey );
if( bitmap[cntx] == NULL )
{
printf("Unable to allocate enough RAM in fname\n");
exit(0);
}
}

<code>

for( cntx=0;cntx<sizex;cntx++)
{
free(bitmap[cntx]);
}

free(bitmap);

}

-----------------------------------------

This seems to require that the following code be used to access each
element

(bitmap[xpos])[ypos]

Is there a neater way of doing it ?
Jul 3 '08 #1
6 1386
On Jul 3, 1:30*pm, raphfrk <raph...@netscape.netwrote:
While I am here, I just thought I'd ask if there is a recommended way
of handling 2d dynamic arrays.

I implement them using the following kind of code.

------------------------------------------------

int fname( *int sizex, int sizey, <other inputs)
{

int *(*bitmap);

int cntx;

bitmap = malloc( sizeof( *bitmap ) * sizex );
if( bitmap == NULL )
*{
*printf("Unable to allocate enough RAM in fname\n");
*exit(0);
*}

for( cntx=0;cntx<sizex;cntx++)
*{
*bitmap[cntx] = malloc( sizeof( *(bitmap[cntx]) ) * sizey );
*if( bitmap[cntx] == NULL )
* {
* printf("Unable to allocate enough RAM in fname\n");
* exit(0);
* }
*}

<code>

for( cntx=0;cntx<sizex;cntx++)
*{
*free(bitmap[cntx]);
*}

free(bitmap);

}

-----------------------------------------

This seems to require that the following code be used to access each
element

(bitmap[xpos])[ypos]

Is there a neater way of doing it ?
Have a look here:

http://c-faq.com/aryptr/dynmuldimary.html

-David
Jul 3 '08 #2
That is what I thought I did the first time, but had to put in the
brackets to get it to work (which is why I always use brackets now).

I guess I must have fixed the actual coding error by accident.
Jul 3 '08 #3
On Jul 4, 4:50 am, raphfrk <raph...@netscape.netwrote:
That is what I thought I did the first time, but had to put in the
brackets to get it to work (which is why I always use brackets now).

I guess I must have fixed the actual coding error by accident.
You mean:
int *(*bitmap);
The brackets are not required and are making the code unreadable. What
kind of errors you were getting without them?
>This seems to require that the following code be used to access each
element
>(bitmap[xpos])[ypos]
>Is there a neater way of doing it ?
How is this dirty? This seems to be the neatest way to access
individual elements in a 2-d array.
Jul 4 '08 #4
On Thu, 3 Jul 2008 10:30:00 -0700 (PDT), raphfrk
<ra*****@netscape.netwrote:
>While I am here, I just thought I'd ask if there is a recommended way
of handling 2d dynamic arrays.

I implement them using the following kind of code.

------------------------------------------------

int fname( int sizex, int sizey, <other inputs)
{

int *(*bitmap);
These parentheses are superfluous. int **bitmap is fine.
>
int cntx;

bitmap = malloc( sizeof( *bitmap ) * sizex );
if( bitmap == NULL )
{
printf("Unable to allocate enough RAM in fname\n");
exit(0);
}

for( cntx=0;cntx<sizex;cntx++)
{
bitmap[cntx] = malloc( sizeof( *(bitmap[cntx]) ) * sizey );
if( bitmap[cntx] == NULL )
{
printf("Unable to allocate enough RAM in fname\n");
exit(0);
}
}

<code>

for( cntx=0;cntx<sizex;cntx++)
{
free(bitmap[cntx]);
}

free(bitmap);

}

-----------------------------------------

This seems to require that the following code be used to access each
element

(bitmap[xpos])[ypos]
The same for these parentheses. bitmap[xpos][ypos] works.
>
Is there a neater way of doing it ?
There is an alternative but I don't consider it neater.

int *bitmap = malloc(sizex * sizey * sizeof *bitmap);

Everywhere you would like to use bitmap[x][y] you would need to use
bitmap[x*sizey+y]. Some even go so far as to create a macro
#define BITMAP(x,y) bitmap[x*sizey+y]
for this purpose.
Remove del for email
Jul 4 '08 #5
On Jul 4, 10:11 am, Barry Schwarz <schwa...@dqel.comwrote:
>
(bitmap[xpos])[ypos]

The same for these parentheses. bitmap[xpos][ypos] works.
Yeah, that is what I wanted. Not sure what I must have done wrong,
but it was giving segmentation faults. I then included the brackets
from then on, I must have fixed 2 things at once.
Jul 4 '08 #6
raphfrk <ra*****@netscape.netwrites:
While I am here, I just thought I'd ask if there is a recommended way
of handling 2d dynamic arrays.
<snip malloc array of malloc arrays>
Is there a neater way of doing it ?
There is if C99 is an option. You can just declare:

int bitmap[sizex][sizey];

or, if you prefer the safety of malloced arrays:

int (*bitmap)[sizey] = malloc(sizex * sizeof *bitmap);

and in both cases you keep the bitmap[x][y] access syntax.

--
Ben.
Jul 5 '08 #7

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

Similar topics

12
by: Tan Thuan Seah | last post by:
Hi all, I was told this in one of the university course I was doing. In C we may expect good performance for: double a, c, d; for (i=0; i<N; i++) for(j=0; j<N; j++) a = a + c *d;
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
3
by: Dusty | last post by:
How can I make my dynamic array ? I'm curios how the ArrayList does it. For example: string s = new string; and later I want to expand this array to more than 16 elements without loosing...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
4
by: learnfpga | last post by:
Here is a little code I wrote to add the numbers input by the user.....I was wondering if its possible to have the same functionality without using dynamic arrays.....just curious..... ...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
4
by: Atemporal | last post by:
hi, all, i got some problem when define and use large dynamic two dimension arrays. At first, i use vector<vector<double>p1, i have three such arrays and each is around 10000*10000, the program...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.