Re: two dimensional arrays:
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Sat, 30 Apr 2005 09:57:05 -0700
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>>
.
- References:
- two dimensional arrays:
- From: Hamish
- Re: two dimensional arrays:
- From: Axter
- two dimensional arrays:
- Prev by Date: Re: a few doubts!
- Next by Date: Re: two dimensional arrays:
- Previous by thread: Re: two dimensional arrays:
- Next by thread: Re: two dimensional arrays:
- Index(es):
Relevant Pages
|