Asked  7 Months ago    Answers:  5   Viewed   39 times

I am pre-allocating some memory to my a vector member variable. Below code is minimal part

class A {
  vector<string> t_Names;
  A () : t_Names(1000) {}

Now at some point of time, if the t_Names.size() equals 1000. I am intending to increase the size by 100. Then if it reaches 1100, again increase by 100 and so on.

My question is, what to choose between vector::resize() and vector::reserve(). Is there any better choice in this kind of scenario ?

Edit: I have sort of precise estimate for the t_Names. I estimate it to be around 700 to 800. However in certain (seldom) situations, it can grow more than 1000.



The two functions do vastly different things!

The resize() method (and passing argument to constructor is equivalent to that) will insert or delete appropriate number of elements to the vector to make it given size (it has optional second argument to specify their value). It will affect the size(), iteration will go over all those elements, push_back will insert after them and you can directly access them using the operator[].

The reserve() method only allocates memory, but leaves it uninitialized. It only affects capacity(), but size() will be unchanged. There is no value for the objects, because nothing is added to the vector. If you then insert the elements, no reallocation will happen, because it was done in advance, but that's the only effect.

So it depends on what you want. If you want an array of 1000 default items, use resize(). If you want an array to which you expect to insert 1000 items and want to avoid a couple of allocations, use reserve().

EDIT: Blastfurnace's comment made me read the question again and realize, that in your case the correct answer is don't preallocate manually. Just keep inserting the elements at the end as you need. The vector will automatically reallocate as needed and will do it more efficiently than the manual way mentioned. The only case where reserve() makes sense is when you have reasonably precise estimate of the total size you'll need easily available in advance.

EDIT2: Ad question edit: If you have initial estimate, then reserve() that estimate. If it turns out to be not enough, just let the vector do it's thing.

Tuesday, June 1, 2021
answered 7 Months ago

As it is, both dimensions of your vector are 0.

Instead, initialize the vector as this:

vector<vector<int> > matrix(RR);
for ( int i = 0 ; i < RR ; i++ )

This will give you a matrix of dimensions RR * CC with all elements set to 0.

Tuesday, June 1, 2021
answered 6 Months ago

From another answer on SO (paraphrased here):

Using a question mark in front of an ID means that you want to access a style attribute that's defined in a style theme, rather than hardcoding the attribute.

Think of it as dereferencing a theme attribute to fetch the resource it points to rather than referring to the attribute itself.

Referencing Style Attributes

Monday, August 9, 2021
answered 4 Months ago

If you create android project using Android Studio. You can find three directories like following. (each directory called Source Set)

  • app/src/main
  • app/src/androidTest : for android UI test. It needs virtual or real device. (Espresso, UI automator)
  • app/src/test : for android independent test.

The androidTest directory contains test cases that touch the screen or check what is displayed on the screen. In the test directory, we mainly do unit tests such as testing one function.

But test directory is not only for Unit test. You can also write Integration test like HTTP call. Even you can UI test in test directory using Robolectric library.(It's really fast rather than Espresso)

So what is testImplementation and androidTestImplementation? (each called Configuration)

  • testImplementation : adds dependency for test source set
  • androidTestImplementation : adds dependency for androidTest source set

See this articles for details.

Monday, September 6, 2021
answered 3 Months ago

Thanks to @PaulR and @PeterCordes. Unrolling the loop by a factor of 4 works.

void apply_mask(vector<uint8_t> &payload, const uint8_t (&masking_key)[4]) {
  const size_t size = payload.size();
  const size_t size4 = size / 4;
  size_t i = 0;
  uint8_t *p = &payload[0];
  uint32_t *p32 = reinterpret_cast<uint32_t *>(p);
  const uint32_t m = *reinterpret_cast<const uint32_t *>(&masking_key[0]);

#pragma clang loop vectorize(enable) interleave(enable)
  for (i = 0; i < size4; i++) {
    p32[i] = p32[i] ^ m;

  for (i = (size4*4); i < size; i++) {
    p[i] = p[i] ^ masking_key[i % 4];

gcc.godbolt code

Sunday, October 31, 2021
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :