Re: two dimensional arrays:



On 30 Apr 2005 04:02:54 -0700, "Axter" <temp@xxxxxxxxx> wrote:

>
>Hamish wrote:
>> I'm trying to use an C API which is for geometry calculations. The
>function
>> requires an argument for an array of polygons:
>>
>> coordpt **polygons
>> //[0..i..polygons_num-1][0..polygons_vertex_num[i]-1]
>>
>> where coordpt is:
>>
>> typedef struct
>> { double x;
>> double y;
>> } coordpt;
>>
snip
>
>Check out the following code for building a 2 dimensional array:
>http://code.axter.com/allocate2darray.h
>http://code.axter.com/allocate2darray.c
>
>Using above code, you can create a 2 dimensional array via following
>method:
>int x = 4;
>int y = 6;
>
>coordpt **My_coordpt = ALLOCATE2DARRAY(coordpt, x, y);

Your macro translates as a call to a routine that returns a void** and
then casts that value to the desired type. While this probably works
on most systems, there is no guarantee that a void** is in any way
compatible with coordpt** or that the conversion via the cast will
produce a meaningful value.

Furthermore, the routine allocates a block of void* and returns the
address of this block. Again, while this probably works on most
systems, there is no guarantee that sizeof(void*) is the same as
sizeof(coordpt*) or that the representation of an address in the two
pointer types is the same.

Since your two allocate functions use the same allocation logic, why
does one use void** and the other unsigned char**?

In the second function, you have a useless (and incorrect but
harmless) cast of the second argument to memcpy. Ignoring the const
in memcpy's prototype for a moment, memcpy expects a void*. The
function receives a void* as its argument. Why do you cast it to
unsigned char* just so the compiler will have to convert it back for
you?

Neither of your allocate functions checks the return from malloc for
success.


<<Remove the del for email>>
.



Relevant Pages

  • Re: A little help please
    ... Well I guess it would be if I didn't know I was passing an array of ints ... incrementing the pointer by nBytes I was pointing to next array element.? ... array to a void* using a static cast. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: multi dimensional arrays as one dimension array
    ... Changing the cast to void* only creates more work for the compiler. ... first convert array (which in this context is identical to &array ... If your system has built in hardware assist for bounds checking, ...
    (comp.lang.c)
  • C question
    ... I have experimented with passing an array of POINT structures to a function. ... void f ... reinterpret_cast, ...
    (microsoft.public.vc.language)
  • Re: what is the suggested way to do void * arithmetic ?
    ... DL> I always feel better when I convert void * to char * but that's probably ... DL>because C++ doesn't allow pointer arithmetic on void *'s. ... DL>>> Unless you are referencing them as array elements (in which case, ... DL>>> you should probably cast them to char for the arithmatic, ...
    (freebsd-current)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... You can't do pointer arithmetic on a void* value. ... qsort behaves in a manner consistent with its specification. ... actually works as advertised...but doesn't mean that the cast is ...
    (comp.lang.c)