As noted by Billy, vec_validate would call vec_resize and then call memset() to clear unused area to 0. This call is required to make sure, if vector is expanded by vec_resize without having to allocate new memory, any unused memory with stale content is cleared.
For new vectors, calling vect_new should be faster as vec_resize would call vec_reszie_allocate_memory which would allocated memory and call memset() to clear it. Thus, calling vec_validate() to create a new vector would end up calling memset() twice to clear allocated vector memory. Regards, John From: [email protected] <[email protected]> On Behalf Of Chris Luke Sent: Thursday, April 12, 2018 4:01 PM To: Bly, Mike <[email protected]>; [email protected] Subject: Re: [vpp-dev] vec_new() vs. vec_validate() 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: [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>> On Behalf Of Bly, Mike Sent: Thursday, April 12, 2018 15:30 To: [email protected]<mailto:[email protected]> 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
