std::vector::resize without inserting default elements

From cppref we have: (https://en.cppreference.com/w/cpp/container/vector/resize )
(talking about std::vector::resize)
If the current size is less than count
1) additional default-inserted elements are appended

What if I want to resize for free? (aka just keep whatever trash maybe in the at that memory location at that time)

if i have an empty contractor does it simply just use whatever trash may be in mem at the time (effectively contracting for free)
EG:
1
2
3
4
5
6
7
8
9
10
#include <vector>
class obj{
    int x,z,y;
    public:
    obj(){}
};
int main(){
	std::vector<obj> x;
	x.resize(4);
}


what if I do not want to use my default constructor for that

can use the overloaded version for something like
1
2
3
4
int main(){
	std::vector<obj> x;
	x.resize(4, undefined_data);
}


Thanks!
Last edited on
Reread my post and its not too coherent :)
Trying again:
as far as I can tell on my compilers an empty constructor that does nothing is optimized out. You can't FORCE resize not to call it, and in debug mode, it probably does, but optimized it seems to go away because it does nothing.

I was able to prove it by changing your resize to a large number and printing the values in x,y,z and some are nonzero junk.
Last edited on
 
x.resize(4, value);


If x is resized upwards, then additional copies of value are appended.
What if ...


You do resize when you want certain number of valid objects*. Each valid object has a value.
You do reserve when you just want to preallocate uninitialized memory for future objects.

1
2
3
4
obj z;
std::vector<obj> x( 42, z );
std::vector<obj> y;
y.resize( 42, z );

Both x and y have 42 valid objects. Value of each object is copied from z.

You can naturally create an unnamed temporary object on the spot:
y.resize( 7, obj() );

When you use the one-parameter form y.resize( 7 );, then it does call the default constructor.


*What does a constructor do? What does implicitly generated default constructor do to members? That is unrelated to vector::resize().
Last edited on
@keskivrto

1)
you appear to be correct on that the default default constractor just uses whatever is the mem at the lite of construction
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <vector>
#include <iostream>

class obj{
    public:
    int x,z,y;
    obj(){}
};

int main(){
    obj k;
    std::cout << k.x << '_'<< k.y << '_' << k.z << std::endl;
    k.x = 1; k.y=2; k.z=3;
    std::cout << k.x << '_'<< k.y << '_' << k.z << std::endl;

    obj* kp = new(&k) obj();
    std::cout << k.x << '_'<< k.y << '_' << k.z << std::endl;
}

which appears to be verfied by https://en.cppreference.com/w/cpp/language/default_constructor (section 6)

though what if it is not

2)
You do resize when you want certain number of valid objects*. Each valid object has a value.

what if i want to move the end pointer (the pointer pointing to .back()) without doing any thing else
(without caring if the underling object is valid)

3)
You do reserve when you just want to preallocate uninitialized memory for future objects.

i have already reserved the space i need
Note, for:
1
2
3
4
class obj{
public:
    int x, z, y;
};

All these are true:
[*] The constructor is not user-provided (i.e., is implicitly-defined or defaulted on its first declaration)
[*] has no virtual member functions
[*] has no virtual base classes
[*] has no non-static members with default initializers.
[*] has no direct base
[*] Every non-static member of class type (or array thereof) has a trivial default constructor
Hence the (implicitly-defined) constructor is trivial and does no actions.

1
2
3
4
5
class obj{
public:
    int x, z, y;
    obj() = default;
};

Although defaulted, it is still trivial.

1
2
3
4
5
class obj{
public:
    int x, z, y;
    obj() {}
};

Explicit constructor. The x, z, and y are default initialized, i.e. are initialized to indeterminate values.
In other words, the constructor does no actions.
https://en.cppreference.com/w/cpp/language/default_initialization

In other words, the resize(bigger_number) does not really do anything for/with the added elements.
Last edited on
Registered users can post here. Sign in or register to post.