I am learning C++ language. There are two concepts I can not figure out:
- What is the difference between new and malloc?
- What is the difference between pointer and reference?
Can anyone help?
The original title of the question and the claim about learning said the question was about C, not C++. But the questions are clearly about C++, not C, because one of the two items in each question does not exist as a relevant concept in C.
C is not C++.
newis how you allocate memory and call constructors in C++.mallocjust allocates memory, in both C and C++.A pointer can be NULL – aka point to a defined invalid location. A reference (which doesn’t exist in C) will always refer to something – it cannot legally refer to an invalid location.