char example[10];

example is an array of 10 chars. Depending on context, it has automatic or static storage. The size can only be compile time constant. The array is destroyed and deallocated automatically.

char* example = new char[10];

example is a pointer. It is not an array. It points to first element of an array in dynamic storage. The size of dynamic array can be determined at runtime. The array is not destroyed and deallocated automatically. If not deallocated, the memory will leak.

Dynamic allocation is generally slower than static or automatic. On the other hand, the amount of memory available for automatic storage is typically very limited.

Bare owning pointers should be avoided. Best practice is to use a smart pointer or a RAII container such as std::vector when dynamic array is needed.

Answer from eerorika on Stack Overflow
🌐
Cppreference
en.cppreference.com › w › cpp › language › new.html
new expression - cppreference.com
If type is an array type, the name of the function is operator new[]. As described in allocation function, the C++ program may provide global and class-specific replacements for these functions. If the new expression begins with the optional :: operator, as in ::new T or ::new T[n], class-specific ...
Top answer
1 of 7
1

It is an array of 'n' pointers, for which memory can be allocated and initialized in loop. If n is 3, it is an array of 3 elements and each is pointer to int, can point to set of array of integer values like below.

            matrix[0] -> Ox001  points to  array of int [ 1 2 3 4]
            matrix[1] -> Ox017                          [ 5 6 7 8]
            matrix[2] -> Ox024                          [ 9 10 11 12]

Sample code like this

           int **m = new int*[3];
           for(auto i=0; i < 3; i++)
           {
               m[i] = new int[3];
               for(auto j=0; j < 3; j++)
                   m[i][j] = 0;
           }


           for(auto i=0; i < 3; i++)
           {
               m[i] = new int[3];
               for(auto j=0; j < 3; j++)
                   cout << m[i][j];
               cout << "\n";
           }
2 of 7
1

for instance there is cricket team and you need

Since you have Cricket* team;, this indicates you have one of two possible

situations:

1) a pointer to a single CricketPlayer (or any derived) type

2) a pointer to an array of CricketPlayer (but not derived) types.

What you want is a pointer to an array of CricketPlayer or derived types. So you

need the **.

You'll also need to allocate each team member individually and assign them to the array:

            // 5 players on this team

          CricketPlayer** team = new CricketPlayer*[5];

          // first one is a bowler

          team[0] = new Bowler();

         // second one is a hitter

          team[1] = new Hitter();

           // etc

// then to deallocate memory

         delete team[0];
         delete team[1];
         delete[] team;

In your query,

           It can be understood as

              int *matrix[]=new int*[n];

SO there are n pointers pointing to n places.

Because

               int *foo;

                foo=new int[5];

will also create 5 consecutive places but it is the same pointer.

In our case it is array of pointers

🌐
Cplusplus
cplusplus.com › reference › new › operator new[]
operator new[]
If set_new_handler has been used ... with a very specific behavior: An expression with the new operator on an array type, first calls function operator new (i.e., this function) with the size of its array type specifier as first argument (plus any array overhead storage to ...
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 62449-new-keyword.html
'new' keyword - C Board
MyClass *var1 = new MyClass(constructorarg); Notice the "*" in there, that specifies that its a pointer, which simply holds the address to the location. if you wish to use the object you have allocated, you need to dereference the pointer, using the *, or their are some other operators you ...
🌐
Unstop
unstop.com › home › blog › new operator in c++ explained (+code examples)
New Operator In C++ Explained (+Code Examples) // Unstop
June 6, 2025 - This is usually done using functions like malloc() and free() in C or the new and delete operators in C++. The new operator is a keyword in the C++ programming language used to allocate memory for an object at runtime.
🌐
Quora
quora.com › Why-we-use-new-keyword-in-Java-for-creating-an-array-But-in-C++-we-dont-use-‘new’-keyword-to-create-an-array
Why we use 'new' keyword in Java for creating an array But in C++ ...
Answer (1 of 5): Because Java forces you to use dynamic memory allocation most of the time even If you know sizeof array (i.e. number of elements) in advance. Dynamic memory allocations are expensive compared to automatic memory allocation (i.e. Stack-based memory allocation ) & static memory all...
🌐
Reddit
reddit.com › r/cpp_questions › what does actually using the new keyword, or instantiating something in c++ really mean?
r/cpp_questions on Reddit: What does actually using the new keyword, or instantiating something in C++ really mean?
August 28, 2020 -

So I don't consider myself a CS noob by any means and have wrangled my fair share of systems, but also find myself forgetting my CS classes a bit and was surprised when I pondered this I didn't have a clear answer. So, in C/C++ (assuming a non-optimizing compiler) I know when we have a declaration like int stackVar = 5; that will probably get placed either in a register or on in-memory stack. However, when you call new, you always get a pointer back, because from what I understand, new will mean it always goes to heap. Why is it this way? It seems to me that instantiating makes sense for larger objects because having a pointer to them is less expensive than loading the object itself. But when then do we put things on stack, or is that only primitive data types? What does "instantiating" actually mean in the context of saying Request* r = new Request() for some web API versus the above int r = 5 example? How are the two different?

Top answer
1 of 9
18
So, it seems like your question sort of boils down to two things: What is heap allocation used for, and what does new do on top of allocation? Heap allocation is mainly used if you’ve got some sort of array or object that the size isn’t known at compile time, or if you’ve got a huge data structure. On top of those two reasons, the heap is also used to extend the lifetime of arrays, objects etc. Generally, you’re going to be putting things on the stack. Variables, normal arrays, things that don’t need extended lifetimes should live on the stack because you don’t have to deal with allocating memory, and it’ll help your code run faster. Using the new keyword with an object not only allocates memory, but it also calls the class constructor with it, which is pretty neat! But, you don’t need to put objects on the heap, and usually you won’t have to. You can simply declare and initialize objects just like you would with any primitive types. Hope this cleared it up! Feel free to ask more questions, my writing isn’t 100% clear lol
2 of 9
5
When you write int x{ 5 }; or int* y = new int{ 5 }; two things happen: allocation and initialization. When you create an object "normally" (like x above), it is placed on the stack, which means that space is allocated for it on the stack and the stack pointer is moved forwards to point to the next free slot. When you place something on the heap, the actual value is placed on the heap, and the pointer y is placed on the stack. Allocating space for something on the heap is very expensive compared to stack allocations, since stack allocations are essentially just pointer arithmetic, whereas heap allocation means that your program asks the operating system to give it some free space. Initialization is the same for stack and heap allocations; the constructor of whatever type is being constructed is run on that freshly allocated chunk of memory. As to why you would allocate something on the heap instead of on the stack; you generally only do it when you don't know the size of whatever object you're trying to allocate space for, or if the object's size exceeds the size of the stack, which is pre-determined. For example, a std::vector might contain 8 integers or it might contain 5 million. Since we don't know, the vector allocates space for its contents on the heap. Polymorphism is another valid use-case.
Find elsewhere
🌐
IBM
ibm.com › docs › en › i › 7.3.0
new expressions (C++ only)
We cannot provide a description for this page right now
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › cpp › new-operator-cpp
new operator (C++) | Microsoft Learn
... Attempts to allocate and initialize an object or array of objects of a specified or placeholder type, and returns a suitably typed, nonzero pointer to the object (or to the initial object of the array). new-expression: ::opt new new-placementopt ...
🌐
Reddit
reddit.com › r/csharp › when to use the “new” operator when creating an array?
r/csharp on Reddit: When to use the “new” operator when creating an array?
July 4, 2022 -

Hi, C# beginner here, can someone explain to me why I should use “new” when creating an array instead of just giving it values from the beginning? Thanks :)

Top answer
1 of 4
60
Sometimes you only know you want an array. You don't know how big it should be yet and certainly don't know the values that go in it. So you just declare a variable: int[] highScores; The variable is null, you have to assign an array to it later. Sometimes you only know how many things go in the array. You don't know the values yet. That's when you'll use the syntax with the new keyword: int[] highScores = new int[10]; This creates a new 10-element array and assigns it to highScores. All of the values in this case are initialized to 0. That's a special case for int[] and other "value types". Some types, like string[], would have null in each element. The point being: after creating an array like this, you still have to put the values inside of it. What you're asking about is a relatively new C# thing called an "initializer". If you know the size AND the values you can set them all at once: int[] extraLivesAt = new int[] { 10000, 20000, 50000, 100000 }; This creates a new array and gives it the element values you specify. You don't have to declare the size because the initializer shows the size. Some people felt like this was too wordy, so C# lets you take a shortcut and say: int[] extraLivesAt = { 10000, 20000, 50000, 100000 }; It's not any different, it's just that the keywords become optional if C# can unambiguously tell the thing on the right of = is supposed to be an array that matches the type on the left. Describing what "unambiguously" means in this context gets really hairy. The thing is, {} isn't just for initializing arrays. C# is like Perl in a lot of ways in that the team loves to reuse the same symbols for wildly different concepts. You can use {} to: Initialize arrays Initialize dictionaries Initialize classes Initialize structs Describe object patterns Represent the body of a statement with a body Create blocks of code with name scoping powers Create variable placeholders in format strings Create variable placeholders in interpolated strings Represent restrictions in regular expressions Probably 3 or 4 other things I'm forgetting about So it'd take me a whole page to cover all of those. Suffice to say this shortcut won't work well if you try to use var, and personally I always use new int[] instead of just having a "naked" initializer. It's too easy to confuse C# if you don't use new and there are so many things it can think the {} means the error message is NOT going to point you in the right direction. And, again, because of the Perl fascination, you can also: int[] highScores = new[] { 1, 2, 3 }; C# reckons since the left-hand side is int[], obviously the right-hand side is int[] so you can drop the type name if you want. Personally I don't. The only time I use the syntax without a type name is when I'm creating arrays of anonymous types and there are less than zero reasons why I should elaborate on that in this post.
2 of 4
2
Not sure what you mean about giving values to start. Are you asking what the difference is between creating a new array with a certain amount of elements, and then defining the values for each element one by one compared to initializing it with the values? If you are, then it's a matter of code readability. IMO the initializer syntax is earlier to read. If you so both, you can see how they differ slightly in the lowered code. https://sharplab.io/#v2:EYLgtghglgdgNAExAagD4AEBMBGAsAKHQGYACLEgYRIG8CT6zT0AWEgWQAoBKGuh/gG4QATiRHCIATwDyMAKYkAvCXkB3EgGMAFiIDaAXWoByCEbgkjwMxY1GAvgG4+/ekNHipAFVUB7JSrl1bT0ifSd8FwYPSW8fXQAGfX8TI3DI+mjYhKTlS1TnF0zfbOTbNIY7AjsgA==
🌐
Cppreference
en.cppreference.com › w › cpp › memory › new › operator_new
operator new, operator new[] - cppreference.com
The standard library's non-allocating placement forms of operator new (9,10) cannot be replaced and can only be customized if the placement new expression did not use the ::new syntax, by providing a class-specific placement new (19,20) with matching signature: void* T::operator new(std::size_t, void*) or void* T::operator new[](std::size_t, void*). Both single-object and array allocation functions may be defined as public static member functions of a class (versions (15-18)).
🌐
Cplusplus
cplusplus.com › forum › beginner › 144787
How useful can the "new" keyword be in h - C++ Forum
Things you can only do with new: A variable-sized array is an array whose size depends on a value that's only available when the program is running, as opposed to being available while the program is being compiled. It's not an array that can change sizes.
🌐
Quora
quora.com › Where-does-the-new-keyword-allocate-memory-in-C
Where does the 'new' keyword allocate memory in C++? - Quora
Answer (1 of 2): There is a memory area assigned to a process (the environment given to the program by the operating system when it launches it) that is mapped onto a linear set of integer addresses. This memory area is segmented into several areas, one where code goes, one where metadata for the...
🌐
Cplusplus
cplusplus.com › forum › beginner › 89449
Array Keyword??? - C++ Forum
Use a different IDE (not microsoft) and you will see "array" is not highlighted as a keyword, because microsoft created it.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › new-vs-operator-new-in-cpp
new vs operator new in C++ - GeeksforGeeks
February 21, 2023 - The new operator is an operator which denotes a request for memory allocation on the Heap. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer ...
🌐
Code with Mosh
forum.codewithmosh.com › c++
C++ Auto Keyword - C++ - Code with Mosh Forum
July 17, 2023 - why can’t we initialize an array with auto keyword in c++
🌐
TutorialsPoint
tutorialspoint.com › using-the-new-keyword-in-chash
Using the new keyword in C#
Use the new keyword to create an instance of the array. The new operator is used to create an object or instantiate an object. Here in the example an object is created for the class using the new. The following is an example.