+ 6
if you want to program in C++ you should follow C++ guidelines.
C++ added two keywords to replace `malloc` , `calloc` and `free` which are `new`
and `delete`
second arrays are fixed-size data structures, Normally you should not need to resize them , you can use other data structures instead of an array if you need to resize your array for an unknown amount of time,
here is an example of creating array at runtime
#include <iostream>
using namespace std;
int main() {
int size{};
// size of the array
cin >> size;
string* ptr=new string[size];
for(int i = 0;i<size;i++)
{
string x{};
cin >> x;
ptr[i] = x;
}
// printing each element of the array
for(int i = 0; i < size; i++)
{
cout << "array at "<< i << " is " << ptr[i] << endl;
}
delete[]ptr;
return 0;
}
+ 3
C++ is designed on top of C, so that means it supports C features but you should avoid using some of C features and do it in the C++ way!
example :
how to initialize a variable in C vs C++
// C
int x = 0;
// C++
int x{};
for further reading check C++ Crash Course
https://books.google.com/books/about/C++_Crash_Course.html?id=n1v6DwAAQBAJ&printsec=frontcover&source=kp_read_button&hl=en#v=onepage&q&f=false
+ 3
avoid using calloc and malloc and free and other memory management functions
C++ approach is to use new and delete keywords
+ 3
using {} for initializing variables is called brace initialization and it's very common in C++ and is the preferred way
Conclusion:
Prefer {} initialization over alternatives unless you have a strong reason not to.