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.

6
  • 1
    for(int i=0;i<50;i++) arr[i]=new int[50]; Commented Dec 20, 2016 at 8:50
  • 5
    For an easy life, use std::vector<int>. Commented Dec 20, 2016 at 8:50
  • 2
    it would be great if you could share your motivation for doing that. Commented Dec 20, 2016 at 8:54
  • 1
    I am just curious regarding how the memory works during allocation. Commented Dec 20, 2016 at 8:55
  • 1
    I wouldn't call int *arr = new int[50]; the "standard way". This is something you should write rarely to never. It's mentioned in very old textbooks perhaps. Commented Dec 20, 2016 at 8:59

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

for (int i = 0; i < sizeof(arr) / sizeof(int); ++i) // to avoid having the constant 50 in two places
assuming arr has not decayed.
Won't there be also pointer dereference overhead besides cache misses?
@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 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.
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."

2 Comments

is std::array<int, 50> vi; similar to having 50 different variables where each will have a non-contiguous memory allocation ?
@PranjalMisra, Refer std::array<int, 50>. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.