Trying to understand C++ pointers and data type initializations

Viewed 78

I am trying to learn C++, I have a fair bit of experience in C# and the 2 languages are so dissimilar and I am having trouble understanding data types and pointer variants of data types and the initialization of them, please consider the code below:

    wchar_t *array = new wchar_t[10]; //Declaring a pointer of wchart_t and initializing to a wchar_t array of size 10 ?
    auto memAddressOfPointer = &array; //auto is resolving memAddressOfPointer to a pointer of a pointer?
    cout << array << endl; //Printing the memory address of array not the object created above?
    cout << *array << endl; //Getting the actual value of the object (empty wchar_t array of size 10 in bytes?
    cout << &array << endl; //Printing the address of the obj?
    cout << memAddressOfPointer << endl; //Printing the address of the obj ?

My question is why would I create a pointer and initialize it? Why not just create an array of wchar_t? like:

    wchar_t array [10];

I refer to this stack post as well: Unable to create an array of wchar_t

Thank you for your consideration.

3 Answers

If you know the extent of the array when writing the program, there's absolutely nothing wrong with wchar_t array [10];. If 10 is a fixed (constexpr) number - stick with that.

What wchar_t *array = new wchar_t[10]; lets you do is to let 10 be a number that you find out in run-time. You can change 10 to x and let x be a number that the user supplies or that you calculate somehow. wchar_t array [x]; when x is not a constexpr is on the other hand not valid C++ (but is available as an exension, called VLA, in some implementations).

Note: One downside with using new is that you need to make sure you delete the same pointer. This is not always simple. Using these raw pointers is therefore not what you usually want to do. Instead, use a smart pointer, like std::unique_ptr<wchar_t[]>, and the resource will be delete[]d when the pointer goes out of scope.

If you know the size of the number of elements you need to put in the array, then just use the array i.e., wchar_t arr[10];.

If you don't know the size, you can create the array at runtime using dynamic memory allocation with the required size i.e., wchar_t *arr = new wchar_t[required_size]. Once the memory is allocated, you need to deallocate it using delete[] operator for arrays and delete for non-array pointers. However I highly recommend you don't do that and instead either

  • Use std::wstring in this particular case which will automatically handle this for you.
  • Use std::vector for everything else if you can. It's a dynamic array which will grow automatically. No manual memory management etc.
  • In case you have to use pointers, use a smart pointer like unique_ptr or shared_ptr. The advantage of using smart pointers is that they will automatically clean up once they go out of scope.

The advantages of creating a pointer instead of an array are the dynamic allocation that you can take advantage of and also the properties of the pointer that might help. Consider the following code that represent the dynamic allocation and reallocation:

int x;
cin >> x;
int *oldArr = malloc(x * sizeof(int));
for(int i = 0; i < x; i++)
    arr[i] = i;
cin >> x;
arr = realloc(arr, x * sizeof(int));

Here is another example that shows one of the pointer features which also you can use along with arrays.

int arr[5] = {1, 2, 3, 4 ,5};
int *ptr = arr;
cout << *ptr;
ptr++;
cout << *ptr;
cout << *(ptr + 1);

Despite these advantages and others, I think that the example you are presenting of using pointers instead of arrays is just for academic purposes to understand how to work with pointers in order to build more complex data structures using pointers in future lessons as you are using constant size arrays.

Related