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.

From: <> On Behalf Of Chris Luke
Sent: Thursday, April 12, 2018 4:01 PM
To: Bly, Mike <>;
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.


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.


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


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.


Reply via email to