new without delete?

How come this code does not require a delete or delete [] after the instantiation via a pointer and the keyword new was used (line 21, 26)? Thanks

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
#include <iostream>
#include <string>
using namespace std;

class Human
{
public:
	string name;
	int age;

	void IntroduceSelf()
	{
		cout << "I am " + name << " and am ";
		cout << age << " years old" << endl;
	}
};

int main()
{
	// An object of class Human with attribute name as "Adam"
	Human* firstMan = new Human;
	firstMan->name = "Adam";
	firstMan->age = 30;

	// An object of class Human with attribute name as "Eve"
	Human* firstWoman = new Human;
	firstWoman->name = "Eve";
	firstWoman->age = 28;

	firstMan->IntroduceSelf();
	firstWoman->IntroduceSelf();
}
It's not required to say delete, but it is advised. On modern desktop systems, memory not explicitly released is reclaimed by the operating system on process exit (when your program ends).

It is very poor practice to use the new keyword at all in this situation.

It would be incorrect to delete[] an object obtained with new (and not new[]).
Last edited on
That helps a lot. Thanks
It is very poor practice to use the new keyword at all in this situation.
But If I take it away (line 21, 26) it gives me an error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	// An object of class Human with attribute name as "Adam"
	Human* firstMan; // gives me an error
	firstMan->name = "Adam";
	firstMan->age = 30;

	// An object of class Human with attribute name as "Eve"
	Human* firstWoman; // gives me an error
	firstWoman->name = "Eve";
	firstWoman->age = 28;

	firstMan->IntroduceSelf();
	firstWoman->IntroduceSelf();
}


What would be a better solution? Thanks
Why exactly do you need a pointer at all?

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
#include <iostream>
#include <string>
using namespace std;

class Human
{
public:
	string name;
	int age;

	void IntroduceSelf()
	{
		cout << "I am " + name << " and am ";
		cout << age << " years old" << endl;
	}
};

int main()
{
	Human firstMan{ "Adam", 30 };
	Human firstWoman{ "Eve", 28 };

	firstMan.IntroduceSelf();
	firstWoman.IntroduceSelf();
}
1
2
	Human* firstMan; // gives me an error
	firstMan->name = "Adam";

What error does the Human* firstMan; generate?

Another compiler warns about the next line:
 In function 'int main()':
22:17: warning: 'firstMan' is used uninitialized in this function [-Wuninitialized]

A warning is not an error that would prevent compiling code, although in this case it points out fatal logical error.

You create a variable 'firstMan' that is a pointer, but you don't set it to point to any Human objects.

In firstMan->name you dereference the pointer in order to access the object that it points to.
Alas, your pointer does not point to any human!
Then you write to member of an object that does not exist. You write to memory that is not what you believe it is. You overwrite something that might be important. It is like I would park my Monster Truck blindfolded; completely ignoring where I am or what else is there.



As said, the OS reclaims all memory allocated for process when the process ends, but relying on that is a bad habit that you don't want to learn. If you wan't to use new, then you should use the delete too, appropriately. Truth is, all that is so tedious that you should transit to "smart pointers" and "containers" that do the menial chores on your behalf.
Topic archived. No new replies allowed.