Does the following create x new objects, or simply allocate space for x objects?:
Vector3D* binArray = new Vector3D[size];
I need to build an array with space for x Vector3D objects on the heap. However, a Vector3D object can only be created when an "add" function is called - this will take the parameters, construct the object on the heap and add its address to the array of Vector3D pointers.
This does create an array of Vector3D objects on the heap.
Each vector is created by calling the Vector3D constructor.
Put a little debugging print statement in the default constructor for Vector3D, and watch the constructor get called the same number of times as you have vectors in your array.
Example:
#include <iostream>
using namespace std;
class C {
public:
C() { cout << "Hello default constructor here\n"; }
};
int main() {
C* cs = new C[5];
}
Output is:
Hello default constructor here
Hello default constructor here
Hello default constructor here
Hello default constructor here
Hello default constructor here
If your class does not have a default constructor, you cannot allocate the array in one shot (thank you for the comment #Everyone), so in this case consider using a std::vector or a std::array and adding your Vector3D objects dynamically --- or even "statically"! Example:
#include <iostream>
#include <vector>
using namespace std;
class Vector3D {
double i, j, k;
public:
Vector3D(double i, double j, double k): i(i), j(j), k(k) {}
};
int main() {
vector<Vector3D> v = {
Vector3D(3, 4, 5),
Vector3D(6, 8, 10),
Vector3D(7, 24, 25)
};
v.push_back(Vector3D(1, 2, 3));
cout << v.size() << '\n';
}
This outputs 4.
You can also make your vector contain pointers to Vector3D objects.
Just to add based on the asker's comments on RayToal's excellent answer. If you don't know the size of the binArray prior to runtime then you must use std::vector. If you want to allocate each item alone, I would recommend using std::vector<Vector3D*>.
This way you can resize the std::vector at runtime and when you do, it will hold a bunch of nullptrs that are not allocated. Then you can allocate each one of them separately.
std::vector<Vector3D*> binArray;
binArray.resize(x); // now you have binArray of size x and no allocated elements
binArray[0] = new Vector3D(...);
Please keep in mind that you need to delete them after you're not using them in order to not have a memory leak:
for(size_t i=0;i<binArray.size(); i++)
if(binArray[i]!=nullptr) delete binArray[i];
Related
I am using the the armadillo matrix library in c++, and I want to create a vec that uses "auxiliare memory". The standard way to do this is
vec qq(6); qq<<1<<2<<3<<4<<5<<6;
double *qqd = qq.memptr();
vec b1(qqd, 6, false);
So here, if I change the elements in b1, the elements in qq is changed, which is what I want. However, in my program, I declare the b1 vector globaly, so when I define it, I cant call the constructor that makes b1 use "auxiliare memory". Is there a function in armadillo that does what I want? Why do I get different results when I run the codes below?
vec b1= vec(qq.memptr(), 3, false); //changing b1 element changes one of qq's element
vec b1;
b1= vec(qq.memptr(), 3, false); //changing b1 element does not chagne qq's
So, how can I make a vector use the memory from a other vector, when it is declared globaly?
Using global variables is generally a bad idea.
However if you insist on using them, here is one possible way of using auxiliary memory with global Armadillo vectors:
#include <armadillo>
using namespace arma;
vec* x; // global declaration as a pointer; points to garbage by default
int main(int argc, char** argv) {
double data[] = { 1, 2, 3, 4 };
// create vector x and make it use the data array
x = new vec(data, 4, false);
vec& y = (*x); // use y as an "easier to use" alias of x
y.print("y:");
// as x is a pointer pointing to an instance of the vec class,
// we need to delete the memory used by the vec class before exiting
// (this doesn't touch the data array, as it's external to the vector)
delete x;
return 0;
}
To illustrate my problem I minimalize my source:
#include <iostream>
class vec{
public:
float* X;
int DIM;
vec(int dimension)
{
DIM = dimension;
X = new float[dimension];
for (int i=0;i<DIM;i++)
X[i] = (float) rand()/RAND_MAX;
}
~vec(void){
delete [] X;
}
vec operator-( vec const& rhs )
{
vec ans(DIM);
for (int i=0;i<DIM;i++)
ans.X[i] = X[i] - rhs.X[i];
return ans;
}
};
int main(){
vec A(5),B(5),C(5);
C= A-B;
return 0;
}
When I execute this program I get an error that a heap was destroyed. I am pretty sure that the destructor is my problem. In the line with C= A-B; the variable ans will be destroyed by the constructor and cannot be returned. Is that right?
If I delete the line delete [] X; everything is ok. But it won't free up the memory.
I did my homework and consult one of the most famous search engine for this problem but didn't find any answer. How can I fix it?
C = A-B calls the default copy-assignment operator (as you haven't defined one). Therefore two different objects will point to the same dynamically-allocated array.
You need to read up on the Rule of Three (in a nutshell: if you define any of the destructor, the copy constructor or the copy-assignment operator, you probably need to define them all).
But preferably, you should avoid using raw arrays entirely; use a container type that manages its own memory.
Well I am questioning myself if there is a way to pass a vector directly in a parameter, with that I mean, like this:
int xPOS = 5, yPOS = 6, zPOS = 2;
//^this is actually a struct but
//I simplified the code to this
std::vector <std::vector<int>> NodePoints;
NodePoints.push_back(
std::vector<int> {xPOS,yPOS,zPOS}
);
This code ofcourse gives an error; typename not allowed, and expected a ')'
I would have used a struct, but I have to pass the data to a Abstract Virtual Machine where I need to access the node positions as Array[index][index] like:
public GPS_WhenRouteIsCalculated(...)
{
for(new i = 0; i < amount_of_nodes; ++i)
{
printf("Point(%d)=NodeID(%d), Position(X;Y;Z):{%f;%f;%f}",i,node_id_array[i],NodePosition[i][0],NodePosition[i][1],NodePosition[i][2]);
}
return 1;
}
Ofcourse I could do it like this:
std::vector <std::vector<int>> NodePoints;//global
std::vector<int> x;//local
x.push_back(xPOS);
x.push_back(yPOS);
x.push_back(zPOS);
NodePoints.push_back(x);
or this:
std::vector <std::vector<int>> NodePoints;//global
std::vector<int> x;//global
x.push_back(xPOS);
x.push_back(yPOS);
x.push_back(zPOS);
NodePoints.push_back(x);
x.clear()
but then I'm wondering which of the two would be faster/more efficient/better?
Or is there a way to get my initial code working (first snippet)?
Use C++11, or something from boost for this (also you can use simple v.push_back({1,2,3}), vector will be constructed from initializer_list).
http://liveworkspace.org/code/m4kRJ$0
You can use boost::assign as well, if you have no C++11.
#include <vector>
#include <boost/assign/list_of.hpp>
using namespace boost::assign;
int main()
{
std::vector<std::vector<int>> v;
v.push_back(list_of(1)(2)(3));
}
http://liveworkspace.org/code/m4kRJ$5
and of course you can use old variant
int ptr[1,2,3];
v.push_back(std::vector<int>(ptr, ptr + sizeof(ptr) / sizeof(*ptr));
If you don't have access to either Boost or C++11 then you could consider quite a simple solution based around a class. By wrapping a vector to store your three points within a class with some simple access controls, you can create the flexibility you need. First create the class:
class NodePoint
{
public:
NodePoint( int a, int b, int c )
{
dim_.push_back( a );
dim_.push_back( b );
dim_.push_back( c );
}
int& operator[]( size_t i ){ return dim_[i]; }
private:
vector<int> dim_;
};
The important thing here is to encapsulate the vector as an aggregate of the object. The NodePoint can only be initialised by providing the three points. I've also provided operator[] to allow indexed access to the object. It can be used as follows:
NodePoint a(5, 6, 2);
cout << a[0] << " " << a[1] << " " << a[2] << endl;
Which prints:
5 6 2
Note that this will of course throw if an attempt is made to access an out of bounds index point but that's still better than a fixed array which would most likely seg fault. I don't see this as a perfect solution but it should get you reasonably safely to where you want to be.
If your main goal is to avoid unnecessary copies of vector<> then here how you should deal with it.
C++03
Insert an empty vector into the nested vector (e.g. Nodepoints) and then use std::swap() or std::vector::swap() upon it.
NodePoints.push_back(std::vector<int>()); // add an empty vector
std::swap(x, NodePoints.back()); // swaps contents of `x` and last element of `NodePoints`
So after the swap(), the contents of x will be transferred to NodePoints.back() without any copying.
C++11
Use std::move() to avoid extra copies
NodePoints.push_back(std::move(x)); // #include<utility>
Here is the explanation of std::move and here is an example.
Both of the above solutions have somewhat similar effect.
I'm trying to create an array of classes using a vector, but I think I'm getting the syntax wrong from instantiating the array. The error I'm getting is:
error: request for member 'setX' in objects[0], which is of non-class type 'std::vector'
#include <iostream>
#include <vector>
using std::cout;
class A {
public:
void setX(int a) { x = a; }
int getX() { return x; }
private:
int x;
};
int main() {
std::vector<A> *objects[1];
objects[0].setX(5);
objects[1].setX(6);
cout << "object[0].getX() = " << objects[0].getX() << "\nobject[1].getX() = " << objects[1].getX() << std::endl;
}
std::vector<A> objects; // declare a vector of objects of type A
objects.push_back(A()); // add one object of type A to that vector
objects[0].setX(5); // call method on the first element of the vector
With an asterisk and a square brackets, you are declaring an array of pointers to vectors instead of a vector. With std::vector<T> you do not need square brackets or an asterisk:
std::vector<A> objects(2); // 2 is the number of elements; Valid indexes are 0..1, 2 is excluded
objects[0].setX(5); // This will work
objects[1].setX(6);
The reason the compiler thought that you were trying to call setX on a vector is that the square bracket operator, overloaded by the vector, is also a valid operator on an array or a pointer.
An array and std::vector are two completely different container types. An array is actually a fixed-size block of memory, where-as a std:vector object is a dynamic sequential container type, meaning it can be dynamically "grown" and "shrunk" at run-time, and the object itself manages the memory allocation of the objects it owns. Their apparent similarities are that both can access members in O(1) complexity and can use the bracket syntax for accessing members.
What you want is something like the following:
int main()
{
//make a call to the std::vector<T> cstor to create a vector that contains
//two objects of type A
std::vector<A> objects(2);
//you can now access those objects in the std::vector through bracket-syntax
objects[0].setX(5);
objects[1].setX(6);
cout << "object[0].getX() = " << objects[0].getX() << "\nobject[1].getX() = " << objects[1].getX() << std::endl;
return 0;
}
here what you did is define an array with 1 element of type std::vector*, you may want to read more about vector and array first.
The correct way to define it is:
std::vector<A> objects(2);
or using pointers if that is what you intend to
std::vector<A*> objects(2);
objects[0] = new A();
objects[1] = new A();
I have:
class first{
private:
int *array;
public:
first(int x){
array = new int[x][10];
}
I want to call this class by:
first class1 = new first(10);
Why it doesn't work ? How to inintialize array by size from constructor ??
Just this is enough:
first class1(10);
new is for when you're allocating a pointer.
first *class1 = new first(10);
Furthermore, you have an incompatibility here:
array = new int[x][10];
array is an int*, but new int[x][10] is a 2D array. I'm not sure which one you want.
For the 1D array:
int *array;
array = new int[x];
For the 2D array:
int (*array)[10];
array = new int[x][10];
That said, you might be better off using std::vector.
Side Note: Since you have memory allocation in the constructor, you should also implement a destructor, copy-constructor, and copy-assignment operator.
You've indicate that you want a one-dimensional array (int*) but attempted to allocate a two-dimensional array (new [x][10]).
I'll assume you need one dimension.
The C++ way to do this is with vector.
#include <vector>
class first{
private:
std::vector<int> array;
public:
explicit first(int x) : array(x) {
}
};