How useful can the "new" keyword be in high level programming?

From my understanding, every time I use the new keyword, all it does is just create a new array and assigns the address of the array to the pointer. But my question is.. what's the point? wouldn't it be easier to just make an array without "new"?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main ()
{
   string *pointer;

   pointer = new string [5];

   for (int n = 0; n <= 4; n++)
        {
        cin >> pointer[n];
        }

   cout << "Here is what you stored in the dynamic array" << "\n";

    for (int m = 0; m <= 4; m++)
        {
        cout << pointer[m] << " ";
        }

}
Last edited on
Foreword: I'm not suggesting the following code follows best practices in C++. I did not cover non-array uses of new.

Things you can only do with new:
1
2
3
4
5
6
7
8
9
10
11
12
//Return an array:
std::string *new_array(){
    std::string *ret = new std::string[10];
    ret[0] = "foo";
    //...
    return ret;
}

//Allocate an variable-sized array (see below):
std::string *more_strings(int n){
    return new std::string[n];
}

A variable-sized array is an array whose size depends on a value that's only available when the program is running, as opposed to being available while the program is being compiled. It's not an array that can change sizes. No such thing exists.
Last edited on
std::strings definitely call new somewhere in their class, we just don't see it. And we as the user of the string class don't need to see it.

With the whole standard library in your arsenal, it cuts out a lot of places where directly using new would be needed.

One thing that the standard library doesn't have is a general "tree" class, most likely because tree uses are too specific (I think map in C++ is implemented in trees, not sure though). If you wrote your own tree class, using nodes, you'd be calling new a lot when making those nodes.

The rule of thumb is that there's mostly likely no need to call new if you're going to be deleting the allocated memory in the same scope that you're new'ing it. (Except for raw arrays, which you should be using std::vectors anyway)
Last edited on
Something else you can do with new, use a function to create and delete objects:

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
#include <iostream>
#include <string>

class Obj
{
public:
		Obj() { ObjID = OID; ++OID; }
		~Obj() { --OID; }
		static int OID;
		int GetID() { return ObjID; }
private:
	int ObjID;
};

int Obj::OID = 0;


Obj* CreateObj()
{
	return new Obj;
}

void DeleteObj(Obj** o)
{
	delete *o;
	*o = 0;
}

#define MAGIC 100000

int main()
{
	float p;
	p = 0;
	
	Obj *ObjArray[MAGIC];
	for(int i = 0; i < MAGIC; i++)
	{
		p = i+1;
		ObjArray[i] = CreateObj();
		std::cout << "\rObj ID = " << ObjArray[i]->GetID() << "\tAddress = " << ObjArray[i] << "\t Percent: " << (p/MAGIC) * 100;
	}
	std::cout << "\n";
	for(int i = MAGIC-1; i > -1; i--)
	{
		p = i+1;
		std::cout << "\rDeleting Obj @" << ObjArray[i] << "\t Percent: " << (p/MAGIC) * 100;
		DeleteObj(&ObjArray[i]);
	}
	return 0;
}


I did this recently ( a month or so ) to learn how I could create objects with unknown arguments at run time. The idea behind this is that the function returns a pointer of type object which is allocated on the heap via new. The delete function takes an obj address as an argument to free that obj's memory.

Keep an eye on the addresses, you can use them to see how big your object is, and see how it is allocated contiguously.
Topic archived. No new replies allowed.