|
From: Daniel J S. <dan...@ie...> - 2007-06-30 19:30:10
|
I'm using a dynarray for the "quick refresh record data etc." patch (more on this shortly) but I just wanted to point out a picky technical issue. That is, it seems it isn't possible to initialize the dynarray to size zero. It'd nice if one could. It's not of great importance, but for such a fundamental utility "consistency" (lack of term) would be nice.
If I do
init_dynarray(foo_array, sizeof(foo), 0, 200);
and then later
nextfrom_dynarray(foo_array);
I will get an execution error of
"world.dem", line 13: nextfrom_dynarray: dynarray wasn't initialized!
>From the programmer's perspective, I have to look to the code to know that I must initialize to some size greater than zero. In the init_dynarray code is
this->v = 0; /* preset value, in case gp_alloc fails */
if (size)
this->v = gp_alloc(entry_size*size, "init dynarray");
Maybe it should be
if (size <= 0)
graph_error("foo");
this->v = ...
So the programmer knows to initialize to something greater than zero. But actually, I might argue for just dropping the error messages
if (!this->v)
graph_error("resize_dynarray: dynarray wasn't initialized!");
It will save a test every time nextfrom_dynarray() is called (which is fairly considerable). Plus, let's say init_dynarray *wasn't* called. gp_alloc uses "malloc" which isn't guaranteed to be zero (probably system dependent). gnuplot should be using "calloc" if the above test is to be foolproof.
I'm comfortable with just dropping those error messages (and allow dynarrays to be initialized to 0). If init_dynarray() isn't called properly first the program will fall apart so fast that the programmer will catch on quick.
However, if one wants to keep the above errors, I'd say switch to some variation of calloc and instead of testing on "this-v", test on "this->entry_size" so that a person can initialize the size to 0. (This would probably be the preferred route.)
Dan
|