From: Alan W. I. <ir...@be...> - 2017-09-16 18:45:40
|
Hi Phil: I am not quite sure how I could absolutely prove yesterday's tentative hypothesis that statically allocated 2D arrays reference a single block of memory with no storage of pointers to each of the row vectors because gdb and C compilers make certain hidden assumptions that only those with complete knowledge of the C standards could figure out. But having slept on it, I do think that tentative hypothesis is correct. Therefore on that assumption here is a revision of the C example which demonstrates how easy it is to make an Iliffe vector (which does store those pointers) from a static 2D array. ____________________________ // C programme to test casts of a statically allocated array #include<stdio.h> void main(void) { int zStatic[2][3]; int* zCast = (int*)zStatic; int (*zCast2)[3] = zStatic; int * zIliffe[2]; zStatic[0][0] = 0; zStatic[0][1] = 1; zStatic[0][2] = 2; zStatic[1][0] = 3; zStatic[1][1] = 4; zStatic[1][2] = 5; zIliffe[0] = zStatic[0]; zIliffe[1] = zStatic[1]; printf("%d, %d, %d, %d, %d, %d\n", zStatic[0][0], zStatic[0][1], zStatic[0][2], zStatic[1][0], zStatic[1][1], zStatic[1][2]); printf("%d, %d, %d, %d, %d, %d\n", zCast[0], zCast[1], zCast[2], zCast[3], zCast[4], zCast[5]); printf("%d, %d, %d, %d, %d, %d\n", zCast2[0][0], zCast2[0][1], zCast2[0][2], zCast2[1][0], zCast2[1][1], zCast2[1][2]); printf("%d, %d, %d, %d, %d, %d\n", zIliffe[0][0], zIliffe[0][1], zIliffe[0][2], zIliffe[1][0], zIliffe[1][1], zIliffe[1][2]); } ____________________________ The (perfect) valgrind results are irwin@raven> gcc -g test_2d_static.c irwin@raven> valgrind ./a.out ==18848== Memcheck, a memory error detector ==18848== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==18848== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info ==18848== Command: ./a.out ==18848== 0, 1, 2, 3, 4, 5 0, 1, 2, 3, 4, 5 0, 1, 2, 3, 4, 5 0, 1, 2, 3, 4, 5 ==18848== ==18848== HEAP SUMMARY: ==18848== in use at exit: 0 bytes in 0 blocks ==18848== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==18848== ==18848== All heap blocks were freed -- no leaks are possible ==18848== ==18848== For counts of detected and suppressed errors, rerun with: -v ==18848== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) That conversion from static 2D array to Iliffe vector is so straightforward, I plan to allow all our C (and C++) users to call plshade, plshades, plcont, plimage, or plvect with an Iliffe vector that is conveniently determined (like above) with one call to an extremely simple routine (still to be implemented, but let's tentatively call it plStatic2dGrid in analogy with plAlloc2dGrid) that generates that Iliffe vector from a corresponding statically allocated z array. And also following the plAlloc2dGrid analogy, wrap that routine as Static2dGrid for the c++ binding, but do not propagate it to any other binding. Then follow up by using plStatic2dGrid in examples/c/x15c and Static2dGrid in examples/c++/x15.cc and also by documenting these 2D array nuances for C, C++, and the rest of our different bindings. Development of (pl)Static2dGrid, of course, means there is little purpose to plshade1 anymore even as a demonstration routine. Therefore, I have changed my plans for plshade1 back from converting it to a C-only demonstration routine to deprecating it (and eventually eliminating it) again. 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 __________________________ |