I am trying to create a template which will add an element to the end of an array (after resizing). I want to specialize it so that if the type is char*, it will include a null byte at the end.
Here is my main:
int main()
{
int* arrI = nullptr;
arrI = insertAtend(arrI, 0, 1); //1
std::cout << arrI[0];
delete[] arrI;
char* arrC = nullptr;
arrC = insertAtend(arrC, 0, 'a'); //a
std::cout << arrC;
delete[] arrC;
return 0;
}
And here are the templates:
template<typename T>
T* insertAtend(T* arr, int size, const T toAdd)
{
T* temp = new T[++size];
if (arr)
{
for (int i = 0; i < size; i++)
{
temp[i] = arr[i];
}
delete[] arr;
}
arr = new T[size + 1];
if (temp)
{
for (int i = 0; i < size; i++)
{
arr[i] = temp[i];
}
}
delete[] temp;
arr[size - 1] = toAdd;
return arr;
}
template<>
char* insertAtend<char*>(char* a, int s, const char* d)
{
return a;
}
Obviously without logic, but I am getting an error:
C2912 "Error C2912 explicit specialization 'char *insertAtend<char>(char *,int,const char *)' is not a specialization of a function template"
You're simply confusing the base type (char) with the pointer type (char *).
Change your specialization to this:
template<>
char* insertAtend<char>(char* a, int s, const char d)
{
return a;
}
Related
There is something wrong with the expand() method.
#include <iostream>
struct obj
{
int fInt;
float fFloat;
};
template <typename T>
class dynamicArray {
private:
T* myArray;
int elements;
int size;
public:
dynamicArray();
void add(T dane);
void expand();
void init(int el);
T get(int index);
};
template <typename T>
dynamicArray<T>::dynamicArray()
{
this->size = 1;
this->elements = 0;
this->myArray = new T[this->size];
init(this->elements);
}
template <typename T>
T dynamicArray<T>::get(int index)
{
return this->myArray[index];
}
template <typename T>
void dynamicArray<T>::init(int el)
{
for (size_t i = el; i < this->size; i++)
{
this->myArray[this->elements] = nullptr;
}
}
template <typename T>
void dynamicArray<T>::expand()
{
this->size *= 2;
T* tempArr = new T[this->size];
for (int i = 0; i < this->elements; i++)
{
tempArr[i] = this->myArray[i];
//tempArr[i] = new T(*this->myArray[i]);
}
for (int i = 0; i < this->elements; i++)
{
delete this->myArray[i];
}
delete this->myArray;
this->myArray = tempArr;
init(this->elements);
}
template <typename T>
void dynamicArray<T>::add(T dane)
{
if (this->size == this->elements)
this->expand();
this->myArray[this->elements] = dane;
this->elements++;
}
int main()
{
dynamicArray<obj*>* arr = new dynamicArray<obj*>();
obj* so = new obj;
so->fInt = 2;
so->fFloat = 2;
arr->add(so);
obj* so2 = new obj;
so2->fInt = 3;
so2->fFloat = 3;
arr->add(so2);
so = arr->get(0);
so2 = arr->get(1);
std::cout << so->fInt << std::endl;
std::cout << so->fInt;
}
In this for loop I would like to assign to temporary array elements of myArray but they are not the copies
for (int i = 0; i < this->elements; i++)
{
tempArr[i] = this->myArray[i];
//tempArr[i] = new T(*this->myArray[i]);
}
and when I delete them they disappear from tempArr too.
for (int i = 0; i < this->elements; i++)
{
delete this->myArray[i];
}
I tried couple things but I can't find the solution.
tempArr[i] = new T(*this->myArray[i]);
I am not sure if this is the right track, but it's giving me a
'initializing': cannot convert from 'obj' to 'T'
and
'=' cannot convert from 'T*' to 'T'
You got yourself confused, you have a pointer to an array of T, not a pointer to an array of T*, but some of your code is written as if you had the latter.
This (in expand)
for (int i = 0; i < this->elements; i++)
{
delete this->myArray[i];
}
delete this->myArray;
should simply be
delete[] this->myArray;
You can't delete individual array elements because they are not (necessarily) pointers. And delete[] this->myArray; will invoke the destructor for all elements in your array.
And init can simply be deleted, because again it assumes that your array elements are pointers.
Try writing some code with dynamicArray<int>, so that your T is definitely not a pointer. That will find all the places where you've incorrectly assumed that T is a pointer (in case I've missed any).
While creating a custom class for STL Multimap, I came across an unintended behaviour where dynamic arrays created by new operator are not of the size between []. In the following code, in a.Set(3, 'c') the arrays stored in newKey and newSize are of size one, when they should have size two. Using the debugger shows that in that lines index is equal to 1, so size should be two. Program does not produce any exception but it also does not output the expected result c.
As clarification, using the debugger shows that the problem occurs while setting the value at index 1 in newKey, newSize, newValue. It does not throw any kind of exception, but does not change any value either.
template<typename T>
void Copy(T const* _source, T* _destiny, unsigned long _size)
{
for (unsigned long i = 0; i < _size; i++)
{
_destiny[i] = _source[i];
}
}
template<typename T>
void CopyNew(T const* _source, T* _destiny, unsigned long _size)
{
T* target = new T[_size];
for (unsigned long i = 0; i < _size; i++)
{
target[i] = _source[i];
}
_destiny = target;
}
template<typename T1, typename T2>
class Multimap
{
public:
Multimap() {}
unsigned long Get(T1 const& _key, T2** _return)
{
for (unsigned long i = 0; i < this->keySize_; i++)
{
if (_key == this->key_[i])
{
CopyNew<T2>(this->value_[i], *_return, this->valueSize_[i]);
return i;
}
}
*_return = 0;
return this->keySize_;
}
unsigned long Get(T1 const& _key)
{
for (unsigned long i = 0; i < this->keySize_; i++)
{
if (_key == this->key_[i])
{
return i;
}
}
return this->keySize_;
}
int Set(T1 const& _key, T2 const& _value)
{
T2* target;
unsigned long index = this->Get(_key, &target);
if (target == 0)
{
T1* newKey = new T1[index + 1];
unsigned long* newSize = new unsigned long[index + 1];
T2** newValue = new T2*[this->keySize_ + 1];
if (this->keySize_ != 0)
{
Copy(this->key_, newKey, index);
delete[] this->key_;
Copy(this->valueSize_, newSize, index);
for (unsigned long i = 0; i < this->keySize_; i++)
{
newValue[i] = new T2[this->valueSize_[i]];
Copy(this->value_[i], newValue[i], this->valueSize_[i]);
delete[] this->value_[i];
}
delete[] this->valueSize_;
}
newKey[index] = _key;
newSize[index] = 0;
this->key_ = newKey;
this->valueSize_ = newSize;
this->value_ = newValue;
this->keySize_++;
}
unsigned long newSize = this->valueSize_[index]+1;
T2* newValue = new T2[newSize];
Copy(this->value_[index], newValue, newSize-1);
newValue[newSize-1] = _value;
this->valueSize_[index] = newSize;
this->value_[index] = newValue;
return newSize;
}
unsigned int GetSize()
{
return this->keySize_;
}
protected:
unsigned long keySize_ = 0;
unsigned long* valueSize_ = 0;
T1* key_ = 0;
T2** value_ = 0;
};
int main()
{
Multimap<int, char> a;
a.Set(2, 'b');
a.Set(3, 'c');
char* b;
a.Get(3, &b);
std::cout << b[0];
}
CopyNew argument _destiny should be T*& (as pointed out by WhozCraig in the comments). Otherwise, the function is changing the argument but not the variable passed to the function. In order to change the variable, you have to de-reference the argument, so its type has to be either a pointer or a reference to the variable type. Since the type of the variable is T*, the argument type should be T** or T*&
I'm trying to work with dynamic arrays. When I try to overload the "=" operator it does not work. When debugging the file it doesn't execute the void function to overload the operator.
#include <iostream>
using namespace std;
class cppArray {
public:
cppArray(int size);
~cppArray();
int read(int index);
void write(int content, int index);
void operator=(cppArray& s);
int search(int target);
int size();
private:
int* myArray;
int arraySize;
};
cppArray::cppArray(int size) {
myArray = new int[size];
arraySize = size;
}
//delete the memory space assigned to myArray
cppArray::~cppArray() {
delete[] myArray;
myArray = 0;
}
int cppArray::read(int index) {
if (index < arraySize) {
return myArray[index];
}
else {
cout << "Out of range" << endl;
exit(1);
}
}
Here I'm trying to copy the content of the original array to an auxiliar one, and then redefine the size of the original array so I can add more content to the original array
void cppArray::write(int content, int index) {
if (index < arraySize) {
myArray[index] = content;
}
else {
cppArray auxArray(arraySize);
auxArray.myArray = myArray;
delete[] myArray;
arraySize = index + 1;
myArray = new int[arraySize];
myArray = auxArray.myArray;
myArray[index] = content;
}
}
I'm pretty sure this is wrong, but I can't figure out a way to overload it correctly
void cppArray::operator=(cppArray& s) {
delete[] s.myArray;
s.myArray = new int[arraySize];
for (int i = 0; i < arraySize; i++)
{
myArray[i] = s.myArray[i];
}
}
int cppArray::size() {
return arraySize;
}
int main(int argc, char** argv) {
cppArray dsArray(3);
dsArray.write(1, 0);
dsArray.write(2, 1);
dsArray.write(3, 2);
dsArray.write(4, 3);
for (int i = 0; i < dsArray.size(); i++) {
cout << dsArray.read(i) << "\t";
}
cout << endl;
return 0;
}```
Your implementation is almost correct, but you delete the wrong array. You should only modify *this object, and not s. Also, you should follow conventions, or people will be very surprised when using your class.
Here's corrected version:
//return *this - a very expected convention
cppArray& cppArray::operator=(const cppArray& s) {
// Make sure s is not modified ^^^^
if (this == &s) {
return *this; //protection against self writing, things would get bad if you did that
}
arraySize = s.arraySize; //copy size first
delete[] myArray; //delete array from this object
myArray = new int[arraySize]; //recreate array
for (int i = 0; i < arraySize; i++)
{
myArray[i] = s.myArray[i]; //no changes here
}
return *this;
}
So I wrote this C++ Class for storing an Array:
#include<bits/stdc++.h>
using namespace std;
class Array
{
private:
int size;
int *A = new int[size];
int length;
public:
Array(int arr[],int sz = 10)
{
length = sz;
size = 10;
for(int i=0;i<length;i++)
A[i] = arr[i];
}
~Array()
{
delete []A;
}
void Display();
void Insert(int index, int x);
};
void Array::Display()
{//code}
void Array::Insert(int index, int x)
{//code}
int main()
{
int a[3] = {1,2,3};
Array arr(a, 3);
arr.Display();
arr.Insert(1,4);
arr.Display();
}
This works fine. But in the main() function:
int main()
{
int a[3] = {1,2,3};
Array arr(a, 3);
arr.Display();
}
I am passing the pointer to the array a in the Class constructor.
Is there a way to pass the array like this?
Array arr({1,2,3}, 3);
You probably need to use something like this. But please note this code is still not optimal and it is usually much better to use std::vector or in some cases std::array.
#include <initializer_list>
class Array
{
private:
int *m_data = nullptr;
int m_size = 0;
int m_capacity = 0;
public:
Array(int arr[], int sz = 10) {
m_size = sz;
m_capacity = sz;
m_data = new int[m_capacity];
for(int i=0; i < m_size; i++) {
m_data[i] = arr[i];
}
}
Array(const std::initializer_list<int> &il) {
m_size = il.size();
m_capacity = il.size();
m_data = new int[m_capacity];
auto it = il.begin();
for (int i = 0; i < m_size; ++i) {
m_data[i] = *it;
++it;
}
}
~Array() {
delete [] m_data;
}
int size() const { return m_size; }
bool empty() const { return m_size == 0; }
int& operator[](int id) { return m_data[id]; }
int operator[](int id) const { return m_data[id]; }
};
#include <cstdio> // for printf
int main() {
Array arr({1,2,3});
printf(" %i \n",arr.size());
return 0;
}
I tried to make a dynamic array that adds elements at the beginning of the array. It works just fine with int, but when i try double, it gives me the error Process returned -1073741819 (0xC0000005). Debugging it works perfectly and all are in range, but when i run it crashes.
This is the header:
#define DYNAMICARRAY_H
#include <cstddef>
template <class T>
class DynamicArray
{
public:
DynamicArray();
DynamicArray(size_t);
DynamicArray(const DynamicArray&);
virtual ~DynamicArray();
DynamicArray& operator=(const DynamicArray&);
T &operator[] (size_t);
void add(T element);
size_t getCapacity();
size_t getLength();
bool isEmpty();
void print();
void resizeArr();
protected:
private:
T *arr;
size_t capacity;
size_t length;
};
#endif // DYNAMICARRAY_H
This is the .cpp
#include "DynamicArray.h"
#include <iostream>
template <class T>
DynamicArray<T>::DynamicArray()
{
length = 0;
capacity = 1;
arr = (T*)malloc(capacity * sizeof(arr));
if(!arr){
throw std::bad_alloc();
}
}
template <class T>
DynamicArray<T>::DynamicArray(size_t newSize)
{
length = 0;
capacity = newSize;
arr = (T*)malloc(capacity * sizeof(arr));
if(!arr){
throw std::bad_alloc();
}
}
template <class T>
DynamicArray<T>::~DynamicArray()
{
delete[] arr;
delete arr;
delete &capacity;
delete &length;
}
template <class T>
DynamicArray<T>::DynamicArray(const DynamicArray& other)
{
length = other.length;
capacity = other.capacity;
arr = (T*)malloc(capacity * sizeof(arr));
if(!arr){
throw std::bad_alloc();
}
for(size_t i = 0; i < length; i++){
arr[i] = other.arr[i];
}
}
template <class T>
DynamicArray<T>& DynamicArray<T>::operator=(const DynamicArray& rhs)
{
if (this == &rhs){
return *this;
}
delete[] arr;
length = rhs.length;
capacity = rhs.capacity;
arr = (T*)malloc(capacity * sizeof(arr));
if(!arr){
throw std::bad_alloc();
}
for(size_t i = 0; i < length; i++){
arr[i] = rhs.arr[i];
}
return *this;
}
template <class T>
size_t DynamicArray<T>::getLength()
{
return length;
}
template <class T>
size_t DynamicArray<T>::getCapacity()
{
return capacity;
}
template <class T>
bool DynamicArray<T>::isEmpty(){
return length == 0;
}
template <class T>
T &DynamicArray<T>::operator[] (size_t index)
{
if(index >= length){
std::cout << "Array index out of bounds" << "\n";
exit(0);
}
return arr[index];
}
template <class T>
void DynamicArray<T>::add(T element)
{
if(length >= capacity){
resizeArr();
}
if(!isEmpty()){
for(size_t i = length; i > 0; i--){
arr[i] = arr[i-1];
}
}
arr[0] = element;
length ++;
}
template <class T>
void DynamicArray<T>::print()
{
if(length == 0){
std::cout << "The array is empty!";
}
else{
for(size_t i = 0; i < length; i++){
std::cout << arr[i] << " ";
}
}
std::cout << "\n";
}
template <class T>
void DynamicArray<T>::resizeArr(){
size_t newCapacity = capacity * 2;
T *arr2 = (T*)realloc(arr, newCapacity * sizeof(arr));
if(!arr2){
std::cout << "Bad memory allocation";
throw std::bad_alloc();
}
arr = arr2;
capacity = newCapacity;
}
template class DynamicArray<double>;
template class DynamicArray<int>;
And the main
#include "DynamicArray.h"
#include <cstdlib>
#include <time.h>
#include <cstddef>
using namespace std;
int main()
{
srand (time(NULL));
DynamicArray<int> d(5);
int randomInt;
for(size_t i = 0; i < d.getCapacity(); i++){
randomInt = rand()% 100;
d.add(randomInt);
}
d.print();
d.add(5);
d.add(55);
d.add(3);
d.add(33);
d.add(37);
d.print();
delete &d;
cout << "\n\n";
DynamicArray<double> d2(5);
double randomDouble;
for(size_t i = 0; i < d2.getCapacity() - 3; i++){
//randomDouble = (double)rand() / ((double)RAND_MAX);
randomDouble = 0.5f;
d2.add(randomDouble);
d2.print();
}
d2.print();
return 0;
}
It crashes at the second for in the main, I've tried to make sure everything it set correctly, but still won't work:
for(size_t i = 0; i < d2.getCapacity() - 3; i++){
//randomDouble = (double)rand() / ((double)RAND_MAX);
randomDouble = 0.5f;
d2.add(randomDouble);
d2.print();
}
arr = (T*)malloc(capacity * sizeof(arr));
delete[] arr;
delete arr;
delete &capacity;
delete &length;
delete &d;
You pass pointers to delete[] and delete that were not returned from new[] or new respectively. Behaviour of the program is undefined. Relevant language rule:
[expr.delete]
... In a single-object delete expression, the value of the operand of delete may be a null pointer value, a pointer to a non-array object created by a previous new-expression, or a pointer to a subobject representing a base class of such an object. If not, the behavior is undefined. In an array delete expression, the value of the operand of delete may be a null pointer value or a pointer value that resulted from a previous array new-expression.
If not, the behavior is undefined. ...
Every delete expression of your program violate this rule.
To fix this, you must consistently use free on memory allocated with malloc, realloc or strdup, and use delete with pointers allocated with new, delete[] with pointers allocated with new[], absolutely no deallocation functions with pointers to non-dynamic objects such as member variables or objects with automatic or static storage duration, (or invalid pointers).
arr = (T*)malloc(capacity * sizeof(arr));
sizeof(arr) is the same as sizeof(T*) which isn't what you want, I suppose.
This should rather be sizeof(*arr) or sizeof(T).
Apart from that, don't mix malloc/delete or new/free, as others already pointed out.
If you're doing C, use malloc/free, and if you're in C++ use new/delete.
Using new would change the above to
arr = new T[capacity];
avoiding the whole sizing altogether.