Re: Passing variable-dimension variable-type dynamic arrays
From: Larry I Smith (larryXiXsmith_at_verizon.net)
Date: 08/17/04
- Next message: Jamie Vicary: "Re: Passing variable-dimension variable-type dynamic arrays"
- Previous message: Jamie Vicary: "Re: Passing variable-dimension variable-type dynamic arrays"
- In reply to: Jamie Vicary: "Re: Passing variable-dimension variable-type dynamic arrays"
- Next in thread: Jamie Vicary: "Re: Passing variable-dimension variable-type dynamic arrays"
- Reply: Jamie Vicary: "Re: Passing variable-dimension variable-type dynamic arrays"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Jamie Vicary: "Re: Passing variable-dimension variable-type dynamic arrays"
- Previous message: Jamie Vicary: "Re: Passing variable-dimension variable-type dynamic arrays"
- In reply to: Jamie Vicary: "Re: Passing variable-dimension variable-type dynamic arrays"
- Next in thread: Jamie Vicary: "Re: Passing variable-dimension variable-type dynamic arrays"
- Reply: Jamie Vicary: "Re: Passing variable-dimension variable-type dynamic arrays"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|