I am currently exploring the new c++11 features, I am wondering how to initialize the memory pointed by the unique_ptr, with the value I provided in the initializer_list. With a raw memory pointer like "T* data", I can use uninitialized_copy, but when I change from "T* data" to "unique_ptr data" (so I don't have to do explicit deconstruction) it does not work. I am still reading materials on unique_ptr (and my guess is that's because unique_ptr is not copyable), but could someone point out the function that does the job? Thanks!
#include <iostream>
#include <memory>
using namespace std;
template<class T>
class Vector
{
private:
unique_ptr<T []> data;
int size;
public:
Vector(initializer_list<T> l)
{
size = l.size();
cout << "Constructing, size is: " << size << endl;
data.reset(new T[size]); // reserve memory
uninitialized_copy(l.begin(),l.end(),data); // not working
//for(int i = 0; i<size; i++ ) // not working -> l has no operator[]
// data[i] = l[i];
}
void Report()
{
for(int i = 0; i<size; i++ )
cout << data[i] << ' ';
cout << '\n';
}
};
int main(int argc, const char *argv[])
{
int a = 1;
Vector<int> v {1,2,3,4};
v.Report();
return 0;
}
Simply use the getter :
std::copy(l.begin(),l.end(),data.get());
Related
I'm a beginner and was researching to find out if you can grab a name from an array to then put that name as the name of the object in a for loop.
The code a specifically created for this question is represented below:
#include <iostream>
using namespace std;
class YourMum{
public:
string name;
YourMum(string aName){
name = aName;
}
};
int main()
{
string names[5] = {"Jeremy_Clarkson", "Boris_Johnson", "Vladmir_Putin", "Peter_Griffin", "MeAndYourMum"};
for(int i = 0; i < 4; i++) {
YourMum names[i];
cout << names[i].name << endl;
cout << "You are great if you answer my question!";
}
return 0;
}
Okay, it's not clear what you're trying to do, but you've got a few things that aren't a good idea.
First, you defined string names[5] and then inside loop you hid names by making a second variable with the same name. While that's legal, it's a bad practice.
Second, you're using the second one (YourMum names) illegally:
You declared it as length i (using a non-standard feature of the compiler) but then reference element i. The elements of an array of length i range from 0..i-1, so [i] is one element past the end.
And you haven't initialized it anyway.
I'm not really clear what you're trying to do, so I can't even provide example code of how to do it.
The YourMum class you have shown is not default constructible, so YourMum names[size]; will not work. Every object of YourMum needs to be passed a parameter in order to create it. You can use placement-new for that, eg:
#include <iostream>
#include <type_traits>
using namespace std;
class YourMum{
public:
string name;
YourMum(string aName){
name = aName;
}
};
int main()
{
string names[5] = {"Jeremy_Clarkson", "Boris_Johnson", "Vladmir_Putin", "Peter_Griffin", "MeAndYourMum"};
std::aligned_storage<sizeof(YourMum), alignof(YourMum)>::type arr[5];
YourMum *mums = reinterpret_cast<YourMum*>(arr);
for(int i = 0; i < 5; i++) {
new (&mums[i]) YourMum(names[i]);
}
for(int i = 0; i < 5; i++) {
cout << mums[i].name << endl;
cout << "You are great if you answer my question!";
}
for(int i = 0; i < 5; i++) {
mums[i].~YourMum();
}
return 0;
}
Or, you can use std::vector instead:
#include <iostream>
#include <vector>
using namespace std;
class YourMum{
public:
string name;
YourMum(string aName){
name = aName;
}
};
int main()
{
string names[5] = {"Jeremy_Clarkson", "Boris_Johnson", "Vladmir_Putin", "Peter_Griffin", "MeAndYourMum"};
std::vector<YourMum> mums;
mums.reserve(5);
for(int i = 0; i < 5; i++) {
mums.emplace_back(names[i]);
}
for(int i = 0; i < 5; i++) {
cout << mums[i].name << endl;
cout << "You are great if you answer my question!";
}
return 0;
}
First of all, you should rename the name variable to something like _name or name_ for lisibility (to differenciate it from variables that aren't in the class), and you should also use the private section of your class to declare it and use getters/setters functions to edit it. Secondly, in your code you are declaring the object inside a loop, so it will get destroyed as soon as the loop iterates again, so if you want to create n objects in a loop and interact with them after you should either use a container class (like std::vector) or use a default constructor (which is better). Here is an updated version of your code:
#include <iostream>
#include <string>
class YourMum
{
public:
YourMum(void) : _name() { }
YourMum(std::string name) : _name(name) { }
std::string getName(void) const { return _name; }
void setName(std::string name) { _name = name; }
private:
std::string _name;
};
int main()
{
std::string names[5] = { "Jeremy_Clarkson", "Boris_Johnson", "Vladmir_Putin", "Peter_Griffin", "MeAndYourMum" };
YourMum mums[5];
for (int i = 0; i < 5; i++)
{
mums[i].setName(names[i]);
std::cout << mums[i].getName() << std::endl
<< "You are great if you answer my question!" << std::endl;
}
// this way you can still access mums here if you want to
return 0;
}
If you want to access to a class by it's name as index what you can also do is using std::map like this
#include <map>
int main()
{
std::string names[5] = { "Jeremy_Clarkson", "Boris_Johnson", "Vladmir_Putin", "Peter_Griffin", "MeAndYourMum" };
std::map <std::string, YourMum> mums;
for (int i = 0; i < 5; i++)
mums[names[i]] = YourMum(names[i]);
std::cout << mums["Jeremy_Clarkson"].getName() << std::endl;
return 0;
}
Try This
> for(int i = 0; i < 4; i++) {
> YourMum name(names[i]);
>
> cout << name.name << endl;
> cout << "You are great if you answer my question!";
> }
The code snippet is as follows. The error is in foo function for the cout line:
typedef struct Datatype {
int first;
int second;
} Datatype;
void foo(std::array<Datatype, 100>* integerarray){
cout << *integerarray[0].first << endl; //ERROR: has no member first
}
void main() {
std::array<Datatype, 100> newarray;
for(int i=0; i<100; i++)
newarray[i] = i;
}
foo(&newarray);
}
Because of operator precedence, *integerarray[0].first is translated as *(integerarray[0].first), which is not what you want. You need to use (*integerarray)[0].first.
cout << (*integerarray)[0].first << endl;
You can make your life simpler by passing a reference.
void foo(std::array<Datatype, 100>& integerarray){
cout << integerarray[0].first << endl;
}
Also, you don't need to use typedef struct DataType { ... } DataType; in C++. You can use just struct DataType { ... };
newarray[i] = i;
In this line you have missed adding value to structure variables.
Also you have passed array as a reference to function. Removing it and passing just the name of array will pass base address of array.
I am adding following code for your reference:
#include<iostream>
#include <array>
struct Datatype{
int first;
int second;
}
typedef Datatype varInts;
void display(std::array<varInts,20> &dummy)
{
int b =5;
for(int i=0; i<20; i++)
{
dummy[i].first =b++;
dummy[i].second = b+5; //Give any logic you wish.just adding different values;
b++;
}
}
int main()
{
std::array<varInts,20> data;
int a =1;
for(int i=0;i<20;i++)
{
data[i].first = a++;
data[i].second = a+5;
a++; //Just adding values for example
}
display(data);
return 0;
}
It runs without error.Hope it helps!!
I have a (what should be simple) assignment in my C++ class.
Here is the assignment:
Create a class template that contains two private data members: T * array and int size. The class uses a constructor to allocate the array based on the size entered. There is member function that allows the user to fill in the array based on the size. In addition, there is a member function that sorts the array and displays the sorted elements. Use the destructor to delete the array. Develop the main( ) to create two objects to call member functions. As a result, the first object will hold its array of double type, and the other will hold its array of the int type.
Here is what I have came up with but coming up with an error of "Allocation of incomplete type 'T'":
#include <iostream>
#include <new>
#include <vector>
using namespace std;
template <class T>
class DynArray {
protected:
int size;
T ** DynamicArray = new T[size];
public:
DynArray(){
void CreateArray(){
cout << "Enter size of Array: ";
cin >> size;
for (int i = 0; i < size; ++i){
DynamicArray[i] = new T();
}
for (int i = 0; i<size; i++) {
cout << "Element " << i << ": ";
cin >> DynamicArray[i];}
}
//Sort Array
void Sort(T a[], int size)
{
int idx, pass;
for (pass=0; pass<size; ++pass){
for (idx=0; idx<size-1; ++idx){
if (a[idx] > a[idx+1])
swap(a[idx], a[idx+1]);}
}
for (int i=0; i<size; ++i) {
for (idx = 0; idx<size-1; ++idx) {
cout << a[idx] << " ";
}
}
}
void DeleteArray(){
for (int i = 0; i < size; ++i){
delete DynamicArray[i];
}
delete[] DynamicArray;
}
};
int main() {
DynArray<class T>();
return 0;
}
Not sure if I am a complete retard in my line of thinking or if I am just missing a small element. Any help is great.
Thank you #jblixr and #user3655463 for your help. I got it worked out I think after your hints and help.
Here is what I came up with for reference if anyone else is working on this.
#include <iostream>
#include <new>
#include <algorithm>
using namespace std;
//Template Class T
template <class T>
class DynArray {
protected:
int size;
T * DynamicArray;
public:
DynArray(){};
DynArray(size_t s): size(s) {
DynamicArray = new T[size];
for (int i = 0; i<size; i++) {
cout << "Element " << i << ": ";
cin >> DynamicArray[i];
}
}
//Sort Array
void Sort(){
sort(DynamicArray, DynamicArray+size);
for (int i=0; i<size; i++) {
cout << DynamicArray[i] << endl;
}
}
//Clear Heap
~DynArray() {
delete []DynamicArray;
}
};
int main() {
int sizeOfArry;
cout << "Enter size of Array: ";
cin >> sizeOfArry;
//Use as an int Array;
DynArray<int> intArray = DynArray<int>(sizeOfArry);
intArray.Sort();
}
From description you have provided looks to me you want to do sth like that:
#include <iostream>
#include <algorithm>
using namespace std;
template <class T>
class DynArray {
protected:
int size;
T * DynamicArray;
public:
DynArray(size_t s): size(s) {
DynamicArray = new T[s];
}
void CreateArray(){
size_t size;
cout << "Enter size of Array: ";
cin >> size;
if(size > this->size)
size = this->size;
for (int i = 0; i<size; i++) {
cout << "Element " << i << ": ";
cin >> DynamicArray[i];
}
}
//Sort Array
void Sort()
{
std::sort(DynamicArray, DynamicArray+size);
}
DynArray() {
delete []DynamicArray;
}
};
int main() {
DynArray<double> dob(3);
DynArray<int> di(3);
dob.CreateArray();
di.CreateArray();
dob.Sort(); di.Sort();
}
I am wondering that whether you are creating a one dimensional array or 2D array, your initial requirements states one dimensional array but you are using 2D array in your code. Anyway, I play along with one dimensional array.
error: allocation of incomplete type 'T'
T *DynamicArray = new T[size];
What you are trying to do here is in-class initialization which can be done for static data members and it is an c++11 extension for non-static data member. So I suggest you not to do so since you are learning. You can only declare the members and don't initialize here.
Even if you make static it cannot be allocated because the template type T is known only after object creation, So the compiler isn't aware of the type it is going to allocate during the compile time.
So it should be simply as
T *DynamicArray;
Nested functions
C++ doesn't support nested functions, learn C++ syntax.
Usage of constructors and destructors
A constructor will do the functionality of CreateArray() and the destructor will do the functionlaity of DeleteArray()
Instantiating a template class
You should explicitly mention the type within the angle brackets that the template class is going to use
DynArray<int> intArray; // if you want to use int
DynArray<float> floatArray;
You can also use your own custom class for type T, hope you will learn soon in your class.
DynArray<MyCustomClass> customArray;
If you correct all these things then your final skeleton will be like the below one
template <class T>
class DynArray {
protected:
int size;
T *DynamicArray ;
public:
DynArray() {
// initailize DynamicArray here
// use your CreateArray() code here
}
void sort() {
// your own sort logic
// No need to pass the size to this function, its a member function
}
~DynArray() {
// use your DeleteArray() code here
}
};
int main() {
DynArray<int> intArray;
intArray.sort()
return 0;
}
Simple, isn't it? :)
If i comment the line arr = 0; in destructor definition, the program terminates with an error. If i uncomment that line i.e., arr is set to 0, then the program executes without any error. Why is it so as there is no need of setting a pointer to NULL. The pointer itself is destroyed after the execution of destructor.
Below is my code.
Array.h
#ifndef ARRAY_H
#define ARRAY_H
class Array
{
int* arr;
int size;
public:
Array(int size = 10);
Array(const Array& arr);
~Array();
void display () const;
};
#endif
Array.cpp
#include "Array.h"
#include <iostream>
using namespace std;
Array::Array(int size)
{
arr = new int[size];
this->size = size;
for (int i = 0; i < size; i++)
arr[i] = i;
}
Array::Array(const Array& a)
{
arr = new int[a.size];
for (int i = 0; i < a.size; i++)
arr[i] = a.arr[i];
size = a.size;
}
Array::~Array()
{
delete[] arr;
arr = 0;
}
void Array::display() const
{
cout << endl;
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
Main.cpp
#include <iostream>
#include "Array.h"
using namespace std;
int main()
{
Array arr(4);
Array a1 = arr;
a1.display();
arr.~Array();
a1.display();
return 0;
}
You shouldn't be calling the destructor here:
arr.~Array();
When arr goes out of scope, the destructor is called a second time. That leads to undefined behaviour when delete[] is called on the pointer data member arr. When you set that to 0, you side-step the problem, but the fact remains that you shouldn't be invoking the destructor like that.
Here's an example showing how the lifetime of an automatic storage object is bound to its scope:
#include <iostream>
struct Foo
{
~Foo() { std::cout << "Foo destructor\n";
};
int main()
{
std::cout << in main()\n";
{
Foo f;
} // f gets destroyed here
std::cout << "Exiting main()\n";
};
You don't need to explicitly call destructors.
They will execute once the program terminates.
http://www.learncpp.com/cpp-tutorial/86-destructors/
look at the article under the heading constructor and destructor timing
The destructors are written to deallocate the memory assigned to pointers. Otherwise the pointer is deleted but its memory remains dangling.
When the program terminates, all the variables will be lost from the memory, all resources will be deallocated, and during that time the destructor will execute itself.
As an exercise, I'm trying to create a class myArray that acts as a simplified array class. Here is my header:
#ifndef myArray_h
#define myArray_h
typedef double ARRAY_ELEMENT_TYPE;
class myArray {
public:
//--constructors
myArray(int initMax);
// post: Allocate memory during pass by value
myArray(const myArray & source);
// post: Dynamically allocate memory during pass by value
//--destructor
~myArray();
// post: Memory allocated for my_data is deallocated.
//--modifier
void set(int subscript, ARRAY_ELEMENT_TYPE value);
// post: x[subscript] = value when subscript is in range.
// If not, an error message is displayed.
//--accessor
ARRAY_ELEMENT_TYPE sub(int subscript) const;
// post: x[subscript] is returned when subscript is in range.
// If not, display an error message and return [0].
private:
ARRAY_ELEMENT_TYPE* my_data;
int my_capacity;
};
#endif
Here is my implementation:
#include "myArray.h"
#include <iostream>
#include <cstring>
using namespace std;
typedef double ARRAY_ELEMENT_TYPE;
//--constructors
myArray::myArray(int initMax)
{
my_capacity = initMax;
}
myArray::myArray(const myArray & source)
{
int i;
my_data = new ARRAY_ELEMENT_TYPE[source.my_capacity];
for(i=0; i < my_capacity; i++)
my_data[i] = source.sub(i);
}
//--destructor
myArray::~myArray()
{
delete [ ] my_data;
}
//--modifier
void myArray::set(int subscript, ARRAY_ELEMENT_TYPE value)
{
if(subscript > my_capacity - 1)
{
cout << "**Error: subscript " << subscript << " not in range 0.." << my_capacity-1 << ". The array is unchanged." << endl;
}
else
my_data[subscript] = value;
}
//--accessor
ARRAY_ELEMENT_TYPE myArray::sub(int subscript) const
{
if(subscript >= my_capacity)
{
cout << "**Error: subscript " << subscript << " not in range 0.." << my_capacity-1 << ". Returning first element." << endl;
cout << my_data[0];
}
else
{
return my_data[subscript];
}
}
And I'm using this as a test driver:
#include <iostream>
using namespace std;
typedef double ARRAY_ELEMENT_TYPE;
#include "myArray.h"
void show (const myArray & arrayCopy, int n)
{
for(int j = 0; j < n; j++)
cout << arrayCopy.sub(j) << endl;
}
int main()
{
int n = 6;
myArray a(6);
a.set(0, 1.1);
a.set(1, 2.2);
a.set(2, 3.3);
a.set(3, 4.4);
a.set(4, 5.5);
a.set(5, 6.6);
show(a, n);
cout << a.sub(11) << endl;
a.set(-1, -1.1);
return 0;
}
The problem is that when I run this, I get nothing for a bit, then the "Press any key to continue..." prompt. What's going wrong?
The myArray constructor doesn't allocate memory for my_data. The first time you call set, it attempts to write to an uninitialised pointer. This results in undefined behaviour but a crash is likely.
You should change the constructor to
myArray::myArray(int initMax)
{
my_capacity = initMax;
my_data = new ARRAY_ELEMENT_TYPE[my_capacity];
}
There are a couple of other issues with the code you could also consider
In 'set', the test
if(subscript > my_capacity - 1)
should be
if(subscript < 0 || subscript > my_capacity - 1)
Or you could change the subscript argument to have type unsigned int.
In sub, the line cout << my_data[0]; should presumably be return my_data[0];
myArray::myArray(int initMax)
{
my_capacity = initMax;
my_data = new ARRAY_ELEMENT_TYPE[my_capacity]; //You missed this
}
In addition to missing your allocation in your current implementation, you are also dynamically allocating memory. A simple array type does not need to be allocated on the heap. The std::array collection does exactly what you are looking to do. I would urge you to look at its implementation for an example (if this is just an academic exercise). If this is for a production codebase, use what is already written and tested.
http://en.cppreference.com/w/cpp/container/array