Jump to content
  • Advertisement
Sign in to follow this  
Jemburula

C++: Array of Pointers to Int or Array of Int Pointers?

This topic is 4032 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

Hello, Man it must get so annoying... it seems like the questions I ask are so simple and alike some times but I can't quite get my head around it... it's almost as if sometimes you have to see someone actually say it/reconfirm what you're trying to understand before you can move on. Basically I've been reading Ted Jensen's C-based article on Pointers. In it, it says
int (*ptr)[10];

is the proper declaration, i.e. ptr here is a pointer to an array of 10 integers. Note that this is different from

int *ptr[10];

which would make ptr the name of an array of 10 pointers to type int.
So that seems fairly straight forward... first is a pointer to an array that holds ints and the second is an array that holds pointers to ints. I've been playing around with the difference but I can't quite seem to see it in actual practice [confused]. Theoretically, if I had an array of 10 pointers to ints shouldn't I be able to say: int *ptr[10]; *ptr = &someInt; It doesn't let me do that though? Another thing I've been wondering is when you delete dynamic arrays with 'delete [] myArray;' how does it know how how many elements are in the array? Does it record how much you new'ed it with?

Share this post


Link to post
Share on other sites
Advertisement
1. You would have to do:

ptr = &someInt; // ptr is the first int* in the array. *ptr gets you the integer
// being pointed at. Which means you could do *ptr = someInt.

or

ptr[x] = &someInt; // For a 'ptr' other than the first one.


2. There is a 16 byte header on the front of each new'd allocation with information on the allocation.

Dave

Share this post


Link to post
Share on other sites
Thanks for the quick reply. I think I see where I'm making the mistake. I started writing more to this post but it made me think a bit more... I think I understand now. [smile]

Share this post


Link to post
Share on other sites
the truth is that you don't need to know the array syntax. Use std::vector for your container needs. If you really need a block of memory just new/malloc it and use it through a pointer.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jemburula
Theoretically, if I had an array of 10 pointers to ints shouldn't I be able to say:
int *ptr[10];
*ptr = &someInt;
It doesn't let me do that though?


No, that's fine. '*ptr' is equivalent to 'ptr[0]', so that puts the pointer to someInt into the first element of the array. Are you sure someInt is an int (and not itself a pointer-to-int)? ;s

Quote:
Another thing I've been wondering is when you delete dynamic arrays with 'delete [] myArray;' how does it know how how many elements are in the array? Does it record how much you new'ed it with?


Implementation-defined. Dave's post describes one possibility. It will of course have to track the information *somewhere*, but it doesn't have to be anywhere near the actual array allocation.

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!