P
P
Pavel2015-12-07 16:58:01
C++ / C#
Pavel, 2015-12-07 16:58:01

How to dynamically increase the size of an array?

I'm trying to create an analogue of an associative array, through 2 classes.
One class where the data itself is described:
data.h

#pragma once
#include "myString.h"
enum gender{MALE,FEMALE};
class data
{
  MyString surname, name, position;
  unsigned char age;
  int salary;
  gender myGen;
public:
  /*data();*/
  data(MyString nameIn = MyString("Anonymous"), MyString positionIn = MyString("No position"), unsigned char ageIn = 18, int salaryIn = 8000, gender genIn = MALE, MyString surnameIn = MyString("Anonymous"));
  friend ostream& operator<<(ostream& os, const data& inputData);
  data& operator=(const data& inputData);
  ~data();
  friend class BD;
};

data.cpp
#include "data.h"
using namespace std;
data::data(MyString nameIn, MyString positionIn, unsigned char ageIn, int salaryIn, gender genIn, MyString surnameIn)
{
  myGen = genIn;
  name = nameIn;
  surname = surnameIn;
  age = ageIn;
  salary = salaryIn;
  position = positionIn;
}

data::~data()
{
}

ostream& operator<<(ostream& os, const data& inputData){
  os << "Surname: " << inputData.surname << "\nName: " << inputData.name << "\nAge: " << static_cast<int>(inputData.age) << "\nSalary: " << inputData.salary << "\nPosition: " << inputData.position << endl;
  return os;
}
data& data::operator=(const data& inputData){
  this->age = inputData.age;
  this->myGen = inputData.myGen;
  this->name = inputData.name;
  this->position = inputData.position;
  this->salary = inputData.salary;
  return *this;
}

and the so-called wrapper class, where I override the data access function
BD.h
#pragma once
#include "data.h"
class BD
{
  data* man;
  int cap;
public:
  BD();
  data& operator[](const MyString& family);
  friend ostream& operator<<(ostream& os, const BD& inputBD);
  ~BD();
};

bd.cpp
#include "BD.h"


BD::BD()
{
  man = nullptr;
  //man = new data[1];
  cap = 0;
}


BD::~BD()
{
  delete[] man;
}

data& BD::operator[](const MyString& family){
  for (int i = 0; i < cap; i++){
    if (this->man[i].surname == family){
      return this->man[i];
    }
  }
  cap++;
  data* tmp = new data[cap];
  if (cap>1)
  {
    memcpy(tmp, this->man, sizeof(data)*(cap-1));
    delete[] this->man;
  }
  this->man = tmp;
  this->man[cap - 1].surname=family;
  return this->man[cap - 1];
}

ostream& operator<<(ostream& os, const BD& inputBD){
  for (int i = 0; i < inputBD.cap; i++)
  {
    os << "Person #" << inputBD.cap << "\n" << inputBD.man << std::endl;
  }
  return os;
}

The problem already arises when redefining the operator [], at the moment
memcpy(tmp, this->man, sizeof(data)*(cap-1));
    delete[] this->man;

I copy from the already current man element, the values ​​into the expanded array, but when I delete the old one, the data from the new one is also deleted. Why is this happening?
Other files are located at the github address

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
iv_k, 2015-12-07
@rusbaron

don't use memcpy in C++, make a copy constructor.

A
AtomKrieg, 2015-12-07
@AtomKrieg

Correct use of std::vector

L
LittleFatNinja, 2015-12-07
@LittleFatNinja

remember the pointer to the current start
and use realloc to allocate memory and point it to our pointer

O
Oleg Tsilyurik, 2015-12-07
@Olej

I'm trying to create an analogue of an associative array, through 2 classes.

What's wrong with std::map?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question