I need to initialize an array of objects of a parametrized constructor . How can i do it in best possible way ?
# include<iostream>
# include<conio.h>
# include<stdio.h>
using namespace std;
class A
{
public:
int a;
A();
A(int x)
{
a=x;
}
};
int main()
{
A *a1,a2(1);
a1 = (A*)malloc(sizeof(A)*10); // equivalent to A[10].
for(int i=0;i<10;i++) a1[i]=a2; // Initialization is important in any program.
for(int i=0;i<10;i++) cout<<a1[i].a;
getch();
return 0;
}
This does work but is there some other way better than this ?
The C++ way would be to use a std::vector.
std::vector<A> a1(10, 1);
creates 10 A's initialized by 1.
This is solved using std::vector constructor taking size and base element :
A a2(1);
std::vector<A> tab(10, a2);
Note that malloc does not construct objects, and so calling a1[i]=a2 is bad form. It probably seems to work fine since they are POD-ish objects, but this is not the proper way to do C++. It is undefined behavior, which is completely unpredictable. It may work ten thousand times in a row, and then erase your bank account. You should use new instead, which also constructs. Or better yet, use a vector, like the other answers suggest. Also, be sure that the default constructor initializes the data, and initialization will be less of a worry.
If you really must use malloc, the "correct way" to initialize is this:
std::uninitialized_copy(a1, a1+10, a2); //constructs and assigns
which is roughly equivalent to:
{
int i=0;
try {
for(i=0; i<10; ++i)
new(a1+i)A(a2); //constructs and initializes in the buffer
} catch(...) {
try {
for(; i>=0; --i)
(a1+i)->~A(); //destroy if an error occured
} catch(...) {
std::terminate();
}
throw;
}
}
Related
#include<iostream>
using namespace std;
template<class T>
class vee
{
T* v;
int size;
public:
vee(int m)
{
v = new T[size=m];
cout<<size<<"\n";
}
vee(T* a)
{
cout<<size<<"\n";
for(int i=0;i<size;i++)
{
v[i]=a[i];
}
}
};
int main()
{
int x[]={1,2,3};
int y[]={2,3,4};
vee<int> v1(3);
v1=x;
return 0;
}
Why i am getting 2 different values of "size" ?
I have created a constructor to intilize the parameter size and it shows correct value in first constructor but it throws a garbage value in the second constructor ,why??
Why i am getting 2 different values of "size" ?
vee(T* a) is called converting constructor. When you write something like v1=x;, array x decays to a pointer, then it is converted to vee with provided converting constructor.
v1=x; is as if you wrote v1=vee<int>(x);
As you can see a temporary instance is created with undefined size and pointer, which is bad. Then you assign this instance to v1 which is worse.
If you do not want this autoconversion in the future, declare your constructor explicit
explicit vee(T* a)
When doing v1=x, you actually go and create new object, since you have not overrided the '=' operator.
but it throws a garbage value in the second constructor ,why??
vee(int m)
{
v = new T[size=m]; // in this constructor you set size
cout<<size<<"\n";
}
vee(T* a)
{
// but in this constructor you don't set size
cout<<size<<"\n";
for(int i=0;i<size;i++)
{
v[i]=a[i];
}
}
So, when you create a vee using the second constructor - and read from size - your program has undefined behaviour.
How would I go about allocating an array of a class without constructing the class, so I could fill up the array later?
I was originally trying to use
Myclass * array = new Myclass[N];
But it tries to construct Myclass to N.
First just declare it without allocating
Myclass * array[N];
when you need it
for(int i=0;i<N;i++){
array[i] = new Myclass(/*params*/);
}
But consider using std::vector/std::list if you must not have to manage memory yourself.
If you really want to do that, (not sure why), you could try
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass()
{ cout << "helo" << endl; }
};
int main(int argc, char *argv[])
{
int size = 4;
// Here is the trick, pointer to pointer.
MyClass **vec = new MyClass *[size];
cout << "before" << endl;
for (int i = 0; i < 4; ++i)
vec[i] = new MyClass;
// remember to free the vec
return 0;
}
Someone suggested placement new, so here it goes:
// allocate space
std::vector<unsigned char> mybuffer(N * sizeof(Myclass));
Myclass *array = reinterpret_cast<Myclass *>(&mybuffer[0]);
// when you're ready to use it
new( &array[0] ) Myclass(2);
new( &array[1] ) Myclass(3);
// etc...
// when you're done with it
array[0].~Myclass();
array[1].~Myclass();
// etc....
Of course, it is undefined behaviour to use array[x] before you have new'd it, or after you called the destructor.
This is generally something you wouldn't use as a solution to a "normal" problem. Consider actually defining a default constructor that does nothing, and having a function you call later which enhances the objects above their default state.
If you can use C++11, the optimal solution for you is probably std::vector<MyClass> with emplace-base insertions:
class MyClass {
public:
MyClass(int a, bool b, char c); // some non-default constructor
MyClass(double d); // another constructor
void bar();
};
void foo(int n) {
std::vector<MyClass> mv;
mv.reserve(n); // not even needed but beneficial if you know the final size.
// emplace_back uses perfect forwarding to call any arbitrary constructor:
mv.emplace_back(2, false, 'a');
mv.emplace_back(3, true, 'b');
mv.emplace_back(3.1415926535);
// can iterate vector easily:
for (auto &i : mv) {
i.bar();
}
// everything destructed automatically when the collection falls of scope ...
}
This creates the values in the collection directly without a copy and defers any construction of elements until you are ready, unlike new[], which makes a bunch of default objects at array-creation time. It is generally better than placement new as well, since it doesn't leave open opportunities for missed destruction or destructing an invalid memory location as well as being just easier to read.
Alternatively, you may use boost::optional.
So in your case:
std::vector<boost::optional<Myclass>> array(N);
I want to store a dynamic array of structs as a member variable within another struct. Is this the proper way to use the constructor given my example in main?
EDIT
I corrected some of the obvious mistakes I had in the code (was 6am at the time). I also added another member to B to see if the push_back is still correct. I know my life would be a lot easier using vectors for dynamic memory but I need to do it this way as these are structs to be used with thrust::device_vector in the end.
struct A
{
float mem1;
int mem2;
};
struct B
{
A * Aarr1;
A * Aarr2;
B(A * a1, A * a2): Aarr1(a1), Aarr2(a2){}
};
int main()
{
A * test = new A[5];
A * test2 = new A[10];
vector<B> btest;
btest.push_back(B(test, test2));
for(int i=0; i<5; i++)
printf("mem1: %f, mem2: %i \n", btest[0].Aarr[i].mem1, btest[0].Aarr[i].mem2);
}
Taken in isolation, the constructor is fine. However, there are many other problems with the code.
As it stands, your code is leaking memory since the array is never deallocated.
You might want to consider moving away from using C arrays to std::vector or std::array.
There's also a bug in printf() (misspelt as print()): one of the two mem1 should be mem2.
The constructor of B is OK, but the way you invoke push_back() is not:
btest.push_back(B(A));
You should do this:
btest.push_back(B(test));
Moreover, the explicit construction of a B object is not necessary, since your constructor is not marked as explicit:
btest.push_back(test);
Also consider using automatic memory management rather than raw pointers (std::vector<> instead of arrays, smart pointers instead of pointers). This way, you will avoid leaking memory due to forgetting this:
delete test;
Leaking aside, the worst thing is that your code also has Undefined Behavior, because it uses the value of uninitialized variables (the member variables of A within the for loop).
Finally, you shouldn't use : after class names in a class definition. This is how you could rewrite your code in C++11:
#include <vector>
#include <cstdio>
struct A // Do not use ":" here, that's for introducing inheritance,
// which you are not using in your example.
{
float mem1 = 0.0; // In C++11, you can specify default initialization
int mem2 = 0; // for your member variables this way. In C++03 you
// would have to define a default constructor which
// initializes your member variables to the desired
// value.
};
struct B
{
std::vector<A> Aarr;
// ^^^^^^^^^^^^^^
// Prefer using standard containers over dynamically allocated arrays, as
// it saves your from taking care of memory management and avoids leaks.
explicit B(size_t s): Aarr(s) { }
// ^^^^^^^^
// It is usually a good idea to mark constructors which take on argument
// and are not copy constructors as explicit, to avoid awkward implicit
// conversions.
};
int main()
{
std::vector<B> btest;
btest.push_back(B(5));
// ^^^^
// We need to explicitly construct an object of type B,
// because we have marked B's constructor as explicit.
for(int i=0; i<5; i++)
{
std::printf(
"mem1: %f, mem2: %i \n",
btest[0].Aarr[i].mem1,
btest[0].Aarr[i].mem2
// ^
// You had "mem1" here.
);
}
}
There are few minor mistakes and typos in your code. It should look like this:
struct A // <-- no ':' after name type
{
float mem1;
int mem2;
};
struct B // <-- no ':' after name type
{
A * Aarr;
B(A * a): Aarr(a){}
};
int main()
{
A * test = new A[5];
vector<B> btest;
btest.push_back(B(test)); // <-- test, A is name of type
for(int i=0; i<5; i++)
printf("mem1: %f, mem2: %i \n", // <-- printf, not print
btest[0].Aarr[i].mem1, btest[0].Aarr[i].mem1);
}
Also consider using std::vector or std::array instead of C-style arrays as well.
I have a class that resembles this:
However, after the initial constructor, the copy constructor is being called 10 times.
If I don't do the thread creation step. It gets called 4 times which is what I'd expect.
Why is that, and how do I avoid it?
Should I avoid using std::vector in this case and just do new delete instead?
#include <cstdio>
#include <vector>
class A
{
public:
A() { printf("hello\n"); }
~A() { printf("Goodbye\n"); }
A(const A&)
{
printf("copy constructing\n");
}
Thread() { }
};
int main()
{
std::vector<A> a(4, A);
for (int i = 0; i < a.size(); i++){
threads_.create_thread(boost::bind(&A::Thread, a[i]));
}
}
Ok, I found the problem.
This:
threads_.create_thread(boost::bind(&A::Thread, a[i]));
Should be:
threads_.create_thread(boost::bind(&A::Thread, &a[i]));
Take a look at Boost.Ref
for (int i = 0; i < a.size(); i++){
threads_.create_thread(boost::bind(&A::Thread, boost::ref(a[i]) ));
}
this is from the Boost.Bind :
The arguments that bind takes are
copied and held internally by the
returned function object. For example,
in the following code:
int i = 5;
bind(f, i, _1);
a copy of the value of i is stored
into the function object. boost::ref
and boost::cref can be used to make
the function object store a reference
to an object, rather than a copy:
int i = 5;
bind(f, ref(i), _1);
bind(f, cref(42), _1);
Threads are irrelevant here. The "problem" is with boost::bind; it makes copies of the arguments you give it, so it can use the value later when invoked. Use ref so have it store a reference instead.
Should I avoid using std::vector in this case and just do new delete instead?
Huh? Why would you do that? You need to get over the fact copies are made; that just happens. You should be worried about writing clean, maintainable code first, not whether or not you happen to make a copy. There's no reason to use new[] over a std::vector; never use delete.
I am trying to create an array of class objects taking an integer argument. I cannot see what is wrong with this simple little code. Could someone help?
#include <fstream>
#include <iostream>
using namespace std;
typedef class Object
{
int var;
public:
Object(const int& varin) : var(varin) {}
} Object;
int main (int argc, char * const argv[])
{
for(int i = 0; i < 10; i++)
{
Object o(i)[100];
}
return 0;
}
In C++ you don't need typedefs for classes and structs. So:
class Object
{
int var;
public:
Object(const int& varin) : var(varin) {}
};
Also, descriptive names are always preferrable, Object is much abused.
int main (int argc, char * const argv[])
{
int var = 1;
Object obj_array[10]; // would work if Object has a trivial ctor
return 0;
}
Otherwise, in your case:
int main (int argc, char * const argv[])
{
int var = 1;
Object init(var);
Object obj_array[10] = { var, ..., var }; // initialize manually
return 0;
}
Though, really you should look for vector
#include <vector>
int main (int argc, char * const argv[])
{
int var = 1;
vector<Object> obj_vector(10, var); // initialize 10 objects with var value
return 0;
}
dirkgently's rundown is fairly accurate representation of arrays of items in C++, but where he is initializing all the items in the array with the same value it looks like you are trying to initialize each with a distinct value.
To answer your question, creating an array of objects that take an int constructor parameter. You can't, objects are created when the array is allocated and in the absence of a trivial constructor your compiler will complain. You can however initialize an array of pointers to your object but you really get a lot more flexibility with a vector so my following examples will use std::vector.
You will need to initialize each of the object separately if you want each Object to have a distinct value, you can do this one of two ways; on the stack, or on the heap. Lets look at on-the-stack first.
Any constructor that take a single argument and is not marked as explicit can be used as an implicit constructor. This means that any place where an object of that type is expected you can instead use an instance of the single parameter type. In this example we create a vector of your Object class and add 100 Objects to it (push_back adds items to a vector), we pass an integer into push_back which implicitly creates an Object passing in the integer.
#include <vector>
int main() {
std::vector<Object> v;
for(int i = 0; i < 100; i++) {
v.push_back(i);
}
}
Or to be explicit about it:
#include <vector>
int main() {
std::vector<Object> v;
for(int i = 0; i < 100; i++) {
v.push_back(Object(i));
}
}
In these examples, all of the Object objects are allocated on the stack in the scope of the for loop, so a copy happens when the object is pushed into the vector. Copying a large number of objects can cause some performance issues especially if your object is expensive to copy.
One way to get around this performance issue is to allocate the objects on the heap and store pointers to the objects in your vector:
#include <vector>
int main() {
std::vector<Object*> v;
for(int i = 0; i < 100; i++) {
v.push_back(new Object(i));
}
for(int i = 0; i < 100; i++) {
delete v[i];
}
}
Since our objects were created on the heap we need to make sure that we delete them to call their deconstructor and, free their memory, this code does that in the second loop.
Manually calling delete has it's own caveats, if you pass these pointers to other code you can quickly loose track of who owns the pointers and who should delete them. An easier way to solve this problem is to use a smart pointer to track the lifetime of the pointer, see either boost::shared_ptr or tr1::shared_ptr which are reference-counted pointers :
#include <vector>
int main() {
std::vector<shared_ptr<Object> > v;
for(int i = 0; i < 100; i++) {
Object* o = new Object(i);
v.push_back(shared_ptr<Object>(o));
}
}
You'll notice that the shared_ptr constructor is explicit, this is done intentionally to make sure that the developer is intentionally stuffing their pointer into the shared pointer. When all references to an object are released the object will automatically be deleted by the shared_ptr, freeing us of the need to worry about it's lifetime.
If you want to stick to arrays, then you must either initialize manually or use the default constructor. However, you can get some control by creating a constructor with a default argument. This will be treated as a default constructor by the compiler. For example, the following code prints out the numbers 0, ..., 9 in order. (However, I'm not sure that the standard dictates that the objects in the array must be constructed in order. It might be implementation dependent, in which case the numbers may appear in arbitrary order.)
#include <iostream>
using namespace std;
struct A {
int _val;
A(int val = initializer()) : _val(val) {}
static int initializer() { static int v = 0; return v++; }
};
int main()
{
A a[10];
for(int i = 0; i < 10; i++)
cout << a[i]._val << endl;
}