Lets take custom vector implementation as an example:
template<typename Object>
class myVector {
public:
explicit myVector(int size = 0) :
_size{ size },
_capasity{ size + SPARE_CAPACITY }
{
_buff = new Object[_capasity];
if (_size > 0) {
for (int i = 0; i < _size; i++) {
//_buff[i] = 0;
}
}
}
// more code
private:
Object * _buff = nullptr;
int _size;
int _capasity;
};
So my question is, how to make myVector be value-initialized in case I'll initialize it as:
int main() {
myVector<int> v02(5);
}
Here, it contains 5 int values, so I need it to be all zeros; same with other types. I commented out _buff[i] = 0; as it's specific to int. Please give me some hints.
It's as simple as
for (int i = 0; i < _size; i++)
_buff[i] = Object{};
Alternatively, you could get rid of the loop and add a pair of {} (or ()) here:
_buff = new Object[_capasity]{};
// ^^
But this option would value-initialize all _capasity objects, rather than the first _size ones, as noted by #bipll.
Also, note that if you want to mimic the behavior of std::vector, you need to allocate raw storate (probably std::aligned_storage) and call constructors (via placement-new) and destructors manually.
If Object is a class type, _buff = new Object[_capasity]; calls default constructors for all _capasity objects, rather than for the first _size objects as std::vector does.
Note that when calling
_buff = new Object[_capasity];
(btw, why have you moved this initialization out of init-list, into constructor body?) you already have default-initialized _capasity objects. Default initialization has the following effects here: while elements of scalar type would remain uninitialized (and reading from them UB), for class types you have already called _capasity constructors.
To avoid unnecessary constructions you have the following possible options, among others:
Use std::aligned_alloc to allocate non-initialized memory:
explicit myVector(std::size_t size = 0) :
size_{ size }
, capacity_{ size + SPARE_CAPACITY }
, buff_{std::aligned_alloc(alignof(Object), _capacity)}
{
if(!buff_) throw std::bad_alloc();
if(size) new (buff_) Object[size]{}; // empty braces answer your original query
}
Remember that again buff_ should be aligned_alloced when vector grows (can be std::realloc()ed for trivial types), and in destructor it should be std::free()d — and prior to that size_ objects inside it should be destructed (with an explicit call to ~Object()).
Change buff_'s type to something more trivial yet properly aligned:
using Storage = std::aligned_storage_t<sizeof(Object), alignof(Object)>;
Storage *buff_;
Object *data_ = nullptr;
public:
explicit myVector(std::size_t size = 0) :
size_{ size }
, capacity_{ size + SPARE_CAPACITY }
, buff_{new Storage(_capacity)}
{
if(size) data_ = new (buff_) Object[size]{};
}
Again, in destructor, objects should be manually destroyed, but this time buff_ can be simply delete[]d afterwards.
Related
This question already has answers here:
How to resize array in C++?
(5 answers)
Closed 4 years ago.
I am sorry if this has already been covered before. I know how to do this is C and Java but not C++. Without using a pre-existing class which includes the use of Vector, how would you increase the size of an array given the code below?
The array expansion and assignment to the array takes place in push() noted with the all caps comment.
EDIT: As I have mentioned in comments below this is a question regarding manually reallocating arrays rather than using std::vector or "Dynamic Arrays."
Line.h
#include <iostream>
#include "Point.h"
using namespace std;
class Line {
public:
Line();
virtual ~Line();
// TAKE IN NEW POINT, INCREASE THE ARRAY SIZE AND ADD NEW POINT TO THE END OF THE ARRAY
void push(const Point& p);
private:
unsigned int index; // size of "points" array
Point* points;
};
Main.cpp
#include <iostream>
#include "Point.h"
#include "Line.h"
using namespace std;
int main() {
int x, y;
int size; // Some user defined size for the array
Line line;
Point a[size]; // Some points that are already filled
// Push the data in a[] to the variable "line"
for(int i = 0; i < size; i++){
// Increase array size of Point* points in variable line and add a[i] to the end of the array
line.push(points[i]);
}
return 0;
}
The simple answer is you should always use std::vector in this case. However it might be useful to explain just why that is. So lets consider how you would implement this without std::vector so you might see just why you would want to use std::vector:
// Naive approach
Line::push(const Point& p)
{
Point* new_points = new Points[index + 1];
std::copy(std::make_move_iterator(points), std::make_move_iterator(points+index), new_points);
new_points[index] = p;
delete[] points;
points = new_points;
index += 1;
}
This approach has many problems. We are forced to reallocate and move the entire array every time an entry is inserted. However a vector will pre-allocate a reserve and use space out of the reserve for each insert, only re-allocating space once the reserve limit is surpassed. This mean vector will far out perform your code in terms of performance as less time will be spent allocating and moving data unnecessarily. Next is the issue of exceptions, this implementation has no exception guarantees, where as the std::vector provides you with a strong exception guarantee: https://en.wikipedia.org/wiki/Exception_safety. Implementing a strong exception guarantee for your class is none trivial, however you would have automatically got this had you implemented this in terms of std::vector as such
Line::push(const Point& p)
{
points.push_back(p);
}
There are also other more subtle problems with your approach, your class does not define copy or assignment operators and so gets compiler generated shallow copy versions generated which means if someone copies your class then allocated members will get deleted twice. To resolve this you need to follow the rule of 3 paradigm pre C++11 and the rule of 5 for C++ 11 onwards: https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming). However had you used a vector none of this would be needed as you would benefit from the rule of zero and be able to rely on the compiler generated defaults: https://blog.rmf.io/cxx11/rule-of-zero
Essentially the only way is to use a dynamic array (one created using new[]) and to create an entirely new dynamic array and copy (or move) the objects from the old array to the new one.
Something like this:
class Line {
public:
Line(): index(0), points(nullptr) {} // initialize
virtual ~Line() { delete[] points; } // Clean up!
void push(const Point& p)
{
// create new array one element larger than before
auto new_points = new Point[index + 1];
// copy old elements to new array (if any)
for(unsigned int p = 0; p < index; ++p)
new_points[p] = points[p];
new_points[index] = p; // then add our new Point to the end
++index; // increase the recorded number of elements
delete[] points; // out with the old
points = new_points; // in with the new
}
private:
unsigned int index; // size of "points" array
Point* points;
};
But this approach is very inefficient. To do this well is quite complex. The main problems with doing things this way are:
Exception safety - avoiding a memory leak if an exception is thrown.
Allocation - avoiding having to reallocate (and re-copy) every single time.
Move semantics - taking advantage of some objects ability to be moved much more efficiently than they are copied.
A (slightly) better version:
class Line {
public:
Line(): index(0) {} // initialize
virtual ~Line() { } // No need to clean up because of `std::unique_ptr`
void push(const Point& p)
{
// create new array one element larger than before
auto new_points = std::unique_ptr<Point[]>(new Point[index + 1]);
// first add our new Point to the end (in case of an exception)
new_points[index] = p;
// then copy/move old elements to new array (if any)
for(unsigned int p = 0; p < index; ++p)
new_points[p] = std::move(points[p]); // try to move else copy
++index; // increase the recorded number of elements
std::swap(points, new_points); // swap the pointers
}
private:
unsigned int index; // size of "points" array
std::unique_ptr<Point[]> points; // Exception safer
};
That takes care of exception safety and (to some degree - but not entirely) move semantics. However it must be pointed out that exception safety is only going to be complete if the elements stored in the array (type Point) are themselves exception safe when being copied or moved.
But this does not deal with efficient allocation. A std::vector will over allocate so it doesn't have to do it with every new element. This code also misses a few other tricks that a std::vector would employ (like allocating uninitialized memory and constructing/destructing the elements manually as and when they are needed/discarded).
You basically have no way but to allocate a new array, copy existing values inside and delete [] the old one. That's why vector is doing the reallocation by a multiplicative factor (say each reallocation doubles the size). This is one of the reasons you want to use the standard library structures instead of reimplementing.
Keep It Simple
In my opinion, in this case, it's better to use a Linked-List of CPoint in CLine:
struct CPoint
{
int x = 0, y = 0;
CPoint * m_next = nullptr;
};
class CLine
{
public:
CLine() {};
virtual ~CLine()
{
// Free Linked-List:
while (m_points != nullptr) {
m_current = m_points->m_next;
delete m_points;
m_points = m_current;
}
};
// TAKE IN NEW POINT, INCREASE THE ARRAY SIZE AND ADD NEW POINT TO THE END OF THE ARRAY
void push(const CPoint& p)
{
m_current = (((m_points == nullptr) ? (m_points) : (m_current->m_next)) = new CPoint);
m_current->m_x = p.m_x;
m_current->m_y = p.m_y;
m_index++;
};
private:
unsigned int m_index = 0; // size of "points" array
CPoint * m_points = nullptr, * m_current = nullptr;
};
.
Or, even better with smart pointers:
#include <memory>
struct CPoint
{
int m_x = 0, m_y = 0;
std::shared_ptr<CPoint> m_next;
};
class CLine
{
public:
CLine() {};
virtual ~CLine() {}
// TAKE IN NEW POINT, INCREASE THE ARRAY SIZE AND ADD NEW POINT TO THE END OF THE ARRAY
void push(const CPoint& p)
{
m_current = (((m_points == nullptr) ? (m_points) : (m_current->m_next)) = std::make_shared<CPoint>());
m_current->m_x = p.m_x;
m_current->m_y = p.m_y;
m_index++;
};
private:
unsigned int m_index = 0; // size of "points" array
std::shared_ptr<CPoint> m_points, m_current;
};
I have an issue with creating objects, adding them to a container class, and having them go out of scope in C++.
As an example, my main.cpp
Container container;
for (unsigned int i = 0; i < 10; i++) {
Item item;
container.add_item(item);
}
and the interface of container.h
struct Container {
std::vector<std::reference_wrapper<Item>> items;
void add_item(Item& item); // Push back of items vector.
};
and item.h
struct Item {
std::unique_ptr<AnotherThing> unrelated_thing;
};
The problem is later on in my main class, the Containers created inside the for-loop have gone out of scope. If I change add_item to pass by value, it gives me issues with the unique_ptr and copy constructor.
Is there some idiomatic way to create objects inside a scope and "transfer" them to another class?
If I change add_item to pass by value, it gives me issues with the unique_ptr and copy constructor.
Your vector need to hold objects by value:
std::vector<Item> items;
then you need to move your object into vector when passed by value:
void Container::add_item( Item item )
{
items.push_back( std::move( item ) );
}
and then you need to move your object in the loop as well:
for (unsigned int i = 0; i < 10; i++) {
Item item;
container.add_item( std::move(item) );
}
or you can simply pass temporary:
for (unsigned int i = 0; i < 10; i++) {
container.add_item( Item() );
}
There are a few ways to handle this. This is basically the exact use-case for passing xvalues. Passing by value will cause a copy of all members of item. If there are members that could have large amounts of data allocated on the heap, such as a std::vector, you will want to avoid this copy and allocation by moving your members.
The simple answer is to pass by what Scott Meyers calls a 'Universal Reference'
void Container::add_item( Item&& item) {
items.push_back( std::move(item))
}
I'm currently working to instantiate an Array of Stacks of cards for a FreeCell implementation. Each of the Array, Stack, and Card classes are created by me, and the Stack and Array classes are templated.
Array<StackRA<Card>> * hometemp = new Array<StackRA<Card>>(4);
When this code runs, it instantiates an Array of size 4, as intended, but calls the default constructor on the Stack, which I need to instantiate to size 13.
I've tried the following:
Array<StackRA<Card>> * hometemp = new Array<StackRA<Card>(13)>(4)
Array<StackRA<Card>> * hometemp = new Array<StackRA<Card>13>(4)
The Stack's default constructor is as follows:
template <typename T>
StackRA<T>::StackRA() : m_stack()
{
size = 0;
}
As well as the 1 arg constructor that I want to call
template <typename T>
StackRA<T>::StackRA( int data ) : m_stack( data )
{
size = data;
}
I've tried my luck on Google, but it keeps leading me to resources on templates inside template classes. Any thoughts are appreciated.
EDIT: Stepping into my program leads to the Array constructor
template<class T>
Array<T>::Array(int length, int start_index)
{
if (length < 0)
{
cout << "Length cannot be negative, length has been defaulted to 0\n";
m_length = 0;
}
else
{
m_length = length;
m_start_index = start_index;
m_array = new T[length];
}
}
The problem, more specifically, is that the "m_array = new T[length];" calls the StackRA default constructor. The question still stands, how can the non default constructor be called. Where in the instantiation would I place the size data I want to pass in?
EDIT2: I've solved this issue for this specific implementation using:
template <typename T>
StackRA<T>::StackRA() : m_stack()
{
size = 13;
}
First, if Array represents an array, and is meant to be used like one, then you don't need to create a pointer. You can construct it as an automatic object:
Array<StackRA<Card>> hometemp(4);
Now the problem as you say is with the following code calling the default-constructor for each element in the array:
m_array = new T[length];
This is true, the default-constructor will be called, initializing each constructor as T(). Prior to C++11, it was not possible to initialize each element using a different constructor, but now you are able to initialize using an initializer-list:
m_array = new T[length] { T(13), T(13), ... };
Of course this wouldn't be preferable for a variable-sized array (or an array with a very large size), and it's not for our purposes. Instead, you can follow the behavior of std::vector and create another constructor that copies an object to each element in the array:
template <typename T>
StackRA<T>::StackRA(int size, const T& obj) : size(size), m_stack(/* ... */)
{
m_array = new T[size];
std::fill(m_array, m_array + size, obj);
}
Now when constructing the array you can pass the correct constructor:
Array<StackRA<Card>> hometemp(4, StackRA<Card>(13));
Is there anything special in your Array so you cannot use std::vector? With std::vector, you would simply say
std::vector<StackRA<Card>> * hometemp =
new std::vector<StackRA<Card>>(4, StackRA<Card>(13));
Better, if the size is always fixed, use std::array, e.g.
std::vector<std::array<Card, 13>> * hometemp =
new std::vector<std::array<Card, 13>>(4);
or
std::array<std::array<Card, 13>, 4> * hometemp =
new std::array<std::array<Card, 13>, 4>();
or even
using Stack = std::array<Card, 13>;
using Deck = std::array<Stack, 4>;
Deck *hometemp = new Deck();
If you want to add functionality, you can simply encapsulate standard library containers into your own types.
Also, you might reconsider your use of the free store and especially raw pointers:
Deck hometemp;
If you insist on your own implementations, you might have a look at std::vector constructors for ideas.
Debug_VLD in VS2010 reveals some memory leaks that come from class member creation / initialization / deletion.
my_member is a data member with type double*. In constructor, I have
my_member = NULL ;
Then in some method, i need to allocate memory for my_member. I cannot do so in constructor,
since i dont know size of array yet, and/or size may different for different calls of the method. What i do in this method is checking if member is NULL. if so, i allocate space for it, if not, i can operate on array (changing value for its element with accesor []). It looks like
void MyClass::my_method()
{
if( my_member == NULL )
my_member = new double[n_dim] ;
for(int k = 0 ; k < n_dim ; k++ )
my_member[k] = k ;
}
and memory leak occurs at line my_member = new double[n_dim] ;.
In destructor, i have
delete[] my_member ;
what is wrong? how to do allocation properly ?
thanks!
Using std::vector<double> is the preferred way, but if you want to have raw double* and write copy constructor, move constructor and operator= "by hand", you should do something like this:
#include <assert.h> // for assert
#include <algorithm> // for std::swap
class MyClass
{
//
// Raw array
//
double * m_ptr; // raw pointer
size_t m_dim; // number of items in the array
public:
// Default constructor - creates empty vector
MyClass()
: m_ptr(nullptr)
, m_dim(0)
{
}
// Copy constructor
MyClass(const MyClass& src)
: m_ptr(nullptr)
, m_dim(0)
{
// Special case of empty source
if (src.m_dim == 0)
{
assert(src.m_ptr == nullptr);
return;
}
// Allocate and deep-copy from source
m_ptr = new double[src.m_dim];
m_dim = src.m_dim;
for (size_t i = 0; i < m_dim; i++)
m_ptr[i] = src.m_ptr[i];
}
// Move constructor: steal the "guts" from src
MyClass(MyClass&& src)
{
m_ptr = src.m_ptr;
src.m_ptr = nullptr;
m_dim = src.m_dim;
src.m_dim = 0;
}
// Destructor
~MyClass()
{
delete [] m_ptr;
}
// Unified operator=
MyClass& operator=(MyClass src)
{
std::swap(m_ptr, src.m_ptr);
std::swap(m_dim, src.m_dim);
return *this;
}
};
If there's any other place in the code where you set my_member to NULL without calling delete[], then yes. If you don't obey the rule of three (properly implemented copy constructor and assignment operator), you'll run into all sorts of trouble.
To prevent this, use a std::vector<double> instead, where you can do:
void MyClass::my_method()
{
my_member.resize(n_dim); // yay, learned something new here
for(int k = 0 ; k < n_dim ; k++ )
my_member[k] = k ;
}
That way, you're not managing memory, so there's no need for a destructor (unless it's virtual, in which case it can be empty).
I have a header file:
using namespace std;
class IntList{
private:
int *Intl;
int Capacity;
int Count;
public:
IntList(int capacity){
Capacity = capacity;
Count = 0;
Intl = new int[capacity];
}
~IntList(){
delete Intl;
}
//adds the integers of the specified collection to the end of the List; return false if the new Count will be greater than Capacity
bool AddRange(const IntList &items){
//int *Temp = items.;
if(items.Count > Capacity - Count){
return false;
}else{
for(int i = 0; i <items.Count; i++){
Intl[Count] = items.Intl[i];
Count++;
}
return true;
}
}
};
But I don't know why I can't return value to IntList object in there:
//creates a copy of a range of elements in the source List
IntList GetRange(int index, int count){
IntList A(count);
for(int i = 0; i < count; i++){
A.Intl[i] = Intl[index -1 +i];
}
return A;
}
I want to return value of A whose type is IntList but I meet an error on "_BLOCK_TYPE_IS_VALID(pHead->nBlockUse) in visual studio 2010. How can I repair it?
Because int *Intl; is an object you manually manage, you'll need to implement the copy constructor for your class.
The function GetRange returns by value. The local object A gets destroyed, and its member Intl gets deleted in the destructor, so your copy (as copied by the default copy constructor) is only a shallow one, and will contain an invalid member.
EDIT: As Rob correctly pointed out, you'll also need to implement the assignment operator (you already have a destructor).
For an object which is returned by value makes a call to copy-constructor . You must make a copy-constructor and define it so that you get appropriate results. Returning by reference actually does not require call to copy constructor but should not be made for a temporary object. Also since you have a pointer type as member variable in class . It would be appropiate for you to overload the = operator. It should be define properly to avoid memory leak. Do something like this Intlist a=GetRange(index,count) . Also you should create a copy constructor for this . Your code also has a bug that it doesnot overload= operator for class Intlist .
you can write a copy constructor something like this :-
Intlist::Intlist(const Intlist& cSource)
{
capacity = cSource.capacity;
count= cSource.count;
// Intl is a pointer, so we need to deep copy it if it is non-null
if (cSource.Intl)
{
// allocate memory for our copy
Intl = new int[capacity];
// Copy the Intl into our newly allocated memory in for loop
for(i=0;i<capacity;i++)
{
// copy part
}
}
else
intl = NULL;
}
Just an e.g how you should write it.