Re: two dimensional arrays:
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Sun, 01 May 2005 00:45:39 -0700
On 30 Apr 2005 18:15:14 -0700, "Axter" <temp@xxxxxxxxx> wrote:
>Barry Schwarz wrote:
>> 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.
>
>Could you please explain that with more details.
>Why do you think there's no garantee that it will work, and please give
>specific reference to support your claim.
Paragraph 6.3.2.3-7 states it will work unless there is an alignment
issue. Since in your code the value is assigned by malloc which
guarantees that it is properly aligned for all types, alignment is not
an issue and I was wrong. It will work.
>
>> 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.
>
>Again, please give specific reference to support this claim.
The only requirements for common size and representations are
contained in 6.2.5-27. There are four such requirements:
pointers to void and pointers to a character type
pointers to qualified and unqualified compatible types
pointers to any type of struct
pointers to any type of union
This paragraph concludes with "Pointers to other types need not have
the same representation or alignment requirements."
>
>>
>> Since your two allocate functions use the same allocation logic, why
>> does one use void** and the other unsigned char**?
>
>If you're referring to the code in the Allocate2DArrayWithValues
>function, that is not part of the code I posted above, and it's not
>really relevent the the poster's question.
>It's a left over function, that didn't get removed.
>The Allocate2DArray function is the function that creates the array,
>and if you notice, both allocation pointers are of type void pointer.
Nope. One is of type pointer to pointer to void and the other is
pointer to void (void** vs void*). These are not the same at all.
>
>> 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?
>Again, second function is not part of the discussion, and doesn't even
>belong there.
>
>>
>> Neither of your allocate functions checks the return from malloc for
>> success.
>>
>
>No. And that's intentional. This is example code, and added error
>checking is left for the developer to add.
>If you like to post a version with the added error check, please feel
>free to do so, but the code I have posted in my site is not for that
>purpose.
<<Remove the del for email>>
.
- Follow-Ups:
- Re: two dimensional arrays:
- From: Axter
- Re: two dimensional arrays:
- References:
- Re: two dimensional arrays:
- From: Axter
- Re: two dimensional arrays:
- Prev by Date: Re: sizeof operator implementation
- 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
|