Using new
to Create Dynamic Arrays
If all a program needs is a single value, you might as well declare a simple variable because that is simpler, if less impressive, than using new
and a pointer to manage a single small data object. More typically, you use new
with larger chunks of data, such as arrays, strings, and structures. This is where new
is useful. Suppose, for example, you’re writing a program that might or might not need an array, depending on information given to the program while it is running. If you create an array by declaring it, the space is allocated when the program is compiled. Whether or not the program finally uses the array, the array is there, using up memory. Allocating the array during compile time is called static binding ...
Get C++ Primer Plus now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.