[EDIT]
Hello James
I rewrote the CreateArr function to avoid the use of variable number of arguments, but I retained the original algorithm for array allocation. As I already explained in my previous posts (see above) The code reserves 2 additional elements at the end of the array as a kind of marker.
Now if you look in the code you will see (my comment)
void *CreateArr(void *a, int elem_size, int update, int num_dims, size_t *args)
{
size_t s, s1, s2;
void **vp;
size_t *marker;
marker = args;
s = *marker++;
s2 = s + 2; //Frankie: It always allocates 2 elements more than requested
if (num_dims == 1)
{
if (update && a)
a = realloc(a, s2 * elem_size);
else
a = calloc(s2, elem_size);
return a;
}
else if (update && a)
{
s1 = 0;
.......
If you replace the line with the following:
s2 = s;
You'll get the correct answer. But... it will broke the multidimensional code!
Moreover you cannot use _msize to get the number of elements, because the dynamically created array is not a standard one when dimensions>1. I.e. following code:
void DoIt (void)
{
AFX_ANCHORPROPERTY *skAnchor = 0;
{
size_t dimensions[2] = {5, 5};
skAnchor = (AFX_ANCHORPROPERTY*)CreateArr (skAnchor, sizeof(AFX_ANCHORPROPERTY), 0, 2, dimensions);
}
printf("%s%.15G\n", "UBOUND-> ", (double)( _msize ( skAnchor) / sizeof ( AFX_ANCHORPROPERTY)));
if (skAnchor) {
DestroyArr((void **)skAnchor, 1, 1);
skAnchor = NULL;
}
}
Will print 1 instead of 10. This is because multidimensional arrays are built as array of pointers to array of values. In the current case you'll get an array of 2 pointers to 2 different monodimensional arrays each holding 5 values. You get 1 because the dimension of the bidimensional pointers array divided by the size of the structure is 1.