Im using opensource library called wxFreeChart to draw some XY charts. In example there is code which uses static array as a serie :
double data1[][2] = {
{ 10, 20, },
{ 13, 16, },
{ 7, 30, },
{ 15, 34, },
{ 25, 4, },
};
dataset->AddSerie((double *) data1, WXSIZEOF(dynamicArray));
WXSIZEOF ismacro defined like: sizeof(array)/sizeof(array[0])
In this case everything works great but in my program Im using dynamic arrays (according to users input).
I made a test and wrotecode like below:
double **dynamicArray = NULL;
dynamicArray = new double *[5] ;
for( int i = 0 ; i < 5 ; i++ )
dynamicArray[i] = new double[2];
dynamicArray [0][0] = 10;
dynamicArray [0][1] = 20;
dynamicArray [1][0] = 13;
dynamicArray [1][1] = 16;
dynamicArray [2][0] = 7;
dynamicArray [2][1] = 30;
dynamicArray [3][0] = 15;
dynamicArray [3][1] = 34;
dynamicArray [4][0] = 25;
dynamicArray [4][1] = 4;
dataset->AddSerie((double *) *dynamicArray, WXSIZEOF(dynamicArray));
But it doesnt work correctly. I mean point arent drawn. I wonder if there is any possibility that I can “cheat” that method and give it dynamic array in way it understands it and will read data from correct place
thanks for help
If You define an array like
All cells occupy a continuous chunk of memory and I’m pretty sure
dataset->AddSerierelies on that.You can’t guarantee that if you allocate memory in chunks, using consecutive calls to new.
My proposition is to write a simple class that allocates a continuous chunk of memory for storage and uses operator() to access that memory as a two dimensional array using 2 indices. Internally You can use a
vector<double>to manage the storage, and You can pass the address of the first element of that vector todataset->AddSeriePlease check the code in this C++ FAQ example and try to understand it. The matrix example uses new[] and delete[]. You should use a vector instead, and the type
doubleinstead ofFredWhere in the example, there is a private section like this
(The example shows a matrix of
Freds) You should use avector<double>That will make the code much simpler. You don’t even need a destructor, because the vector manages the memory.