473,804 Members | 3,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<siz ex;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<siz ex;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 1410
On Jul 3, 1:30*pm, raphfrk <raph...@netsca pe.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<siz ex;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<siz ex;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...@netsca pe.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*****@netsca pe.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<siz ex;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<siz ex;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*****@netsca pe.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
2400
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
3764
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 believe the problem centers around my handling of strings, arrays, pointers and dynamic memory allocation. Here is the problem I'm trying to solve: I want to fill a list box with a list of Project Names from a database (in Palm this is more...
3
17426
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 previous data (like ReDim in VB). Any ideas ?
60
10212
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: > http://groups.google.com/group/comp.lang.c++/msg/db577c43260a5310?hl > >Another way is to create a one dimensional array and handle the >indexing yourself (index = row * row_size + col). This is readily >implemented in template classes that can create dynamically allocated >multi-dimensional...
4
2473
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..... //trying to get input from the user to add all the numbers that user inputs //tried to do it without dynamic memory usage but probably cannot achieve it //here is using "new" and "delete" operator....
11
3063
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
7055
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, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money. Note The above informaton is...
1
7979
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
4
1892
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 compiles ok, however, when it gets following error when running Unhandled exception at 0x7c812aeb in Value.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0013f988.. Then I turn to use the "new" method like the follows,
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10323
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10310
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
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 we have to send another system
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.