Creating an Array on the heap

I think I get this, but I'm just not sure. Given the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int * applyAll(const int *const arr1, size_t size1, const int *const arr2, size_t size2) {
    int *newArray{};

    newArray = new int[size1 * size2];

    int position{0};
    for (size_t i{0};i  < size2; ++i) {
        for (size_t j{0}; j < size1; ++j) {
            newArray[position] = arr1[j] * arr2[i];
            ++position;
        }
    }
    return newArray;
}


the first line's declaring a variable of type int. I get that. And I get that the next line:

newArray = new int[size1 * size2];

is the new int variable newArray being transformed into an array[]. I actually commented out the 4th line and did this to the 2nd:

int *newArray[const_cast<int>(size1) * const_cast<int> (size2)]{};

That got me in trouble, but i don't know why. I'd appreciate it if someone wld tell me if lines 2 and 4 are the best way to do this? Or is it the only way?
Last edited on
That got me in trouble, but i don't know why.
newArray = new int[size1 * size2];
size1 and size2 are variables. Standard C++ does not support variable length arrays (VLA).
With standard C++ the size of the array must be known at compile time.
So your original lines 1-4 are correct.
So your original lines 1-4 are correct.


Thank you for the feedback.
Or even:

1
2
3
4
5
6
7
8
9
int* applyAll(const int* const arr1, size_t size1, const int* const arr2, size_t size2) {
	const auto newArray {new int[size1 * size2]};

	for (size_t i {}, position {}; i < size2; ++i)
		for (size_t j {}; j < size1; ++j)
			newArray[position++] = arr1[j] * arr2[i];

	return newArray;
}


NOTE that it is responsibility of the caller to free the allocated memory. This is usually a bad idea.

Why not use std::vector? Much easier.
Last edited on
the first line's declaring a variable of type int. I get that.

No, it isn't. It's declaring a variable of type int * - that is, a pointer to an int; that is, a variable which stores a memory address, and treats the data held at that address as an int.

This is important; int is not the same type as int *.

is the new int variable newArray being transformed into an array[].


No. Line 4 is:
- dynamically allocating memory for the array on the heap
- storing the address of that array (or, more precisely, the address of the first element of the array) in the pointer variable called newArray.

EDIT: On line 2, you are declaring and initialising newArray, and on the very next line of code (line 4), you are changing it's value to something else. This is pointless; you should simply initialise it to the value you want it to actually be in a single line:

 
int *newArray{new int[size1 * size2]};


Last edited on
@seeplus:
Why not use std::vector? Much easier.

The coding exercise, on udemy.com, is on arrays and pointers.

@MikeyBoy:
On line 2, you are declaring and initialising newArray, and on the very next line of code (line 4), you are changing it's value to something else. This is pointless

I agree your way is more of what I was expecting but unsure how to do it.

This is an extremely helpful Forum. No one here tries to make you feel stupid just because you don't know something or you're just not getting it. Kudos to you all.
Last edited on
Topic archived. No new replies allowed.