Ask HN: C programmers, do you prefer to initialize in place or allocate?
An API for a data structure in C could be designed in three ways. It could allocate memory by itself and return a pointer, like so:<p><pre><code>  typedef struct {
      char *name;
      int *adj_matrix;
      ...
  } MyGraph;

  void my_graph_create(const char *name, int n_vertices) {
      MyGraph *result = malloc(sizeof(*result));
      result-&gt;name = strdup(name);
      result-&gt;adj_matrix = calloc(...);
      ...
      return result;
  }
</code></pre>
it could initialize the struct in place, but allocate memory for the dynamically-sized parts:<p><pre><code>  void my_graph_init(MyGraph *result, const char *name, int n_vertices) {
      result-&gt;name = strdup(name);
      result-&gt;adj_matrix = calloc(...);
      ...
  }
</code></pre>
or it could make the user responsible for all memory allocation:<p><pre><code>  size_t my_graph_adj_matrix_size_needed(int n_vertices);

  void my_graph_init(MyGraph *result, char *name, int *adj_matrix) {
      result-&gt;name = name;
      result-&gt;adj_matrix = adj_matrix;
      ...
  }
</code></pre>
Which style do you prefer? Can you explain what makes you prefer it? Is it different between external APIs that you&#x27;ll export from a library and internal APIs that will only be used by other parts of your code?