How to use a dynamic array

closed account (Ey80oG1T)
Here I have a dynamic array:

int* intArr = new int[2];

But how can I add elements to this array?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
    int* array = new int[2]; // A LIMIT OF 2!
    
    array[0] = 99;
    array[1] = 102;
    
    std::cout << "Total: " << array[0] + array[1] << '\n';
    
    return 0;
}
closed account (Ey80oG1T)
Oh, so it's just like a normal array? And is there a way to add a third element?
No. You'd have the create a new array of size three, copy the first two elements into it, and then put the third in.

This being C++, we just use vector instead.

You could use realloc on the array, but that's just doing the same thing and is more to go wrong. Just use a vector. Use a vector.
Last edited on
closed account (Ey80oG1T)
Oh... okay
@OP,

Dynamic arrays and pointers are an important and fundamental part of C++, particularly for people starting off. In fact they are vitally important.

Later on in your studies you will learn about vectors and other ‘containers’ which take a lot of the hard work out of using basic arrays but it is a major mistake not to go through the basics and leap to mysterious conclusions.

Don’t be put off by self-appointed ‘experts’ who probably aren’t more than a couple of lectures ahead of you.

In any case dynamic arrays for quick/simple applications are all that’s needed.
Dynamic arrays and pointers are an important and fundamental part of C++, particularly for people starting off.


To teach C++ as "C with bits on top" is considered the wrong approach by the experienced C++ community, for good reasons. The evidence shows that teaching people to think and write in C first, rather than in C++, is damaging.

Here is Kate Gregory, one of the world's most well-known speakers on the subject, discussing it: https://www.youtube.com/watch?v=YnWhqhNdYyk

You don't have to listen to me.
You don't have to listen to againtry.
You can listen to the experts.
Last edited on
arrays aren't C, end of story.

C++ = C with bits on top
is a straw man argument and totally misses the point about learning and understanding.

Stamp your foot and use bold type as much as you like. You're simply, plainly and demonstrably wrong.

It's a wonder you're not prattling on like some cult zealot that STL are not 'pure C++'.
Don't listen to me, don't listen to againtry, listen to the experts. Conversation over.
Conversation never existed. You're out of your depth.
Kate Gregory's talks are great, I love what she says about variable names. Too often do I see "thing1" and "thing2" acting in mysterious ways, when better names would have made the relationship much clearer (/slightly off topic).
Oh, so it's just like a normal array? And is there a way to add a third element?

if you use C's memory (malloc and free) instead of new and delete, there is a realloc. You can also make a new block of memory and copy the old one to it, release the old one... its not efficient to do this too much. Vectors do this for you, and they over-allocate when they grow to reduce the # of times this happens.

Generally you will use the STL or third party trusted tools like boost unless doing very low level coding (eg, writing the stl for a compiler) or embedded work (may not support stl) or the like. Playing with pointers should be considered 'just to learn them better'.

The stl does not have every known container.
Topic archived. No new replies allowed.