473,699 Members | 2,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Review: 3D-Array

Hi c.l.c,

Is this a correct method of allocating a 3-dimensional
array?

/* assume all malloc's succeed */
#define MAT 2
#define ROW 2
#define COL 2

char ***ptr = malloc ( MAT * sizeof *ptr );

for ( i = 0; i < MAT; i++ )
ptr[i] = malloc ( ROW * sizeof **ptr );

for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof ***ptr );

Thanks.

--
"When you will be pleased to dine, Mr. Homles?" asked Mrs Hudson,
the landlady, curiosly.
"At seven thirty, the day after tomorrow." said he invovled in his work.
Nov 14 '05 #1
18 6433

"Vijay Kumar R Zanvar" <vi*****@hotpop .com> wrote in message
news:bv******** ****@ID-203837.news.uni-berlin.de...
Hi c.l.c,

Is this a correct method of allocating a 3-dimensional
array?

/* assume all malloc's succeed */
#define MAT 2
#define ROW 2
#define COL 2

char ***ptr = malloc ( MAT * sizeof *ptr );

for ( i = 0; i < MAT; i++ )
ptr[i] = malloc ( ROW * sizeof **ptr );

for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof ***ptr );

Thanks.

You define a pointer to a pointer to a pointer to a char. So your first
malloc() should create the space for several 'pointer to a pointer to a
char' and make ptr (which is a pointer to a "pointer to a pointer to char"
point to it through the malloc call)

char *** ptr=malloc(MAT * sizeof **ptr);

For each of the entries in this array of pointers to pointers to a char you
now need to point to a 'pointer to a char' type:

for(i=0;i<MAT;i ++)
ptr[i]=malloc(ROW * sizeof *ptr);

So now you have a bunch of ROW pointers to chars in each row of the array
ptr[i] so now you must point these to the 'char' (or the first char in an
array of char)

for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof char );
You can think of it more clearly like this:

char array[MAT][ROW][COL];

char * array[MAT][ROW]

char ** array[MAT]

char *** array;

In practice the sizeof ***ptr and sizeof ** ptr etc will all be the same.
Therefore what you end up with in your original code is a three dimentional
array of the type char * as follows:

char * array[MAT][ROW][COL];

Which might be what you wanted, but all the char * array entries you created
do not yet point anywhere and when you do point them somewhere you will in
fact end up with a 4 dimensional array like this for example:

char array[MAT][ROW][COL][STRLEN+1];

Hope this helps you clarify this...

Sean
Nov 14 '05 #2


Sean Kenwrick wrote:
"Vijay Kumar R Zanvar" <vi*****@hotpop .com> wrote in message
news:bv******** ****@ID-203837.news.uni-berlin.de...
Hi c.l.c,

Is this a correct method of allocating a 3-dimensional
array?

/* assume all malloc's succeed */
#define MAT 2
#define ROW 2
#define COL 2

char ***ptr = malloc ( MAT * sizeof *ptr );

for ( i = 0; i < MAT; i++ )
ptr[i] = malloc ( ROW * sizeof **ptr );

for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof ***ptr );

Thanks.

You define a pointer to a pointer to a pointer to a char. So your first
malloc() should create the space for several 'pointer to a pointer to a
char' and make ptr (which is a pointer to a "pointer to a pointer to char"
point to it through the malloc call)

char *** ptr=malloc(MAT * sizeof **ptr);


Are you sure about that? Isn't sizeof **ptr equivalent to sizeof char*
seeing as ptr is of type char***? Perhaps you are confusing "**ptr" with
"char**"?

I think you should use
char*** ptr = malloc(MAT * sizeof *ptr);
here instead.


For each of the entries in this array of pointers to pointers to a char you
now need to point to a 'pointer to a char' type:

for(i=0;i<MAT;i ++)
ptr[i]=malloc(ROW * sizeof *ptr);
Similarly "*ptr" is not the same as "char*". Use:
ptr[i] = malloc(ROW * sizeof *(ptr[i]));

So now you have a bunch of ROW pointers to chars in each row of the array
ptr[i] so now you must point these to the 'char' (or the first char in an
array of char)

for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof char );
And here:
ptr[i][j] = malloc(COL * sizeof *(ptr[i][j]));


You can think of it more clearly like this:

char array[MAT][ROW][COL];

char * array[MAT][ROW]

char ** array[MAT]

char *** array;

In practice the sizeof ***ptr and sizeof ** ptr etc will all be the same.
Therefore what you end up with in your original code is a three dimentional
array of the type char * as follows:

char * array[MAT][ROW][COL];
I'd have though it was more like char array[MAT][ROW][COL];
The array has three sets of braces so it decays to a triple pointer
right? Which is exactly what the OP started with.


Which might be what you wanted, but all the char * array entries you created
do not yet point anywhere and when you do point them somewhere you will in
fact end up with a 4 dimensional array like this for example:

char array[MAT][ROW][COL][STRLEN+1];

Hope this helps you clarify this...

Sean


Hmmm - I'm a bit confused by all this...

Nov 14 '05 #3
nrk
Vijay Kumar R Zanvar wrote:
Hi c.l.c,

Is this a correct method of allocating a 3-dimensional
array?

/* assume all malloc's succeed */
#define MAT 2
#define ROW 2
#define COL 2

char ***ptr = malloc ( MAT * sizeof *ptr );

for ( i = 0; i < MAT; i++ )
ptr[i] = malloc ( ROW * sizeof **ptr );

for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof ***ptr );

Thanks.


Correct. Note that as someone who's posted here more than once before, you
must know this is answered in the FAQ. Specifically Q6.16:
http://www.eskimo.com/~scs/C-faq/q6.16.html

-nrk.

--
Remove devnull for email
Nov 14 '05 #4
nrk
Sean Kenwrick wrote:

"Vijay Kumar R Zanvar" <vi*****@hotpop .com> wrote in message
news:bv******** ****@ID-203837.news.uni-berlin.de...
Hi c.l.c,

Is this a correct method of allocating a 3-dimensional
array?

/* assume all malloc's succeed */
#define MAT 2
#define ROW 2
#define COL 2

char ***ptr = malloc ( MAT * sizeof *ptr );

for ( i = 0; i < MAT; i++ )
ptr[i] = malloc ( ROW * sizeof **ptr );

for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof ***ptr );

Thanks.
You define a pointer to a pointer to a pointer to a char. So your
first malloc() should create the space for several 'pointer to a pointer
to a
char' and make ptr (which is a pointer to a "pointer to a pointer to
char" point to it through the malloc call)

char *** ptr=malloc(MAT * sizeof **ptr);


Wrong. OP was correct. **ptr has type pointer-to-char, and you need sizeof
pointer-to-pointer-to-char.
For each of the entries in this array of pointers to pointers to a char
you now need to point to a 'pointer to a char' type:

for(i=0;i<MAT;i ++)
ptr[i]=malloc(ROW * sizeof *ptr);

Wrong again. *ptr has type pointer-to-pointer-to-char, and you need sizeof
pointer-to-char.
So now you have a bunch of ROW pointers to chars in each row of the array
ptr[i] so now you must point these to the 'char' (or the first char in an
array of char)

for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof char );

Ok. But I would prefer:
ptr[i][j] = malloc(COL * sizeof ***ptr);

You can think of it more clearly like this:

char array[MAT][ROW][COL];

char * array[MAT][ROW]

char ** array[MAT]

char *** array;

In practice the sizeof ***ptr and sizeof ** ptr etc will all be the same.
Absolute nonsense, particularly given OP's declaration of ptr. Don't bandy
about such dangerous and incorrect advice.
Therefore what you end up with in your original code is a three
dimentional array of the type char * as follows:

char * array[MAT][ROW][COL];

OP wants a two dimensional array of pointer-to-char.
Which might be what you wanted, but all the char * array entries you
created
do not yet point anywhere and when you do point them somewhere you will
in fact end up with a 4 dimensional array like this for example:

char array[MAT][ROW][COL][STRLEN+1];

Wrong again. OP's code is fine.
Hope this helps you clarify this...

No. It certainly doesn't.

-nrk.
Sean


--
Remove devnull for email
Nov 14 '05 #5

"nrk" <ra*********@de vnull.verizon.n et> wrote in message
news:kd******** ******@nwrddc02 .gnilink.net...
Sean Kenwrick wrote:

"Vijay Kumar R Zanvar" <vi*****@hotpop .com> wrote in message
news:bv******** ****@ID-203837.news.uni-berlin.de...
Hi c.l.c,

Is this a correct method of allocating a 3-dimensional
array?

/* assume all malloc's succeed */
#define MAT 2
#define ROW 2
#define COL 2

char ***ptr = malloc ( MAT * sizeof *ptr );

for ( i = 0; i < MAT; i++ )
ptr[i] = malloc ( ROW * sizeof **ptr );

for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof ***ptr );

Thanks.
You define a pointer to a pointer to a pointer to a char. So your
first malloc() should create the space for several 'pointer to a pointer
to a
char' and make ptr (which is a pointer to a "pointer to a pointer to
char" point to it through the malloc call)

char *** ptr=malloc(MAT * sizeof **ptr);


Wrong. OP was correct. **ptr has type pointer-to-char, and you need

sizeof pointer-to-pointer-to-char.
Yeh, I meant to use the TYPE not the object here so it would be:

char *** ptr=malloc(MAT * sizeof (char**));

Hence the two '**' which was wrong when I used them with sizeof **ptr...
For each of the entries in this array of pointers to pointers to a char
you now need to point to a 'pointer to a char' type:

for(i=0;i<MAT;i ++)
ptr[i]=malloc(ROW * sizeof *ptr);

Wrong again. *ptr has type pointer-to-pointer-to-char, and you need

sizeof pointer-to-char.
Again I meant 'char *' not *ptr - very careless...
So now you have a bunch of ROW pointers to chars in each row of the array ptr[i] so now you must point these to the 'char' (or the first char in an array of char)

for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof char );

Ok. But I would prefer:
ptr[i][j] = malloc(COL * sizeof ***ptr);

You can think of it more clearly like this:

char array[MAT][ROW][COL];

char * array[MAT][ROW]

char ** array[MAT]

char *** array;

In practice the sizeof ***ptr and sizeof ** ptr etc will all be the

same.
Absolute nonsense, particularly given OP's declaration of ptr. Don't bandy about such dangerous and incorrect advice.


Again I meant sizeof(char ***) and sizeof(char **) would be the same in
practice ...

I think my brain suffered a temporary blue screen of death...

Sean
Nov 14 '05 #6

On Fri, 30 Jan 2004, Sean Kenwrick wrote:

"nrk" <ra*********@de vnull.verizon.n et> wrote...
Sean Kenwrick wrote:

You define a pointer to a pointer to a pointer to a char. So
your first malloc() should create the space for several 'pointer
to a pointer to a char' and make ptr (which is a pointer to a
"pointer to a pointer to char" point to it through the malloc call)

char *** ptr=malloc(MAT * sizeof **ptr);
Wrong. OP was correct. **ptr has type pointer-to-char, and you need
sizeof pointer-to-pointer-to-char.


Yeh, I meant to use the TYPE not the object here so it would be:

char *** ptr=malloc(MAT * sizeof (char**));

Hence the two '**' which was wrong when I used them with sizeof **ptr...


This is /EXACTLY/ why we here in c.l.c don't try to foist the
ridiculous "sizeof (char **)" nonsense on newbies (or even oldbies,
for that matter). If you stick to "sizeof *foo" for every malloc
call you ever write, you will /never/ commit such a serious blunder,
and the regulars won't have to waste time correcting you.

You can think of it more clearly like this:

char array[MAT][ROW][COL];
char * array[MAT][ROW]
char ** array[MAT]
char *** array;
Maybe *you* can; but an experienced C programmer might look at
that list of declarations and see immediately that they're all of
different types, stored and accessed by different methods; and then
he might accuse you of either oversimplificat ion or ignorance -- and
we wouldn't want that, now would we?

In practice the sizeof ***ptr and sizeof ** ptr etc will all be
the same.


Absolute nonsense, particularly given OP's declaration of ptr. Don't
bandy about such dangerous and incorrect advice.


Again I meant sizeof(char ***) and sizeof(char **) would be the same in
practice ...


Still wrong, as far as C is concerned. (char ***) and (char **) are
not compatible types, nor do they necessarily have the same size.
I think my brain suffered a temporary blue screen of death...


I think you're still working through the stage that comes right after
wide-eyed newbieness, which is 1337|\|355. ("Don't assume that just
because you can kick the TV and make it work, that you are an
uber-TV-repairman.") Keep reading c.l.c, though, and you'll pull
through. ;-)

-Arthur
Nov 14 '05 #7

On Fri, 30 Jan 2004, Vijay Kumar R Zanvar wrote:

Is this a correct method of allocating a 3-dimensional
array?

/* assume all malloc's succeed */
No! Definitely not! ;-D

#define MAT 2
#define ROW 2
#define COL 2

char ***ptr = malloc ( MAT * sizeof *ptr );

for ( i = 0; i < MAT; i++ )
ptr[i] = malloc ( ROW * sizeof **ptr );
I would stick to the c.l.c idiom here, and write

ptr[i] = malloc(ROW * sizeof *ptr[i]);

just to avoid any lingering doubts about how many asterisks
are supposed to go there.
for ( i = 0; i < MAT; i++ )
for ( j = 0; j < ROW; j++ )
ptr[i][j] = malloc ( COL * sizeof ***ptr );


Ditto on this malloc. Except that in practice, I'd fold
these two loops into one, making the finished product look
like this:

char ***new_array_wi thout_error_che cking(int MAT, int ROW, int COL)
{
char ***ptr;
int i, j;

ptr = malloc(MAT * sizeof *ptr);
for (i=0; i < MAT; ++i) {
ptr[i] = malloc(ROW * sizeof *ptr[i]);
for (j=0; j < ROW; ++j)
ptr[i][j] = malloc(COL * sizeof *ptr[i][j]);
}

return ptr;
}
Incidentally, I see that the FAQ (q6.16) uses malloc casting,
which it actually POINTS OUT reaches a point where "the syntax
starts getting horrific," so I don't know why Steve Summit added
it in the first place! :-(

HTH,
-Arthur
Nov 14 '05 #8
Arthur J. O'Dwyer wrote:
Incidentally, I see that the FAQ (q6.16) uses malloc casting,
which it actually POINTS OUT reaches a point
where "the syntax starts getting horrific," so
I don't know why Steve Summit added it in the first place! :-(


Snicker, snicker. Chortle, chortle. Gloat, gloat.

Nov 14 '05 #9

"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote in message
news:Pi******** *************** ***********@uni x43.andrew.cmu. edu...

On Fri, 30 Jan 2004, Sean Kenwrick wrote:

"nrk" <ra*********@de vnull.verizon.n et> wrote...
Sean Kenwrick wrote:
>
> You define a pointer to a pointer to a pointer to a char. So
> your first malloc() should create the space for several 'pointer
> to a pointer to a char' and make ptr (which is a pointer to a
> "pointer to a pointer to char" point to it through the malloc call)
>
> char *** ptr=malloc(MAT * sizeof **ptr);

Wrong. OP was correct. **ptr has type pointer-to-char, and you need
sizeof pointer-to-pointer-to-char.


Yeh, I meant to use the TYPE not the object here so it would be:

char *** ptr=malloc(MAT * sizeof (char**));

Hence the two '**' which was wrong when I used them with sizeof **ptr...


This is /EXACTLY/ why we here in c.l.c don't try to foist the
ridiculous "sizeof (char **)" nonsense on newbies (or even oldbies,
for that matter). If you stick to "sizeof *foo" for every malloc
call you ever write, you will /never/ commit such a serious blunder,
and the regulars won't have to waste time correcting you.

This reason I got into trouble here is /EXACTLY/ because I used sizeof
(*foo) instead to sizeof(type). When you see a statement like sizeof
***foo or sizeof **foo you cannot immediately descern from the code what is
going on without hunting back to track down the declaration of foo then
figuring out it's indirection so that you can know what ***foo is refering
to (is it the object, a pointer to the object or a pointer to a pointer ot
the object??? (horrible!)). Whereas with sizeof (char**) is immediately
clear what it is refering to.

The only advantage I can see of using sizeof *foo is that it protects you
from the case where you might change the fundamental type of foo in the
future, but this is a very rare occurance for something that destroys the
readability of your code...

I'm sure you will enlighten me as to othjer reasons why you prefer sizeof
*foo here in c.l.c and I might yet be converted if you present a good enough
case :-)

Sean
Nov 14 '05 #10

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

Similar topics

8
3397
by: fortuneteller | last post by:
Hello, I'm quite new to python and Scipy. Anyway I want to use it to plot graphs. Does anybody know if there is the possibility to use Latex in SciPy's plotting functions like gplt? Thanks for your help, Matthias
136
5481
by: Merrill & Michele | last post by:
A derangement is a mapping of a set onto itself leaving no element fixed. I realized that that was what I was programming when I was asked to randomly determine who buys presents for whom this year in my wife's family. Obviously, one does not buy for himself. The code is followed by some questions. #include <stdio.h> #include <stdlib.h> #include <time.h>
2
1571
by: Rick D. | last post by:
Hi all, I'm looking for information on running a c# application on a webpage, just like a java-applet. And the second thing i'm looking for is information on how to display 3d graphics with c#. And if there are 3d model importers available for it (like milkshape ms3d or something similar). I'm not even sure if all this can be done, but if it can, i would like
12
16407
by: Dr. Zharkov | last post by:
Hello. Inform, please, on what site it is possible to find materials on construction of the three-dimensional graphic of function z=f(x,y) with the help of Visual Basic .NET and GDI+? Beforehand many thanks for your answer. Best regards, Dr. V.A. Zharkov. Moscow, Russia.
10
4493
by: FreeStyler | last post by:
I was thinking about lerning web application through the creating the site which will enable the users to upload, download and viewing the 3D models created in 3DStudio, Maya etc. What do you think about idea ? Does make sense ? Does something like that exist? Will viewing (zooming, rotating...) works in web environment? I find with google only 3D viewer applets, but no signs of web application, or I google with the wrong words ?
12
3591
by: Xah Lee | last post by:
Of Interest: Introduction to 3D Graphics Programing http://xahlee.org/3d/index.html Currently, this introduction introduces you to the graphics format of Mathematica, and two Java Applet utilities that allows you to view them with live rotation in a web browser. Also, it includes a introductory tutorial to POV-Ray.
2
3171
by: OpenPavilion | last post by:
Hello, did anyone succeed in combining wxpython and a 3d engine (pyogre, crystalblend, panda3d, soya etc.) ? I would like to create an application, which uses wxpython tree, menu and grid elements and embedds a 3d view of some of the listed objects in an own detail window, so showing the object as a 3d model. I know there is PyOpenGL as well, but I need to load complete models (meshes done with cinema4d or blender) into the 3d view.
4
2454
by: Peter Webb | last post by:
I am writing some visualisation code for 4 dimensional geometric shapes. This is to run in the C# .NET XNA environment, so I am cross posting a maths and C# group. My software runs just fine for 3D objects - for example I can draw cubes, tetrahedrons, icosahedrons etc and rotate them on screen. All of the "heavy lifting" is done by the XNA libraries, which have transformation libraries to map 3D constructs onto 2D with perspective. Using...
4
5234
by: Jon Harrop | last post by:
I am writing a 3D graphing component built upon WPF and would like to have 2D vector graphics (e.g. typeset mathematics) as labels laid out from 3D coordinates. For example, a tick on an axis has a 3D coordinate and its 2D label might be right-aligned to the 2D projection of that 3D point. The following Mathematica plot illustrates the functionality I am after: http://math.arizona.edu/~goriely/M322/Mathma-ComplexFunc.jpg I have done...
0
8689
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
9178
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
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
8916
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
7752
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5875
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
4631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2348
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.