& and ~

Hi, I have a practice exam, and wanted to make sure I was right on a question.

In C++ the____*______ keyword tells the system to allocate dynamic memory
and the____~_____keyword tells the system to deallocate dynamic memory.


I'm mostly unsure about the asterisk. Is that supposed to be an ampersand?

Thank you.
In C++ the new or new[] keyword tells the system to allocate dynamic memory, the delete or delete[] keyword tells the system to deallocate dynamic memory.

~ is the bitwise NOT logical operator.
https://en.cppreference.com/w/cpp/language/operator_arithmetic#Bitwise_logic_operators

* is a bit of a special case. Depending on context it is the multiplication operator, used to declare a pointer or dereference an existing pointer.
https://en.cppreference.com/w/cpp/language/operator_member_access#Built-in_pointer-to-member_access_operators

Pointers can be created that point to stack memory.
1
2
int a = 5;
int* b = &a;

No dynamic memory accessed above.

~ and * have nothing directly to do with using dynamic memory.

& is the address-of operator.
https://www.learncpp.com/cpp-tutorial/67-introduction-to-pointers/
Last edited on
O.O

I completely forgot about new and delete. Jeepers.

Thanks man.

Most likely, the ~ is referring to the declaration of a destructor of a class.
https://www.geeksforgeeks.org/destructors-c/

As FurryGuy said, it has nothing in itself to do with dynamic memory. A destructor is called when delete/delete[] is called on a dynamic object, or automatically when an object on the stack goes out of scope. A destructor should not be called directly by the user.
Last edited on
Most likely, the ~ is referring to the declaration of a destructor of a class.

It was a fill-in-the-blank quiz question, not a question that asked what '~' is used for. I mentioned the bitwise NOT operator since the question was on keywords.

The OP even admits they had a brain fart. Something that happens to me more often than I'd like when it comes to C++ syntax and features.
Last edited on
Topic archived. No new replies allowed.