How can I find the end of a dynamic array.

Started by
19 comments, last by Dancin_Fool 16 years, 3 months ago
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."

Advertisement
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.
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.

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.
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}
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.

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.
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).
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. :/
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]
Q: How many programmers does it take to write a nice piece of software?A: MORE.

This topic is closed to new replies.

Advertisement