Dynamically allocated arrays

I know. Use vectors. And that's what I do, most of the time.

So I see the code

1
2
3
4
int myInt;
//Insert some way to initialize myInt here
int* p;
p = new int[myInt];


And, of course, prevent memory leaks, because those are bad and could even crash one's computer, I hear.

But the new keyword allocates the required amount of memory dynamically and returns a pointer to the new stuff. So I'm assuming this is interpreted as new (int[myInt]);, as I couldn't make sense of the code the other way. But an array's a pointer to the first element ... so shouldn't this be returning a pointer to a pointer to the first element?

Also, preventing memory leaks. Is delete p; sufficient, or do I need to do something else?
Last edited on
'p' will hold the address of the first int, i.e. &p[0].

I don't see why it should return a pointer-to-pointer-to-first. It doesn't return a point to an array; it is the array. It is fully defined by the location/address of the first item and the length (which you know, because you use it to initialize the array).

delete p; is not enough. Anything initialized with new T[] needs to be freed with delete[] p;. If your array has multiple dimensions, you'll have to iterate over all "inner arrays" to initialize and delete.
so shouldn't this be returning a pointer to a pointer to the first element?

it returns int pointer to the first element.

lso, preventing memory leaks. Is delete p; sufficient

no, it is not.

see this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;


int main()
{
	const int myInt = 10;
	int* p;
	p = new int[myInt];

	for (short i = 0; i < 10; ++i)
		p[i] = i;

	for (short i = 0; i < 10; ++i)
		cout << p[i] << endl;

	delete[] p;
	return 0;
}
closed account (4z0M4iN6)
I don't now about new (int[myInt])

This code doesn't make sense, because:

- myInt isn't initialized
- you don't seem to understand, what myInt means.

I suggest, you change the name of myInt to size and then you should understand.

@dadabe
He explicitly added //Insert some way to initialize myInt here . It's the second line of his 4-line code. That's 25% of the code. How did you miss that?

I think his proposed syntax is just syntactical confusion as "new" is an operator, not a function. Both are correct, by the way, just like you can write any operator "in full":

1
2
3
4
5
6
7
8
9
10
11
12
struct wInt {
	int a;
	wInt(const int A) : a(A) {}
	void operator+=(const int B) { a += B; }
}

int main()
{
    wInt W(5);
    W.operator+=(5);
    return(0);
}
closed account (4z0M4iN6)
Oh sorry, I didn't look at the comment, only the code - because I thought it would be only a comment - and comments I look not very often - because I think it's none of the code. I'm used, when to look at the code, not to see the comments - because they would distract my sight on the code.
Last edited on
Apologies, I know I should get it by now, but for some reason my brain doesn't want to work.

(Yes, I did know new wasn't a function. :) The reason I wrote it that way was because I couldn't understand it as (new int)[5].) So here's my reasoning.

It'll add some new dynamic memory (deleted with delete[]p, thanks, codekiddy and Gaminic!), and return a pointer to the memory you told it to allocate, in this case, an int[myInt]. But since an array is a pointer to the first element, it's returning a pointer to an array, which is a pointer to a pointer ... ?

There's clearly something wrong, as my reasoning is going against the experimental evidence, and I'd like to thank everyone for trying so far, but I don't think I've fully "gotten" why this reasoning isn't right.
Last edited on
An array is not a pointer.

This is a pointer to an array of 10 ints.
int (*arr)[10];
This is not very useful because it can only point to int arrays of length 10. When you do dynamic allocation you often don't know what the size is going to be at compile time so that is not going to work. Giving you a pointer to the first element in the array is much more convenient and works the same whatever the size is. Having a pointer to the first element in the array also has the advantage that you can use the same syntax to access the elements in the array as if the pointer actually was an array.
Huh. I thought, for some reason, int p[5] (Hopefully I got that right, I can never keep C++'s and Java's syntax for arrays straight!) would tell the compiler to allocate space for 5 adjacent ints, and set p equal to a pointer to the first one, therefore p[2] would be the same as *(p+2). Or am I getting this completely wrong?
An array can be cast to a pointer, often implicitly, but that doesn't mean it's a pointer.

Honestly, static arrays are plain confusing. If you want to use them, use std::array<T, size>.
Thanks a lot for your time, everyone. I know I was probably annoying.
Topic archived. No new replies allowed.