Jump to content
  • Advertisement
Sign in to follow this  
startreky498

Addresses of Arrays

This topic is 4933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
I am not really familiar with C++, but I would guess it returns a pointer, so it points to where your array is.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!