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

Question on memory allocation for multi-dimensional array

I don't get why the correct way to allocate memory space for
3d-array should be like this in C++:

long (*ptr)[10][15];
ptr=new long[5][10][15];

and when deallocation

delete[] ptr;

Why declare the last two dimensions (*ptr)[10][15] first ?
Why not
long (*ptr)[5][10];
or
long *ptr=new long[5][10][15];

And why deallocate an 3d-array only use

delete[] ptr;
not
delete[][][] ptr;

Can anybody shed some light on the questions above ?
I'm new to C++ and is eager to get a better understanding of it,
Your help would be appreciated. Thanx in advance :)
Gary.
Jul 22 '05 #1
1 2239
sugaray wrote in news:ad**************************@posting.google.c om:
I don't get why the correct way to allocate memory space for
3d-array should be like this in C++:

long (*ptr)[10][15];
ptr=new long[5][10][15];
Perhapse it would help to understand that a C/C++ multi-dimension array
is really a thin vinear over a single-dimension array.

For example int a[10][5]; is implemented as a single block of storage
with 50 contiguous elements, a bit like int a_storage[50];.

Then when you reference a[n] the compiler translates the expression
a[n] to a_storage[ n * 10 ] (10 being the first declared dimension).
The type of the resulting expresion is int *, so a[n][m] is evaluated
as (a[n])[m] which is translated to a_storage[ (n * 10) + m ] with
type int.
Example 2: int b[2][3][4]; stored as int b_storage[ 2 * 3 * 4 ];
b[n] evaluates as b_storage[ n * 3 * 4 ] the type it evaluates to
is int (*)[4], b[n][m] evaluates to b_storage[ (n * 3 * 4) + (m * 4) ]
the type it evaluates to is int *. b[n][m][o] evaluates to
b_storage[ (n * 3 * 4) + (m * 4) + o ].

and when deallocation

delete[] ptr;
The compiler need's to know that ptr was initialized with a new []
expression, this is so it can find out in some way how many elements
were allocated (the new long[5][10][15] expression stored this
information (5 * 10 * 15 == 750 longs) when it allocated the memory).
This is so the compiler can loop over the array calling the destructor
for each element. Note that it only needs the total number of elements
as even though the array is multi-dimensional its stored as a single
dimensined array.

Why declare the last two dimensions (*ptr)[10][15] first ?
This is a result of the way pointer (and thus array) arithmatic is
done in C/C++.
Why not
long (*ptr)[5][10];
or
long *ptr=new long[5][10][15];
This simply won't work as it isn't possible to evaluate ptr[n]
as the size of the object (sub-array) it referes to is not part
of the type of ptr. In the example the sub-array is 10 * 15 long's
in size, but that information is *not* in the declaration of ptr.

And why deallocate an 3d-array only use

delete[] ptr;
not
delete[][][] ptr;


As explaned above all the compiler need to know is wether its deleteing
a single object (delete ptr) or an array (delete[] ptr).

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

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

Similar topics

18
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a...
16
by: Jacob | last post by:
It is common practice (I've heard) to always catch allocation exceptions from "new". How is done in practice? What would be a common procedure (path of sequence) after such an event has occured?...
9
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
6
by: Sandeep Chikkerur | last post by:
Hi, If the entire heap memory for dynamic allocation is not available, does the compiler always return NULL ? eg: char *s; s = (char *)malloc(...);
18
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard usage of setjmp() and longjmp() could result in...
1
by: Silent Ocean | last post by:
Hi All I have following questions regarding C# Assembly and Threading. Let me know the precise answer or lead me to the proper materials. 1. Is memory leakeage possible in .Net Manager...
3
by: EasyKev | last post by:
We have been trying to upgrade all our C++ projects from VC6 to VS .Net2003 for a while (before VS 2005 arrived), and seem to be stuck now because of the performance degradation seen for the same...
20
by: Daniel | last post by:
I have the following three classes class A { public: virtual void f() = 0; }; class B: public A {
13
by: Ilias Lazaridis | last post by:
How to detect memory leaks of python programms, which run in an environment like this: * Suse Linux 9.3 * Apache * mod_python The problem occoured after some updates on the infrastructure....
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...

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.