8/18/2023 0 Comments Vector code c![]() ![]() This kind of behavior is a little more difficult to realize in code, though, so you might want to stick with your current version if you're still in the process of getting a good hold of C++, especially if you don't feel safe working with low-level memory routines. However, if you take a look at std::vector, you will find that it offers a reserve interface that lets you allocate memory in advance without actually constructing any elements. The issue is that your container cannot reserve any memory without (default -) constructing elements. ![]() While you say that your container is a simple clone of std::vector, its behavior differs gravely from the standard container when it comes to allocating and managing elements.While your overall code makes a pretty good impression on me, there are some things that don't look and feel right and, to my mind, deserve another thought or two: You did not ask for this kind of review, so if you're not interested, feel free to simply ignore this part of the answer. If you mean use of the STL and standard algorithms, I'd say there's still work to do. If you mean correct indentation, variable names etc. Depends on what you mean by "general style".your code does not have anything that screams "I'm going to be really slow" at me, so there's little reason to optimize anyway.this implies that you don't actually care too much about performance (if you did, you would have measured it) and.you do not seem to have measured any performance characteristics of the code, and as long as you don't benchmark you don't care about performance.Probably, but I don't want to go into those here as.To correct this, just delete the old array before you assign arr = newarr. After the method returns, the internal pointer points to the newly allocated array while the old one is left abandoned and unreachable in the depths of the heap. This is evident by the fact that you're allocating memory there, but not freeing the old memory. Yes, there is at least one memory leak in resize().Some people argue, however, that this method tends to be very verbose (and it can be, especially with multi-layer templates), so either way is fine. Personally, I prefer splitting declarations and definitions up so that I have an overview of what my class offers at the top. Some people do it one way, some the other. Int new_cap = min(cap, 1) // size-0 vectors wont loop infinitelyįirst of all, lets answer the questions you have: Are there any performance optimizations I could make?.I'm new to lower-level languages that require memory management, so there might be leaks.I have seen people defining methods outside of the class, and only prototyping them inside the class.I'm learning C++, so I decided to make a simpler clone of std::vector.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |