Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2004:
[Freeciv-Dev] Re: very simple specvecs
Home

[Freeciv-Dev] Re: very simple specvecs

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Jason Dorje Short <jdorje@xxxxxxxxxxxx>
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: very simple specvecs
From: Vasco Alexandre Da Silva Costa <vasc@xxxxxxxxxxxxxx>
Date: Sat, 15 May 2004 17:53:28 +0100 (WET DST)

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



[Prev in Thread] Current Thread [Next in Thread]