[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [d-i] libdebian-installer2

[Junichi Uekawa]
> You can add members at the end of struct if and only if any other
> application does not try to malloc it.

Both malloc and static variables will be a problem.  Examples of bad

  /* library */
  typedef struct { int a; } foo;
  void use_foo(foo *f) { ... }

  /* client code */
  int bar() { foo b; use_foo(&b); }
  int baz() { foo *b = malloc(sizeof(*b)); use_foo(b); }

Example of good interface:

  /* library */
  struct f { int a; };
  typedef struct f *foo;
  foo foo_alloc() { return malloc(sizeof(*foo)); }
  void use_foo(foo f) { ... }

  /* client code */
  int bar() { foo b = foo_alloc(); use_foo(b); }

The good API can be made harder to misuse by moving the struct f
definition into a private header file, not exposed to the clients.

Reply to: