Addresses of Arrays

Started by
14 comments, last by iMalc 19 years, 3 months ago
Trying to cure a debate. When declaring a dynamic array in the following method:

int* newArray;
newArray = new int[10];

would "newArray[0]" be the actual value in memory of the integer or would it point to a place in memory where the actual integer is kept?
Advertisement
I am not really familiar with C++, but I would guess it returns a pointer, so it points to where your array is.
Quote:Original post by Raduprv
I am not really familiar with C++, but I would guess it returns a pointer, so it points to where your array is.


All right, thanks a bunch. This'll save me hours in testing phases.
Quote:Original post by Raduprv
I am not really familiar with C++, but I would guess it returns a pointer, so it points to where your array is.


c++'s new is like malloc, except it automagically figures out the size of the datatype and doesn't require a cast. Darn good for nothin' c programmers [wink]
Quote:Original post by startreky498
Trying to cure a debate. When declaring a dynamic array in the following method:

*** Source Snippet Removed ***

would "newArray[0]" be the actual value in memory of the integer or would it point to a place in memory where the actual integer is kept?

newArray[0] is not a pointer. It will give you the 1st element of the dynamically allocated array as an lvalue, which is of type int.
Quote:Original post by ontheheap
Quote:Original post by Raduprv
I am not really familiar with C++, but I would guess it returns a pointer, so it points to where your array is.


c++'s new is like malloc, except it automagically figures out the size of the datatype and doesn't require a cast. Darn good for nothin' c programmers [wink]


Yeah, I figured that much, but I wasn't 100% sure.

As for startreky498, the best way to find out the answer to such questions (which is sometimes confusing even for experienced programmers) is to try both ways. One of them would most likely crash the program, so if that happens it means it's the wrong way. Sometimes the compiler might even give you a warning.
Quote:Original post by Polymorphic OOP
newArray[0] is not a pointer. It will give you the 1st element of the dynamically allocated array as an lvalue, which is of type int.


So then what's the point of the new operator? You get the same thing by just ommiting the "new" keyword.

[edit] Nvm, I am tired :)
Quote:Original post by Raduprv
Quote:Original post by Polymorphic OOP
newArray[0] is not a pointer. It will give you the 1st element of the dynamically allocated array as an lvalue, which is of type int.


So then what's the point of the new operator? You get the same thing by just ommiting the "new" keyword.

No you don't. If you just declare newArray as a pointer without setting the pointer value equal to the dynamically allocated array, then the expression newArray[0] will attempt to access whatever garbage memory address is stored in the uninitialized pointer. It's just like what happens if you were to do it in C without using malloc.
That was me ^
Quote:Original post by Raduprv
So then what's the point of the new operator? You get the same thing by just ommiting the "new" keyword.


int* newArray;newArray = int[10];


...

won't ever compile.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement