Classes and dynamic memory

Hi, I've just started learning C++. Right now I'm fine with classes, and I'm fine with dynamic memory, but I struggle to get things right when I'm dealing with both of them combined.

Right now I'm trying to make a dynamic kind of database in which the array shrinks or grows as data is inserted or deleted. So far I've only tried to make a "create entry" function, but even that doesn't seem to be working out for me. It compiles fine, but then when I try running it everything crashes and I'm not quite sure why.

Here's the function in question.
"member" is the name of the class.

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
void create_member(string firstName, string surName, char gender, int memberNo, member*& memberA)
{
	//create a new array one bigger than the old one
	//copy all the old contents into the new one
	//place the new member at the end
	//memberArray points to new array
	//delete old array

	member* tempArray;
	tempArray = new member[arraySize+1];

	
	for(int i = arraySize; i > 0; i--)
	{
		tempArray[arraySize] = memberA[arraySize];
	}
	

	for(int i = arraySize; i>= 0; i--)
	{
		delete [arraySize] memberA;
	}

	arraySize++;

	tempArray[arraySize] = member(firstName, surName, gender, memberNo);
	memberA = tempArray;

}


If anyone can help, I'd greatly appreciate it! Thanks!
Your loops are wrong. tempArray[arraySize] = memberA[arraySize] keeps setting the same index offset of your array. You need to use tempArray[i] = memberA[i]; Your delete is wrong. It should just be delete[] memberA; Line 24 is making arraySize a value that is now one position beyond the maximum index position in your array. Just remove that line. (Remember that arrays are zero-based, so int Array[5] would have indexes of 0 - 4). You can increment arraySize after you finish adding the newest member of the array as on line 26.

~psault
Last edited on
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
void create_member(string firstName, string surName, char gender, int memberNo, member*& memberA)
{
	//create a new array one bigger than the old one
	//copy all the old contents into the new one
	//place the new member at the end
	//memberArray points to new array
	//delete old array

	member* tempArray;
	tempArray = new member[arraySize+1];

	
	/*
          This isn't right
        */
         for(int i = arraySize; i > 0; i--)//Does this loop enouh times? what about index[0]
	{
		tempArray[arraySize] = memberA[arraySize]; //using the wrong variable as index
	}
	

	/*
            This is incorrect
            It is not the way to delete a dynamic array
             You are just trying to delete the same thing over and over again - 
        */
        for(int i = arraySize; i>= 0; i--)
	{
		delete [arraySize] memberA;
	}

	arraySize++;

	tempArray[arraySize] = member(firstName, surName, gender, memberNo);
	memberA = tempArray;

}




Also, is the member pointer being passed to the function a viable one??
Last edited on
Oops... the loops were careless of me.
And yes, I think the member pointer is

I've changed it a little bit. It still compiles but crashes though... not sure what I'm doing wrong. :(
Thanks already for the help 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
void create_member(string firstName, string surName, char gender, int memberNo, member*& memberA)
{
	//Create a new array that is one larger than the old one.
	member* tempArray;
	tempArray = new member[arraySize+1];

	//Copy all of the old values into the new ones.
	for(int i = arraySize; i >= 0; i--)
	{
		tempArray[i] = memberA[i];
	}
	
	//Delete the old array.
	for(int i = arraySize-1; i >= 0; i--)
	{
		delete [] memberA;
	}

	//Copy the new values into the end of the array.
	tempArray[arraySize] = member(firstName, surName, gender, memberNo);
	memberA = tempArray;
	
	//Increment array size.
	arraySize++;
}


Sorry to bother you all.
This bit is still wrong:
1
2
3
4
5
6
	//Delete the old array.
	for(int i = arraySize-1; i >= 0; i--)
	{
		delete [] memberA;
	}


it should just be delete [] memberA; just once NOT in a loop.

One thing I'm interested in knowing is how you declare and initialise the member*& memberA that you
pass to the function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void create_member(string firstName, string surName, char gender, int memberNo, member*& memberA)
{
	//Create a new array that is one larger than the old one.
	member* tempArray;
	tempArray = new member[arraySize+1];

	//Copy all of the old values into the new ones.
	for(int i = arraySize; i >= 0; i--)
	{
		tempArray[i] = memberA[i];
	}
	
	//Copy the new values into the end of the array.
	tempArray[arraySize] = member(firstName, surName, gender, memberNo);
	memberA = tempArray;
	
	//Increment array size.
	arraySize++;

	delete [] memberA;
}


Sigh... maybe I should start with something easier to begin with. :(

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
class member
{

public:
	//default constructor
	member()
	{

	}
	
	//constructor
	member(string firstNamec, string surNamec, char genderc, int memberNoc)
	{
		firstName = firstNamec;
		surName = surNamec;
		gender = genderc;
		memberNo = memberNoc;
	}
	
	//destructor
	/*
	~member()
	{
		delete firstName;
		delete surName;
		delete gender;
		delete memberNo;
	}
	*/
	//edit name
	void edit_name(string newFirstname, string newSurname)
	{
		firstName = newFirstname;
		surName = newSurname;
	}

	//edit gender
	void edit_gender(char newgender)
	{
		gender = newgender;
	}
	
	//edit memberNo
	void edit_memberNo(int newmemberno)
	{
		memberNo = newmemberno;

	}

	//friend functions

	friend void show_data(member*&);

private:
	string firstName;
	string surName;
	char gender;
	int memberNo;
Here is a small program using the class you provided.
I commented out the friend function declaration because I didn't want to write it.

I assume that arraySize variable is a Global one.

Note that we create the member* as an array - because we will be using it as an array - and
we have to delete it as an array using delete[] - otherwise we would have problems.
Note that although we create the array with 1 dummy entry we still initialise the arraySize
variable to 0.

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>

using namespace std;

//Global variable
int arraySize = 0;

class member
{

public:
    //default constructor
    member()
    {

    }

    //constructor
    member(string firstNamec, string surNamec, char genderc, int memberNoc)
    {
        firstName = firstNamec;
        surName = surNamec;
        gender = genderc;
        memberNo = memberNoc;
    }

    //destructor
    /*
    ~member()
    {
    delete firstName;
    delete surName;
    delete gender;
    delete memberNo;
    }
    */
    //edit name
    void edit_name(string newFirstname, string newSurname)
    {
        firstName = newFirstname;
        surName = newSurname;
    }

    //edit gender
    void edit_gender(char newgender)
    {
        gender = newgender;
    }

    //edit memberNo
    void edit_memberNo(int newmemberno)
    {
        memberNo = newmemberno;

    }

    //friend functions
    //friend void show_data(member*&); //*******************temporarily sommented out

private:
    string firstName;
    string surName;
    char gender;
    int memberNo;

};

void create_member(string firstName, string surName, char gender, int memberNo, member*& memberA)
{

    member* tempArray;
    tempArray = new member[arraySize+1];


    for(int i = arraySize-1; i >= 0; i--)
    {
        tempArray[i] = memberA[i]; 
    }

    delete [] memberA;

    cout << arraySize << endl;

    tempArray[arraySize] = member(firstName, surName, gender, memberNo);
    memberA = tempArray;

    arraySize++;

}

int  main ()
{

    //Create and initialise the  array
    //Note: We create an array of 1  in this case using the default constructor
    member *pmem = new member[1];
    pmem[0] = member();


    // Add  a few members
    create_member("Andy","St.Clair",'M', 220,pmem);
    create_member("Jean","Simmons",'F', 221,pmem);
    create_member("Paul","Jones",'M', 222,pmem);

    delete []pmem;

    return 0;
}
Thanks, it seems to be working now!
However, it doesn't work when I include the "delete []pmem;"
What do you mean by "doesn't work" ?
Um... I'm using Microsoft Visual C++ right now, and the command window just stops responding.
Did you set up pmem as an array to begin with ?
1
2
3
    member *pmem = new member[1];
    pmem[0] = member();


Yep.
Well, never mind, I'll try to get the basics down first. Thanks for all the help on the first question!
Topic archived. No new replies allowed.