-2

Here we have arr1 and arr2...

int  arr1[3]{};
int *arr2 = (int *)std::malloc(size_t(int) * 3);

and i think entities of arr2 should be stored in the heap compared to the arr1 that is stored in the stack memory.

question: Is there any reason to use arr2 if its not going to get bigger or smaller during the code?

shiyon sufa
  • 179
  • 3
  • 7
  • 3
    `arr1` is not a pointer, it's an array. – Yksisarvinen Jul 19 '22 at 15:17
  • 2
    *"arr1 and arr2 are pointers..."* No `arr1` is not a pointer. It's type is `int [3]`. – Jason Jul 19 '22 at 15:17
  • Also: `arr1` will be automatically destroyed when it goes out of scope. You can also take the `std::size` of `arr1` while that information is lost in `arr2`. `arr2` is uninitilaized, `arr1` is not. – Ted Lyngmo Jul 19 '22 at 15:18
  • Allocating arrays on heap may be useful as data returning from functions when `std::vector` is unavailable (for example, when you are creating a library that may be linked with C code), but I think `int *arr2 = new int[3];` is better. – MikeCAT Jul 19 '22 at 15:18
  • isnt (arr1 == &arr1[0]) ? @Yksisarvinen – shiyon sufa Jul 19 '22 at 15:18
  • @TedLyngmo sorry edited mybad – shiyon sufa Jul 19 '22 at 15:20
  • `arr1 == &arr1[1]` will be false. `arr1 == &arr1[0]` will be true because arrays in this type of expressions are converted to a pointer to the first element of the array. – MikeCAT Jul 19 '22 at 15:20
  • 1
    They are not the same https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay – pptaszni Jul 19 '22 at 15:20
  • 3
    malloc usually doesn't have a place in C++, even new/delete are no longer recommended. For arrays of fixed size use std::array, for dynamically resizable arrays use std::vector. With manual memory managment it is just too easy to write buggy code. And if you have to do your own memory managment have a look at std::make_unique (or rarely std::make_shared) – Pepijn Kramer Jul 19 '22 at 15:20
  • The expression `arr1 == &arr1[0]` evaluates to `true`, even though one is a pointer and the other is an array. `==` does not compare _types_. – Drew Dormann Jul 19 '22 at 15:21
  • Try using `int arr[1000000000];` in your code and you know why you might need `int *arr = new int[1000000000];`. (hide the `new` in a unique/shared_ptr) – Goswin von Brederlow Jul 19 '22 at 15:52
  • @DrewDormann It only compares true since `arr1` decays to a pointer to the first element. So both sides of the `==` are the same type after decay. Otherwise the comparison could give anything. – Goswin von Brederlow Jul 19 '22 at 15:54

2 Answers2

2

arr1 and arr2 are pointers

No, arr1 is not a pointer. In particular, arr1 has type int [3], it's just that it decays to a pointer to its first element i.e., int* in certain contexts due to type decay.


Is there any reason to use arr2 if its not going to get bigger or smaller during the code?

arr1 will be automatically destroyed when it goes out of scope while for arr2 you will have to free the memory(pointed by arr2) manually. That is, you'll have to do manual memory management(not recommended).

Moreover, smart pointers should be preferred over raw pointers if/when you decide to use dynamically allocated memory.

Or better yet would be to use(if allowed) standard containers like std::vector altogether etc.

Jason
  • 36,170
  • 5
  • 26
  • 60
1

The memory held by arr1 is guaranteed to be allocated only in it's scope. The memory arr2 points to is yours till you free() it (or exit the program), thus you might pass it as a return value, store it, etc. Also, arr2's allocation likely takes more bytes in practice, as memory allocation usually maintains blocks of free memory. So allocating via malloc or new is both slower and takes more memory (but sometimes required for other benefits).

lorro
  • 10,687
  • 23
  • 36