[Freeciv-Dev] Re: very simple specvecs
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
On Sat, 15 May 2004, Jason Dorje Short wrote:
> Vasco Alexandre Da Silva Costa wrote:
>
> > Just malloc a chunk and store the size before the beggining of the array.
> > Then return a pointer to the beggining of the array. To know the size,
> > access the memory before the pointer to the beggining. That should do what
> > you want.
>
> But specvecs aren't malloced. E.g. we have
>
> foo_vector foo;
>
> foo_vector_init(&foo);
>
> rather than
>
> foo_vector foo = foo_vector_new();
typedef foo * foo_vector;
void foo_vector_init(foo_vector *tthing)
{
int *ptr;
/* vector size. */
ptr = malloc(sizeof(int));
*ptr = 0;
/* advance pointer. */
ptr++;
*tthing = (foo_vector)ptr;
}
int foo_vector_size(foo_vector *tthing)
{
int *ptr;
ptr = (int *)*tthing;
ptr--;
return *ptr;
}
---
Vasco Alexandre da Silva Costa @ Instituto Superior Tecnico, Lisboa
|
|