3

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.

  • `*memAddressOfPointer == array == &array[0]` and the rest follows. `Why not just create an array of wchar_t` You *could* create an array, instead, but (a) you'd need to know the size at compile time, (b) you can't repoint it to a different array later, and (c) if it's a local/automatic variable its lifetime would end at the end of the enclosing block or function. – dxiv Jun 13 '20 at 23:08
  • I feel like "why use pointers" is a bit broad. – Taekahn Jun 13 '20 at 23:12
  • 1
    You can create an array with : `wchar_t array[10]` this will create an array in **stack** meaning whenever array is out of scope it's done you won't be able to access it. Creating an array with `new` will return a pointer to the first element in the array. NOTE: **when using *new* the array will be created in the heap**. This is called dynamic memory allocation. So: array will return the address of the array in the heap. *array will enable you to access data in that memory address. And `memAddressOfPointer` is a pointer to pointer. – Abdelbaki Boukerche Jun 13 '20 at 23:22
  • Does this answer your question: https://stackoverflow.com/questions/162941/ – M.M Jun 13 '20 at 23:26
  • `cout << *array << endl` causes undefined behaviour (you try to output uninitialized objects) – M.M Jun 13 '20 at 23:27

3 Answers3

4

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.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Thank you very much for this insightful answer, it makes 100% sense, I haven't gotten to smart pointers yet but I'll definitely keep them in mind. 1 step at a time I suppose. As you upvoted Waqar's answer as well I'll set that as the answer to the question. – Helgard Wagener Jun 13 '20 at 23:53
  • 2
    @HelgardWagener You are welcome! - and I think you chose wisely when you accepted Waqar's answer. Using a `std::wstring` is most probably the best route you could take. – Ted Lyngmo Jun 13 '20 at 23:55
4

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.
Waqar
  • 8,558
  • 4
  • 35
  • 43
  • 1
    You get my upvote for pointing out the obvious solution: `std::wstring` - I missed making that point completely. – Ted Lyngmo Jun 13 '20 at 23:41
  • And I think `std::vector` could also be an answer. Thats the best dynamic array we have, and should be used instead of taking the pointer road. – Waqar Jun 13 '20 at 23:44
  • 1
    As Ted himself also upvoted this answer I am selecting this one as the future answer. Thank you very much for the explanation.This makes complete sense, its a tough road ahead but I am absolutely falling in love with C++ – Helgard Wagener Jun 13 '20 at 23:52
1

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.

Ansary
  • 61
  • 2