Am I using dynamic memory right?

I have to write a code for class where the user inputs how many colors he wants to enter and then after that the user enters the different colors. I have to use an array of course to store all these colors in, but I just want to make sure that I'm using everything correctly before I continue on. It compiles and runs just fine, but I still feel like I'm using dynamic memory wrong here.

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
//Nathaniel Davidson Assignment 9
#include<iostream>
#include<algorithm>
#include<string>

using namespace std;



int main()
{
const int Capacity = 1000;
string* ColorArray;
ColorArray = new string [Capacity];
int Amount;

cout << "How many colors do you want to enter? ";
cin >> Amount;

for (int i = 0; i < Amount; ++i)
{
	cout << "Enter a color: " << endl;
	cin >> ColorArray[i];
}



delete[] ColorArray;
ColorArray = NULL;
}
You are creating 1000 elements in your ColorArray whatever the user enters, so I would say you are not using it correctly.

(I would use a vector in this case anyway).
I thought the delete[] ColorArray, deletes those unused elements though..
delete[] ColorArray deletes ALL memory taken in the array.

You would want to do something like
ColorArray = new string [Amount];
After the player enters in the Amount. Then delete it when you never need to use the data again.
Okay that makes so much more sense I'll give that a try, thank you!
Okay, I have one more question actually. I have to write a function that takes string pointers and length parameters as parameters. I've been searching all day about this as well and don't really get what my teacher want's by that. How do I add a string pointer? would it just be this

 
print_array(string ColorArray, int Amount)


or would I have to do something differently?
print_array(string *ColorArray, int Amount)
Thank you so much for helping me out with this! I'll post a reply if I run into any other errors
I'm getting this error now, and I cant figure out for the life of me what to do next.

a09.cpp: In function ‘int main()’:
a09.cpp:41:21: error: expected primary-expression before ‘*’ token
print_array(string *ColorArray, int Amount);
^
a09.cpp:41:34: error: expected primary-expression before ‘int’
print_array(string *ColorArray, int Amount);

this is my code right now if you need to see it.

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
//Nathaniel Davidson Assignment 9
#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

string print_array(string *ColorArray, int Amount)
{

}


int main()
{
const int Capacity = 1000;
string* ColorArray;
int Amount;


cout << "How many colors do you want to enter? ";
cin >> Amount;

ColorArray = new string [Amount];

for (int i = 0; i < Amount; ++i)
{
cout << "Enter a color: ";
cin >> ColorArray[i];
}

sort (ColorArray, ColorArray + Amount);

cout << endl;
cout << endl;
for (int i = 0; i < Amount; ++i)
{
cout << ColorArray[i] << endl;
}

print_array(string *ColorArray, int Amount);

delete[] ColorArray;
ColorArray = NULL;

return 0;
}
I fixed my error, I just deleted string * and the int in line 41. Derped hard on that one
Topic archived. No new replies allowed.