2

The standard way of allocating array using new int is:

 int* arr = new int[50];

while declaring it in this manner there is going to be contiguous memory allocation and there will be a single array variable in the stack of variables.

if I want to declare it in the form of 50 different pointer variables so that each pointer would have different memory address and not necessarily contiguous the most obvious way of going for it is like this:

int * arr[50];

but in this way what would be the command / code for assigning memory ( i.e. via new int ) and what are the downsides or advantages of declaring in each manner.

iammilind
  • 68,093
  • 33
  • 169
  • 336

2 Answers2

5

The obvious way would be to iterate over all the elements and allocate memory for them:

for (int i = 0; i < 50; i++){
    arr[i] = new int;
}

The downside of non-contiguous memory chunk would be cache misses. You can read more on that here.

nishantsingh
  • 4,537
  • 5
  • 25
  • 51
  • 2
    for (int i = 0; i < sizeof(arr) / sizeof(int); ++i) // to avoid having the constant 50 in two places – Sven Nilsson Dec 20 '16 at 08:53
  • 1
    assuming `arr` has not decayed. – George Dec 20 '16 at 08:54
  • Won't there be also pointer dereference overhead besides cache misses? – olegst Dec 20 '16 at 08:59
  • @olegst Yeah, but it's nothing large and you'd hope it wold be optimised to happen once per entry. I think unless it's for a library, I would just use a macro. – George Dec 20 '16 at 09:16
  • @George Anyway I think it's not correct to compare contiguous and non-contiguous memory access this way because to do that one should access entries scattered across the memory (as opposed to placed one after another) which is not the same as dereferencing an array of pointers (which are very likely to point to subsequent addresses, by the way). Author had better explain what exactly they are trying to achieve. – olegst Dec 20 '16 at 09:28
2

How to assign, is already mentioned in this answer; Hence not repeating.
For single int allocation, your below line is an overkill:

int* arr[50];  // all downsides only

Instead of that, you should use simple integers:

int arr[50];

Better to utilise facilities by standard containers such as:

std::vector<int> vi;  // if the number of int-s are dynamic
std::array<int, 50> ai; // if the number of int-s are fixed

Finally, from this answer,

"Avoid pointers until you can't... So the rule of thumb is to use pointers only if there is no other choice."

Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • is std::array vi; similar to having 50 different variables where each will have a non-contiguous memory allocation ? –  Dec 20 '16 at 09:06
  • 1
    @PranjalMisra, Refer [`std::array`](http://www.cplusplus.com/reference/array/array/). It's a better alternative of `int [50]`. Because, it will also have **contiguous** memory allocation, but with `exception` support. Throws runtime exception, for "out of range" array issues. If you have to choose between compile time and dynamic, then choose compile time. Because it's efficient and deterministic. Also, in dynamic allocation, if you have a choice among contiguous and non-contiguous, then choose contiguous. Because it will have less holes in memory and future allocation will be faster. – iammilind Dec 20 '16 at 09:10