I am trying to initiate an object with an array. Is there a way to do it with pointers or should i find another way to do this.
EDIT: I want to write this code with dynamic memory allocation, I know vector is better way to solve this.
#include <iostream>
template <class t>
class die {
private:
int sideCount;
t* valueOfSides;
public:
die(int side, t arr[]) {
sideCount = side;
valueOfSides = (t*)malloc(side * sizeof(t));
for (int counter; counter < side; counter++) {
valueOfSides[counter] = val[counter];
}
}
~die() {
free(valueOfSides);
}
};
int main() {
die<int> sixsided(6, {1,2,3,4,5,6});
}
The right ways to do this would be
std::vector<t> valueOfSides;
template<size_t len> die(t (&arr)[len])
: valueOfSides(std::begin(arr), std::end(arr))
{}
or
std::vector<t> valueOfSides;
die(std::initializer_list<t> arr) : valueOfSides(arr) {}
I think. Though really, the best answer is
std::vector<t> valueOfSides;
die(std::vector<t> arr) : valueOfSides(std::move(arr)) {}
One should never use raw pointers to own memory in C++, and virtually never use new or malloc. As it is, you have undefined behavior in your code because of misusing malloc.
If you're absolutely insane, or doing homework, it can be done with raw pointers, though I doubt I can get it entirely right without tests and a compiler.
template<class t>
class die {
private:
int sideCount;
t* valueOfSides;
public:
die(int side, t* arr) {
sideCount = 0;
std::size_t buffer_size = sizeof(t)*side;
char* buffer;
try {
buffer = new char[side];
valueOfSides = reinterpret_cast<t*>(buffer);
for(int i=0; i<side; i++) {
new(valueOfSides+i)t(arr[i]);
sideCount++;
}
} catch(...) {
for(int i=sideCount; i>=0; i--)
(valueOfSides+i)->~t();
delete[]buffer;
throw;
}
}
die& operator=(die&& rhs) {
sideCount = rhs.sideCount;
valueOfSides = rhs.valueOfSides;
rhs.valueOfSides = nullptr;
rhs.sideCount = 0;
return *this;
}
//die& operator=(const die& rhs) not shown because its super hard.
~die() {
for(int i=sideCount; i>=0; i--)
(valueOfSides+i)->~t();
delete[]reinterpret_cast<char*>(valueOfSides);
}
};
As we've said before, getting this stuff right is crazy hard. Use a std::vector.
Use std::vector.
#include <iostream>
#include <initalizer_list>
#include <vector>
template<class T>
class die {
public:
die() = default;
die(std::initializer_list<T> list)
: sides{list}
{ /* DO NOTHING */ }
private:
std::vector<T> sides{};
};
int main() {
die<int> sixsided({1,2,3,4,5,6});
}
One way you can do this, using more of a C technique, is a variable argument list:
#include <cstdarg>
#include <iostream>
template <class t>
class die {
private:
int sideCount;
t* valueOfSides;
public:
die(int side, ...) {
sideCount = side;
valueOfSides = new t[side];
va_list args;
va_start(args, side);
for (int counter = 0; counter < side; counter++) {
valueOfSides[counter] = va_arg(args, t);
}
va_end(args);
}
~die() {
delete[] valueOfSides;
}
};
int main() {
die<int> sixsided(6, 1,2,3,4,5,6);
}
Rather than passing an array, you're passing the parameters individually (i.e. no need for a temporary array) and using a va_list to access them.
Also, the calls to malloc and free were replaced with new and delete which is the C++ way of allocating and deallocating memory.
The C++ solution:
template <class t>
class die {
private:
int sideCount;
t* valueOfSides;
public:
die(int side, t arr[]) {
sideCount = side;
valueOfSides = new T[side]
for (int counter = 0; counter < side; counter++) { //always initialize variables
valueOfSides[i] = arr[i];
}
}
~die() {
delete[] valueOfSides;
}
};
int main() {
int arr[6] = { 1,2,3,4,5,6 };
die<int> sixsided(6, arr);
}
The new operator is like malloc and the delete and delete[] operators are like free. They are dynamic allocators.
C solution:
template <class t>
class die {
private:
int sideCount;
t* valueOfSides;
public:
die(int side, t arr[]) {
sideCount = side;
valueOfSides = (t*)malloc(side * sizeof(t));
for (int counter = 0; counter < side; counter++) { //always initialize variables
valueOfSides[i] = arr[i];
}
}
~die() {
free(valueOfSides);
}
};
int main() {
int arr[6] = { 1,2,3,4,5,6 };
die<int> sixsided(6, arr);
}
Note: in C the <iostream> header will not work, this is C++ only.
There are other containers, namely std::vector, that can work, but this is the solution for your answer.
I'm trying to overload the [] operator in a templated dynamic array, however it doesn't seem to be doing anything?
I created a templated dynamic array for school, I've tried separating the overload to outside the class.
The DynArray.h
template <typename T>
class DynArray
{
public:
//The constructor initialises the size of 10 and m_Data to nullptr
DynArray(void)
{
m_AllocatedSize = 10;
m_Data = nullptr;
}
//deletes m_Data
~DynArray()
{
delete[] m_Data;
m_Data = nullptr;
}
T* operator [] (int index)
{
return m_Data[index];
}
//creates the array and sets all values to 0
T* CreateArray(void)
{
m_Data = new T[m_AllocatedSize];
m_UsedElements = 0;
for (int i = 0; i < m_AllocatedSize; ++i)
{
m_Data[i] = NULL;
}
return m_Data;
}
private:
bool Compare(T a, T b)
{
if (a > b)
return true;
return false;
}
T* m_Data;
T* m_newData;
int m_AllocatedSize;
int m_UsedElements;
};
Main.cpp
#include <iostream>
#include "DynArray.h"
int main()
{
DynArray<int>* myArray = new DynArray<int>;
//runs the create function
myArray->CreateArray();
int test = myArray[2];
delete myArray;
return 0;
}
I expected the overload to return in this case the int at m_Data[2], however it doesn't seem to overload the [] at all instead says no suitable conversion from DynArray<int> to int.
You are returning a pointer which is not what you want. You should do like this:
T& operator [] (const int& index)
{
return m_Data[index];
}
Also myArray is a pointer you have to dereference it before using.
int test = (*myArray)[2];
It's better to not to use pointer:
int main()// suggested by #user4581301
{
DynArray<int> myArray;
//runs the create function
myArray.CreateArray();
int test = myArray[2];
return 0;
}
There is no reason for using pointers here.
Instead of new and delete for dynamic allocation it is better to use smart pointers.
There is also one issue here, you are not chacking the range and what if theindex was for example a negative number.
I am learning dynamic memory allocation. I have the following class where 'class A' should own a dynamically allocated array in the constructor. Also the copy constructor and destructor should be modified. This is what I have so far...
#include <iostream>
#ifndef A_HH
#define A_HH
#include "B.hh"
class A {
public:
A() { B *array = new B[12];}
A(const A&) { /* Do not know what to put here..*/ }
~A() { delete[] array;}
private:
//B array[12] ; <- This is the array that I have to modify so it becomes dynamic.
B *array;
} ;
#endif
For starters your default constructor there is overriding the member variable "array" with a local variable of the same type, so you want the default constructor to look like this:
A() { array = new B[12]; }
Then the copy constructor presumably needs to deep copy the array over, but with a simple array you can't tell the arraysize at runtime. You need to either move to a smarter container (e.g. stl::vector) or store the size, but a naive copy constructor would look like this:
A(const A& other)
{
array = new B[12];
for(int i=0;i<12;i++)
{
array[i] = other.array[i];
}
}
Check out this code:
#include <iostream>
using namespace std;
class MyArray{
public:
int* array;
int length;
MyArray(int length){ // constructor which takes length of array as argument
this->length = length;
this->array = new int[this->length];
for(int i=0; i<this->length; i++){
this->array[i] = i;
}
}
MyArray(const MyArray &obj){
this->length = obj.length;
this->array = new int[this->length];
for(int i=0; i<this->length; i++)
{
this->array[i] = obj.array[i];
}
}
~MyArray(){ // destructor
delete[] this->array;
}
void print(){ // test method for printing the array
for(int i=0; i<this->length; i++){
cout << this->array[i] << endl;
}
cout << endl;
}
private:
protected:
};
int main()
{
MyArray *array = new MyArray(10);
MyArray *array2 = new MyArray(*array); // call copy constructor
array->print();
array2->print();
delete array;
delete array2;
}
when I try to enter elements through getData() function, I get error segmentation fault(core dumped).
I am not able to enter elements into array. I am not able to understand why i cannot access the memory location returned by void* into Class type.
size of *parr is 4 bytes, still I cannot data into that memory location???
#include<iostream>
#include<cstdlib>
using namespace std;
class Array
{
int *arr;
public:
void* operator new(size_t size)
{
void *parr=::new int[size];
//cout<<sizeof(parr);
return parr;
}
void operator delete(void *parr)
{
::delete (int*)parr;
}
void getData()
{
cout<<"Enter the elements";
for(int i=0;i<5;i++)
cin>>arr[i];
}
void showData()
{
cout<<"Array is:\n";
for(int i=0;i<5;i++)
cout<<arr[i];
}
};
int main()
{
Array *A=new Array;
A->getData();
//A->showData();
(*A).showData();
delete A;
return 0;
}
What you probably meant to do instead of overloading the new and delete operators for class Array, is to write appropriate constructor and destructor functions:
class Array {
int *arr;
public:
Array(size_t size) : arr(new int[size]) {
}
~Array() {
delete arr;
}
// ...
};
I am getting passing 'const Array' as 'this' argument of 'int& Array::operator[](int)' discards qualifiers when compiling under Dev C++. If I compile using gcc on cygwin I've got a few error messages of this type:
error: extra qualification ‘Array::’ on member ‘Array’ [-fpermissive]
Can you tell me what is the reason for the error? I've spent a lot of time on this code and still can't make it work correctly.
#include<iostream>
using namespace std;
class Array
{
int *m_ptr;
int m_size;
public:
Array(int sz)
{
cout<<"constructor\n";
m_size = sz;
m_ptr = new int[sz];
}
~Array()
{
cout<<"Delete\n";
delete[] m_ptr;
}
int& operator[] (int j)
{
cout<<"Operation []\n";
return m_ptr[j];
}
void Array::copy(const Array& ar)
{
m_size = ar.m_size;
m_ptr = new int[m_size];
int *ptr = ar.m_ptr;
int j;
for(j = 0;j < m_size; j++)
m_ptr[j] = ptr[j];
}
Array::Array(const Array& ar)
{
copy(ar);
}
void Array::print(const Array& ar)
{
int i;
int len = ar.m_size;
for(i = 0;i < len;i++)
cout<<ar[i]<<" ";
cout<<endl;
}
};
int main()
{
Array a1(10);
Array a2(5);
int i;
for(i = 0;i < 10;i++)
{
a1[i] = 1;
if(i < 5) a2[i] = 2;
}
print(a1);
return 0;
}
Furthermore, the book I am reading also has the function
Array& operator= (const Array& ar)
{
delete m_ptr;
copy(ar);
return *this;
}
I do not understand why do we need to use this function.
Just change
void Array::copy(const Array& ar)
to
void copy(const Array& ar)
The class scope qualifier is only needed for function definitions appearing outside of the class declaration.
Issue 1
You don't need the Array:: in
void Array::copy(const Array& ar) { ... }
when the function is defined inside the class definition. It can simply be
void copy(const Array& ar) { ... }
You need the Array:: part only when the function is defined outside the class definition.
Issue 2
In print, you have the line:
cout<<ar[i]<<" ";
Since ar is a const object and the operator[]() function is not a const member function, the compiler doesn't like it. What you should do is provide two overloads of the operator[] function -- one for const objects and one for non-const objects.
int& operator[] (int j)
{
return m_ptr[j];
}
int operator[] (int j) const
{
return m_ptr[j];
}
Then, you can use the array operator to access the elements of a const object as well as access and modify the elements of a non-const object.