Sign in to follow this  
jmau0438

How can I find the end of a dynamic array.

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
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
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
You are right, though I must have missed the game development part, which I still don't see what it has to do with this thread.

It is also possible to keep your statement same but to look at it from a different perspective:

"If you are working on a game, don't ask questions about linked lists."

Share this post


Link to post
Share on other sites
Quote:
Original post by Dynx
You are right, though I must have missed the game development part, which I still don't see what it has to do with this thread.


Admittedly, not much. I'm inferring the game development bit by the nature of this site, but the OP could well be doing something else. I'm merely defending the use of std::vector and the standard library as a whole in the real world.

Quote:
Original post by Dynx
It is also possible to keep your statement same but to look at it from a different perspective:

"If you are working on a game, don't ask questions about linked lists."


That's not what I mean to say. If you're using a linked list in your game and have a question about it, then of course the question is still valid. My point is more that if you're using a linked list in your game, you'd be best suited using a std::list, and spending your time on problems that haven't already been solved for you.

Share this post


Link to post
Share on other sites
I agree with you. Innovations should be based on other people's work otherwise we can barely move forward.

For the perspective what I meant to say is that 'I' believe if you are using a code block that is implemented by someone else and you don't know how to implement it yourself, you should not do it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dynx
For the perspective what I meant to say is that 'I' believe if you are using a code block that is implemented by someone else and you don't know how to implement it yourself, you should not do it.


I'm afraid I have no idea how to write an operating system. Does that mean I shouldn't program? I also didn't know how to write a compiler when I started programming...

More to the point, I agree that writing a fully usable container class is indeed a great learning experience. However, writing a fully usable container class in C++ is a minefield, because you can't guess all the problems related to shallow copy, swapping, ACID, exception-safety and similar issues until someone warns you about them.

So, before writing anything general-purpose in C++, I would suggest reading and understanding Guru of the Week first. This one, for instance, is a great read.

Share this post


Link to post
Share on other sites
The memory has to be allocated somewhere, and when you allocate it just remember how big it is, what's wrong with that? Alternatively here's another method if it's too much effort to write safe code:

while( array_ptr++ )
{
Object& it = *array_ptr;
// do something with it
}

Share this post


Link to post
Share on other sites
Quote:
Original post by TheGilb
*** Source Snippet Removed ***


Technically, this code either is called on a null pointer, at which point the increment causes undefined behaviour, or it's called on a non-null pointer, at which point the loop never ends and instead causes undefined behaviour when you leave buffer boundaries.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheGilb
The memory has to be allocated somewhere, and when you allocate it just remember how big it is, what's wrong with that? Alternatively here's another method if it's too much effort to write safe code:

*** Source Snippet Removed ***


That's hideous, and as ToohrVyk says it doesn't even work.

Share this post


Link to post
Share on other sites
If you know for certain that a given pointer has been allocated via malloc(), most compilers supply functions that allow you to determine the size of the allocation from that pointer. For example, MSVC uses the _msize() function and in most GCC distributions you can use malloc_usable_size(). On the other hand, needing to use these functions is pretty much a sign of bad design (unless used for debug consistency checks and the like).

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.


So a beginning driver should first study the internal combustion engine? A beginning painter should first learn to mix powdered metals into oil to make the paints? A beginning musician should first learn about Fourier transforms?

Quote:
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.


... Exactly. :/

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
So a beginning driver should first study the internal combustion engine? A beginning painter should first learn to mix powdered metals into oil to make the paints? A beginning musician should first learn about Fourier transforms?


Actually, as a beginning driver who was curious to learn some stuff about how the engine works while he was taking his driving lessons, I can testify that it's usefull (especially the parts about clutch :D )

Ah, and I think a beginning musician actually starts with the Fourier transforms ;) Just they don't call them that, they have this ... strange ... term for them, scales.

[0.02]

Dynx is right. A beginner SHOULD make the best of the first experiences ... learning to do it yourself helps you get better AND helps you know if you really want to do this ;) (as in: if things get tough but you love what you're doing, you won't quit)

Lots of the experienced people don't follow the 'do-it-yourself' approach because they are under some pressure (time isn't enough, some manager , etc), that's why libraries were made for: help us focus on what we want to do. But if you're a beginner you have all the time in the world and you can really focus on what you want to do: learn more.

[/0.02]

Share this post


Link to post
Share on other sites
I think the writing of a custom dynamic array class should be reserved for school assignments. If you're not going to school to be a software engineer and it's only for a personal project. In this case, quite frankly there's no need to know how things work behind the scenes of the stl vector class.

Share this post


Link to post
Share on other sites

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