Problem with resizing arrays

I want to make a database with C++ well not a real one but a fake one
What I'm planing is there is a record class which is the data
A Field class holding an array of records
A table class holding an array of fields
And a database class holding an array of tables

but i seem to have problems resizing arrays
Here what i have so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <cstdlib>
#include <iostream>
#include <string>
#include "constants.h"

using namespace std;

int i,z;
class DBrecord
{
      public:
             int DBindex,DBdataType;
             string DBdata;
             DBrecord(){};//default constructor
             DBrecord(int index,int type,string data){//constructor
                          DBindex=index;//index of the record
                          DBdataType=type;//data type
                          DBdata=data;
             }
             /*DBrecord operator=(DBrecord source){
                      DBindex=source.DBindex;
                      DBdataType=source.DBdataType;
                      DBdata=source.DBdata;
                      return *this;
             }*/
             ~DBrecord(){};                     
};

class DBfield
{
      public:
             int DBnRecords;//number of records
             string DBFname,DBFcaption;//name and caption of the field
             DBrecord *DBrecords;//pointer for the array which will hold the records
             DBfield(){};//default constructor
             DBfield(string name,string caption){//constructor
                    DBFname=name;
                    DBFcaption=caption;
                    DBnRecords=0;
                    DBrecords=new DBrecord[5];
             }
             ~DBfield(){}//destructor
             int addRecord(int type,string data){
                 if(DBnRecords==0){
                    DBnRecords=1;
                    DBrecords[0].DBindex=1;
                    DBrecords[0].DBdataType=type;
                    DBrecords[0].DBdata=data;
                 }else{
                    if(DBnRecords<4){ 
                       DBrecords[DBnRecords].DBindex=DBrecords[DBnRecords-1].DBindex+1;
                       DBrecords[DBnRecords].DBdataType=type;
                       DBrecords[DBnRecords].DBdata=data;
                       DBnRecords++;
                       
                    }else{
                       DBrecord *temp=new DBrecord[DBnRecords+1];
                       for(i=0;i<DBnRecords;i++){
                           temp[i].DBindex=DBrecords[i].DBindex;
                           temp[i].DBdataType=DBrecords[i].DBdataType;
                           temp[i].DBdata=DBrecords[i].DBdata;
                           
                       }
                       temp[DBnRecords].DBindex=temp[DBnRecords-1].DBindex+1;
                       temp[DBnRecords].DBdataType=type;
                       temp[DBnRecords].DBdata=data;
                       DBnRecords++;
                       delete [] DBrecords;
                       DBrecord *DBrecords=new DBrecord[DBnRecords];
                       for(int i=0;i<DBnRecords;i++){
                           DBrecords[i].DBindex=temp[i].DBindex;
                           DBrecords[i].DBdataType=temp[i].DBdataType;
                           DBrecords[i].DBdata=temp[i].DBdata;
                       }
                       delete [] temp;
                    }
                 }
             }
};

int main(int argc, char *argv[])
{
    DBfield *test=new DBfield("Test","Test");
    test->addRecord(1,"Test");
    test->addRecord(1,"Test2");
    test->addRecord(1,"Test3");
    test->addRecord(1,"Test4");
    test->addRecord(1,"Test5");
   for(i=0;i<test->DBnRecords;i++){
        cout<<test->DBrecords[i].DBindex<<"."<<test->DBrecords[i].DBdata<<endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}


Everything works until i add another record
when i add test->addRecord(1,"Test6);
the index of the first record becomes some random number 24135135
and the last two also get random indexes
The difference is that the first has its data "Test" but the last dont
And the program crushes

Please can you look at my code and help me find the error
And could you help me with operator= for pointers so that i dont have to write
temp[i].DBindex=DBrecords[i].DBindex;
temp[i].DBdataType=DBrecords[i].DBdataType;
temp[i].DBdata=DBrecords[i].DBdata;

all the time.
Thanks in advance.

Sample run:
1
2
3
4
5
6
4722624.Test
2.Test2
3.Test3
4.Test4
5.Test5
12812649795.

and crush
Last edited on
Hi,

you have DBrecords declared as a member variable (line 34). In line 69 you declare a new (local) variable with the same name. Try DBrecords=new DBrecord[DBnRecords]; instead. You should also delete[] the member variable in the destructor.

Hope this works.
Kay
Consider using std::vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>

std::vector<DBRecord> DBRecords;

int addRecord(int type,string data)
{
    DBRecord record;

    record.DBdataType = type;
    record.DBdata= data;

    DBRecords.push_back(record);
}
Using a vector you can access your records like this:

1
2
3
4
5
for(size_t i = 0; i < DBRecords.size(); ++i)
{
    std::cout << "Type: " << DBRecords[i].type << std::cout;
    std::cout << "Data: " << DBRecords[i].data << std::cout;
}
Last edited on
@kmw Yes that was the problem thank you ^^

@Galik i want to use stuff that i learned at school because its a school project
I know it will be easier with vectors but for now ill stick with this
Topic archived. No new replies allowed.