When is delete keyword necessary?

[Closed by author]
Last edited on
You have a conceptual error, there. It's not true that nothing ever existed. Whenever you new something you're a) creating an object, and b) allocating some memory for that object. What new does once that creation is complete is give you a pointer to that object. If, like in your second example, you don't assign that pointer to anything, the object becomes lost and unusable, but it never ceases to exist. You're just unable to destroy it or release its memory so it can be used for something else.
If you new something and you don't have a corresponding delete then you're leaking memory, and for certain objects you may also end up with logic errors.

Also, this:
1
2
int num = new int(5)
std::cout << num;
doesn't compile (the second line would compile but not do what you think). This does:
1
2
int *num = new int(5);
std::cout << *num;
[deleted by author]
Last edited on
[Deleted by author]
Last edited on
I have no idea what "CTRPF" is. For all I know it may not even be C++.
That aside, all leaking memory does is make memory unavailable until the process terminates or, in the worst case, until the hardware is power-cycled. It could not brick a piece of hardware.

EDIT:
I should never use the new keyword unless it is
on an actual object?
I don't understand the question.
Last edited on
[deleted by author]
Last edited on
That code doesn't compile, so I suppose you could say it's "safe", in a way.
helios said:
I have no idea what "CTRPF" is. For all I know it may not even be C++.


It's not important that you understand the simple concepts being said here,
I understand that your simple mind cannot comprehend such things.

All it is is a 3ds C++ framework for making cheats. :P
[Deleted by author]
Last edited on
new always returns a pointer to the specified type. So new int returns type int*, new int* returns type int**, new someType returns type someType* etc.

You can do this:

1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
	int* someArray[5] {};
	someArray[2] = new int(4);
	std::cout << *someArray[2];
	delete someArray[2];
}


which displays:


4

Last edited on
The fuck is wrong with you, dude? Is this how you always treat people who you are asking for help?
@Alexander123, you keep begging for help, and when you get answers you don't like you shit on people. Stop being an major asshole, or go the fuck away. This is not the first time you've done this.
Ah, so this is a recurring pattern, is it? Well, I have a new name for my ignore list, then.
Yes, helios, very much a recurring pattern. You were just the latest. I doubt you will be the last.

In a way it is rather amusing. He begs for help, many times for really simple things, and then when he gets an answer he doesn't like he haughtily sniffs the respondent doesn't understand simple concepts.

Reminds me of againtry.

For me Alexander's been moron silenced for some time now.
Topic archived. No new replies allowed.