Jump to content
  • Advertisement
Sign in to follow this  
jmau0438

How can I find the end of a dynamic array.

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

I have a quick question on dynamic arrays. I would like to find the end of the array without storing the size within a #define or as a member of a class. Using sizeof() doesn't work, it keeps returning 1 [array/sizeof(datatype)]. I know that when an array is allocated with 'new' it inserts a header at both ends of the array, is there a way to detect this? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by jmau0438
I know that when an array is allocated with 'new' it inserts a header at both ends of the array...

No, it doesn't. What you may be thinking of is the markers used by some compilers in debug builds to help developers identify array bounds violations.

If you allocate memory, keep track of it.

Share this post


Link to post
Share on other sites
Really? Man, that sucks. So I have no choice but to keep track of it somewhere. Alright, now I know. Thanks for the help!

Share this post


Link to post
Share on other sites
Quote:
Original post by Dynx
Or to write your own :)
What advantages (besides experience, perhaps) would that offer over using the dynamic array class offered by the standard library?

Just curious...

Share this post


Link to post
Share on other sites
Using standard library as a new beginner (I assume he is since he posted under here) is as close as it gets to doing a Calculus homework using the solution manual.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dynx
Using standard library as a new beginner (I assume he is since he posted under here) is as close as it gets to doing a Calculus homework using the solution manual.


Well, that's the whole point, isn't it? Using the standard library should make things easier, especially for beginners.

Share this post


Link to post
Share on other sites
A beginner (and when I say beginner, count me in, I mean where are we on the spectrum of knowledge?), not only beginner to programming, should make the early steps as much of a learning experience as it can, rather than as easy as it can.

That's how you make learning experience a valuable one. Probably don't even need to say that that's how you get good at what you are doing.

One more thing that might come up on this discussion (which should be on finding the end of a dynamic array anyway) is that, you actually should think about where you are going to apply the knowledge you gain. I am sure those of FORTRAN, BASIC programmers back then that lived off of other people's innovations cannot find jobs anywhere these days. Ask yourself why.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dynx
A beginner (and when I say beginner, count me in, I mean where are we on the spectrum of knowledge?), not only beginner to programming, should make the early steps as much of a learning experience as it can, rather than as easy as it can.


I agree with you to a point; writing your own container classes can be a great learning experience. However, if as a beginner you set out to make a game (which, granted, the OP might not be trying to do), you don't want to get bogged down in relatively low-level memory management and hacking around with a custom linked-list class. More to the point, as a beginner, writing a game, you can't out-do what the standard library offers in terms of features and performance. There are much bigger, more important lessons to be learned from making a game than reinventing a dynamic array, and it's a hard enough task without trying to make your life any harder on purpose.

So, making your own containers may be a great learning experience, but if you want to make a game, make your game instead.

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!