Hi Alan
I don't have the plplot code here right now, so can't comment on plshade(1).
But i can tell you about 2d arrays.
Statically and dynamically allocated 2d arrays are very different in memory so
you cannot do
double z[10][10];
double **z1=(double**)z;
Or anything else that directly casts z to be a double**.
A static 2d array is actually a pointer to a block of memory and the compiler
knows that this block of memory represents a 2d array and how to find each
element in that memory – presumably one row followed by the next, then the next.
A dynamic 2d array is actually multiple separate vectors as you described. One
vector for each row of the array – they do not need to be contiguous in memory.
Then there is one vector (the double **) where each element points to the
beginning of each row. This is literally an vector of pointers.
So to summarise, a double** dynamic array is a vector of pointers to each row
and a double[][] static array is actually a single vector that the compiler
knows to interpret as a 2d array. So it should be clear now why casting between
the two cannot be done.
You could create a double ** from a static array something like
double z[10][10];
double z1 = malloc(10*sizeof(double*));
for( int i=0; i<10; ++i)
{
z1[i] = &z[i][0];
}
Note that it is often regarded as faster to use static arrays or some method to
treat a single block of memory as a 2d array, because adding the appropriate
offset (row number * stride + column number) to the beginning of the block then
dereferencing once is supposed to be faster than dereferencing twice. I've
never tested this though, so i don't know if this is still true with modern
compilers and hardware.
Phil
Sent from my Windows 10 phone
From: Alan W. Irwin
Sent: 14 September 2017 18:40
To: PLplot development list
Subject: [Plplot-devel] Can statically allocated z arrays be used as the
zargument for plshade (and plshades, plcont, plimage, and plvect)?
This general question has come up because plshade1 only differs from
plshade in how the z 2D matrix argument is typed and handled inside
the routine, but there is no similar plshades1, plcont1, plimage1, and
plvect1 equivalents to plshades, plcont, plimage, and plvect. So for
consistency with the rest, I would like to deprecate plshade1 and
encourage the use of plshade instead. But part of that deprecation is
explaining to users how to use plshade, but I need help with that.
I could recommend to users to call plshade with a z argument that
follows how the z 2D matrix argument of plcont is typed and defined.
For example, in example 15 we currently use the statically defined z
matrix
PLFLT z[35][45];
z[i][j] = ....;
plshade1( &z[0][0], ...);
And I could replace that logic with logic where z is
defined as in example 9 with plcont, i.e., as a 2D Iliffe vector
(a vector of pointers to vectors of data, see
<https://en.wikipedia.org/wiki/Iliffe_vector>). So in this
case examples/c/x15c.c would look like the following:
PLFLT **z;
plAlloc2dGrid( &z, 35, 45);
z[i][j] = ....;
plshade( (const PLFLT * const *) z, ...);
plFree2dGrid( z, 35, 45);
Because of the similarity with how the z argument of plcont is
handled, I am positive the above method would work, but I haven't
implemented that yet because I think there _might_ be a simpler
alternative where we use a combination of
PLFLT z[35][45];
z[i][j] = ....;
and a call to
plshade( (const PLFLT * const *) z, ...);
The assumption here is that this statically allocated z is a special case
of an Iliffe vector and should "just work". But perhaps that assumption
is wrong?
The reason I am uncertain about that assumption, is I have already
tried the above combination of statically allocated z array and that
call to plshade, and it compiles without any warnings which is
encouraging. However, the result segfaults at run time. So the
question is whether there is something extra I have to do to get the
above combination to work (and similarly for plshades, plcont,
plimage, and plvect) or do users _always_ have to call those routines
with z dynamically allocated with plAlloc2dGrid?
Help with this question from the C experts here would be much appreciated.
Alan
__________________________
Alan W. Irwin
Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).
Programming affiliations with the FreeEOS equation-of-state
implementation for stellar interiors (freeeos.sf.net); the Time
Ephemerides project (timeephem.sf.net); PLplot scientific plotting
software package (plplot.sf.net); the libLASi project
(unifont.org/lasi); the Loads of Linux Links project (loll.sf.net);
and the Linux Brochure Project (lbproject.sf.net).
__________________________
Linux-powered Science
__________________________
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Plplot-devel mailing list
Plplot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/plplot-devel
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Plplot-devel mailing list
Plplot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/plplot-devel