Re: Passing variable-dimension variable-type dynamic arrays

From: Larry I Smith (larryXiXsmith_at_verizon.net)
Date: 08/17/04


Date: Tue, 17 Aug 2004 15:18:26 GMT

Jamie Vicary wrote:
> Larry I Smith wrote:
>
>> Jamie Vicary wrote:
>>
>>> Dear all,
>>>
>>> I wish to pass variable-dimension dynamically allocated (that is,
>>> pointer-to-pointer) arrays to a function of mine. The best way I can
>>> think of doing this is as follows (it is safe to assume that the
>>> array will either be of size 5, or 5x5, or 5x5x5, and will by of type
>>> int).
>>>
>>> int manipulate_array(int * ptr, int num_dimensions) {
>>> if (1 == num_dimensions) {
>>> /* code doing stuff with ptr[0], ..., ptr[4] */
>>> return 1;
>>> } else if (2 == num_dimensions) {
>>> ptr = (int **) ptr;
>>> /* code doing stuff with ptr[0][0], ..., ptr[4][4] */
>>> return 1;
>>> } else if (3 == num_dimensions) {
>>> ptr = (int ***) ptr;
>>> /* code doing stuff with ptr[0][0][0], ..., ptr[4][4][4] */
>>> return 1;
>>> }
>>> return 0;
>>> }
>>>
>>> Two questions:
>>>
>>> i) Casting pointers like this gives compile-time warnings. It is
>>> actually dangerous in any way?
>>>
>>> ii) I might in future not only know the dimensionality of the
>>> array, but also not know the /type/ of the array. In this case I
>>> might adapt the function above so that its first line reads
>>>
>>> int manipulate_array(void * ptr, int num_dimensions, char *
>>> var_type)
>>>
>>> and then write code like
>>>
>>> if ((strcmp(var_type, "float") == 0) && 3 == num_dimensions) {
>>> ptr = (float ***) ptr;
>>> /* code manipulating ptr[0][0][0], ..., ptr[4][4][4] */
>>> return 1;
>>> } else if ((strcmp(var_type, "unsigned short") == 0) && 1 ==
>>> num_dimensions) {
>>> ptr = (unsigned short *) ptr;
>>> /* code manipulating ptr[0], ..., ptr[4] */
>>> return 1;
>>> }
>>>
>>> (or I could #define FLOAT_TYPE 0, #define UNSIGNED_SHORT_TYPE 1
>>> etc and pass the types as integers, but that's irrelevant.) Is it at
>>> all dangerous casting pointers from type to type and
>>> dereference-level to dereference-level? I can only imagine it might
>>> be dangerous if some breed of pointer had a different sizeof() than
>>> some other type; in that case, it might be possible that in the code
>>>
>>> int **** silly_pointer = my_dynamically_allocated_4D_array;
>>> silly_pointer = (long double *) silly_pointer;
>>> silly_pointer = (int ****) silly_pointer;
>>>
>>> the last two lines do not actually form a null operation. After all -
>>> casting a float to a long and back again would certainly not be a
>>> null operation. Is the same ever true for pointers? I suspect that it
>>> /is/ true, and that's why the (void *) pointer type exists - are my
>>> suspicions correct?
>>>
>>> Regards,
>>>
>>> Jamie Vicary.
>>>
>>
>> Hmm, read about the 'function template' feature of C++.
>> That should solve your problem (using g++) of variable
>> types (int, double, etc).
>>
>> A 'class template' is even more generic, and may aid
>> with the problem of 'how many dimensions?' (1, 2, 3, etc).
>>
>> Perhaps one of the container classes from the C++ Standard
>> Library would be suitable (vector, list, etc).
>>
>> Regards,
>> Larry
>>
>
> Hi Larry,
>
> That's great - except I'm using C! I really should've mentioned
> that... I need the library I'm writing to be C-compatible, so fancy
> things like class templates are unfortunately out of my reach.
>
> Jamie
>

I realized that you're using C.

It's perfectly legal to mix C and C++ code in one app.
All of the C Std Lib functions are also available in C++.

It's your call; I was simply making a suggestion.

Larry

-- 
Anti-spam address, change each 'X' to '.' to reply directly.


Relevant Pages

  • Re: Passing variable-dimension variable-type dynamic arrays
    ... Larry I Smith wrote: ... >> think of doing this is as follows (it is safe to assume that the array ... > A 'class template' is even more generic, ...
    (comp.os.linux.development.apps)
  • Re: Semaphore behaviour
    ... Larry I Smith wrote: ... >> int main{ ... >> I know the order is undefined, but I would expect thread3 to block, and ...
    (comp.os.linux.development.apps)
  • Re: Why does this work? (rot13 function)
    ... >> without warnings, I'd like to hear about it. ... > - Larry ... has the benefit of documenting that the parameter is not used by the ... int bar ...
    (comp.lang.c)
  • Re: Identity columns
    ... You must be thinking of an INT in VBScript. ... In SQL Server, the upper bound ... "Larry" wrote in message ... > If you great an identity column using the statement below, ...
    (microsoft.public.sqlserver.server)