Sign in to follow this  

Addresses of Arrays

This topic is 4716 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
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
Quote:
Original post by Fruny
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.


Yeah, it occured to me AFTER I posted (see my edit). I only got 4 hours of sleep last night :)

Share this post


Link to post
Share on other sites
Now here's my issue. I'm not really working with integers (they just make convenient datatypes for tests). I'm actually trying to work out an image storage array. I want it to be dynamic because the program won't know how many images are to be placed inside the array. When I try to resize the array, I place the existing array into a temporary array, resize, then place back in the original array.

My wish, in the interest of processor speed, is to work only with the pointer values of the images, so that I won't need to keep recreating the images (thus taking up processor time). Therefore it would be nice to know if this is, in fact, what I'm doing in my array.

Share this post


Link to post
Share on other sites
Quote:
Original post by startreky498
So vectors are more performance-friendly than arrays in this case? I should look into these. Thanks.


They'll have the same performance (they *are* dynamically allocated arrays). The library just takes care of all the resizing for you. Why reinvent the wheel or be content with kludgy code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Raduprv
Yeah, it occured to me AFTER I posted (see my edit). I only got 4 hours of sleep last night :)
Then get some sleep! Seriously, 4 hours is far from optimal.

I'd like to discourage posting guesses, the OP probably has the same chance as guessing the answer as you do. Posting the wrong answer (especially when it is the first post), can be worse than posting nothing at all. I'm pleased you clearly stated that it was a guess though.

I'm worried about people in general just guessing though, as it can lead others astray. An expert in the particular field will usually respond in a relatively timely manner, or ask for more information if necessary.

(Polymorphic OOP, is of course correct)

Share this post


Link to post
Share on other sites

This topic is 4716 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this