473,395 Members | 1,516 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,395 software developers and data experts.

plz help me out(Dynamic Allocation)

how to create double dimensional and 3 dimensional arrays dynamically(using new operator).please help me out as soon as possible.
Aug 16 '06 #1
1 1376
Banfa
9,065 Expert Mod 8TB
I wouldn't try.

I would create a single dimensioned array and use arithmatic to calculate the correct index values.

For instance suppose you want the equivilent of

int array[X][Y][Z];

Where X, Y and Z represent any (sensible) integer value.

This actually has X*Y*Z entries so I would allocate a single dimensioned array with this many entries

int *array = new int[X*Y*Z];

and write a function to calculate the array index give x, y and z

Expand|Select|Wrap|Line Numbers
  1. inline unsigned arrayIX(unsigned x, unsigned y, unsigned z)
  2. {
  3.     assert(x<X);
  4.     assert(y<Y);
  5.     assert(z<Z);
  6.  
  7.     return (x*Y*Z+y*Z+z);
  8. }
  9.  
The I can use the function to access array

array[arrayIX(1,4,6)];

since arrayIX is inline and is doing a similar calculation to the one that would have to be done anyway there is not going to be a big difference in performance (especially for an optomising compiler)

and the new/delete process is much easier since it only envolves 1 new and 1 delete.
Aug 16 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
0
by: Jack Black | last post by:
Well, here's what I want to do: the user enters one or more keywords. Those keywords are then run individually against several different tables, each presumably returning between 0 and -x- records...
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; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
0
by: Kelly Bert Manning | last post by:
I didn't get any hits when I searched deja for DSN1COPY dynamic allocation so either everyone else had no trouble realizing that this happens or nobody else has ever come across it. I've used...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.