Test > Test

Change valist with an array of args

<< < (2/2)

jcfuller:
frankie,
  I'm back.
The attached uses the CreateArr function you developed which I have been using with no apparent issues.
I wanted to determine how big the array was so I used _msize to find the index count of the array. It reports 2 more than I requested?? Compiled as a 64bit app. Same results with VC .

James

frankie:
[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)

--- Code: ---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;
          .......

--- End code ---

If you replace the line with the following:

--- Code: ---    s2 = s;

--- End code ---
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:

--- 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;
    }
}

--- End code ---
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.

jcfuller:
Thank you for your explanation. My old brain is not as good at retaining as it used to be :)
Now, would s2 = s work on single dimension arrays only?

James

frankie:
Yes it should work, but on multidimensional arrays CreateArr will crash, you will not be able anymore to create multidimensional arrays.

Navigation

[0] Message Index

[*] Previous page

Go to full version