How can I find the end of a dynamic array.

Started by
19 comments, last by Dancin_Fool 16 years, 3 months ago
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.
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.
Really? Man, that sucks. So I have no choice but to keep track of it somewhere. Alright, now I know. Thanks for the help!
Another good reason to just use std::vector.
Or to write your own :)
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...
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.
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.
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.
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.

This topic is closed to new replies.

Advertisement