Vec_new always allocates storage, vec_validate ensures that an existing 
allocation is at least a certain size, or create a new one if the pointer is 
NULL. The latter is typically used when the storage will be used as an array 
and you want to make sure it's large enough to store element N.

See 
https://docs.fd.io/vpp/18.07/db/d65/vec_8h.html#a003a343880e8218b2398b72ae16c5163
 and 
https://docs.fd.io/vpp/18.07/db/d65/vec_8h.html#a3b2e25abfbea7eea806d37920ba769e3.

The other subtle difference is that you need to tell vec_new the type of the 
elements in the array (so it knows the size) whereas vec_validate can glean 
that from the type of the vector (possibly NULL) you pass in.

Chris.

From: vpp-dev@lists.fd.io <vpp-dev@lists.fd.io> On Behalf Of Bly, Mike
Sent: Thursday, April 12, 2018 15:30
To: vpp-dev@lists.fd.io
Subject: [vpp-dev] vec_new() vs. vec_validate()

Hello,

After some digging, near as I can tell, for a new pointer to a new entity, it 
would seem that vec_new() is pretty much providing the same results as 
vec_validate(), albeit, with one less memset(bob, o, sizeof(*bob)) being 
performed. However, I see a 10:1 usage (preference?) of vec_validate() vs. 
vec_new() in the existing vpp src, so I was hoping someone could share some of 
the "why" here as to when one should use either.

-Mike

Reply via email to