The point is, "never" is a pretty strong word. And for something as fundamental as dynamic allocation?
The point is, "never" is a pretty strong word. And for something as fundamental as dynamic allocation?
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
I don't know why there is such a big discussion about the matter. A vector contains an array. End of story. There are both in the heap, so what is the real difference? The vector has extra features. If you need them it is better. If you don't then use a new array[]. If the new array[] is "poor" implement it the way you want.
>> If you don't then use a new array[]. If the new array[] is "poor" implement it the way you want. <<
This is bad advice unless you can come up with an example where the automatic memory management of vector is not a benefit over an array with new[].
>> The point is, "never" is a pretty strong word. And for something as fundamental as dynamic allocation? <<
Who said "never"? I don't know about others in this thread, but I think I've made it clear that the point is that I have never heard of a single situation where using new[]/delete[] should be preferred over vector and that I'm opened to ideas if anybody has them. So far for every situation that has been mentioned there is clear rationale for choosing vector.
It has been already mentioned before but the variable sized arrays are not a magic bullet (though it might be for certain uses):
This program is fundamentally broken because it segfaults for a sufficiently large value of x (here it is somewhere around 600000) because the stack size is rather limited. Also, the maximum allowed size of x depends on what other variables and arrays might be around since these also use the same limited memory. And what's worse, there might not be a standard way of finding out how large x can be beforehand (not that I know of).Code:#include <iostream> int main() { int x; std::cin >> x; int v[x]; }
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Vectors has a lot of feature. Every feature though has its cost. Some features of the vector have a memory cost (which isn't a big deal most of the times) some have a cost in speed (which might be a big deal).
For example. When you insert an element in a vector with push_back, some checks need to be made. One check is where to put it. Another check is if the size is sufficient. If not you need to resize. Etc etc.
What if you want a different behaviour. If you want a more advance behaviour you can create a container class, having a vector inside it. If you want a more limited behaviour though? You will have to implement it yourself if you care about performance.
So saying that implementing something might be bad advise is true in one hand. But on the other hand thinking that you should always use a vector even if you don't need its features is not a good idea either.
Generally, use a vector if you want its features. Use a vector if you don't care about performance so much and you just something guaranteed to work. Use something manual, based on the simplest thing available, an array, if you want to gain more performance.
I looked in one site where tests where made which showed that Java was faster than C++ in a code based on calculations with arrays. In that side the guy was saying that the codes where false because vectors where used instead of arrays. Don't know details, but even if you save a few nsec in performance, if some code is executed millions of times you will actually have a difference in performance.
Depending on the need, you need to choose the correct container, because they were built for different situations.
A vector's push_back is no slower than manual checks of such type. I'd say they would be of equal performance.
But for a dynamic array, or a buffer, a vector fits the description perfectly.
Vectors are good for appending and deleting the last element, but not in the middle.
When dealing a lot with inserting or deleting in the middle, a linked list is better, for example.
Pretty much everything you need for these kind of things already exist in the standard library.
That is true. On the other hand, these checks are unavoidable, even if you implement a dynamic array by hand. If they are avoidable, then push_back() was avoidable to begin with, so you have simply used the wrong function.Originally Posted by C_ntua
Perhaps you would like to read Stroustrup's answer to the FAQ Why are the standard containers so slow?Originally Posted by C_ntua
That's true, so you should profile your code before trying to make a messy optimisation like substituting hand crafted functionality for an existing well tested container.Originally Posted by C_ntua
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>> But on the other hand thinking that you should always use a vector even if you don't need its features is not a good idea either. <<
Obviously there are shades of gray, but in the simple question of which is better, a vector or an array created with new[], the answer is vector. It is not less efficient (if you think it is please provide evidence). It is more robust.
Just because it has extra features doesn't mean you have to use those extra features. Nor does it imply that the performance is worse when you don't.
There are occasions where neither new[] nor vector will be appropriate for you task. I'm not sure that's relevant to the original point, though. If you have a situation where you need to implement your own functionality in terms of either new[] or vector, then vector will almost certainly be the better choice.
You are worried about vector's performance, but I have yet to see a scenario where it caused a slowdown when compared to the equivalent implementation with new[]. If you have an example, please feel free to provide it.
Your example of Java vs C++ might have been referring to plain, statically sized arrays. Those are different than arrays created with new[] or vector. You should also look up the "premature optimization is the root of all evil" quote. If you can provide an actual example where there is a significant time difference, then in that case you might have reason to choose new[]. But as I've said repeatedly, I've never seen such an example.
I don't understand the resistance. It seems to be only based on misunderstanding of vector and what it does. What is wrong with advocating the better choice?
"better" is subjective. And a point of religious contention when it comes to language bigotry. You guys are funny.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
That is true. But if you disagree, then you should provide a clear argument and to how manual memory management of dynamically allocated arrays is (generally) better than the use of std::vector. I have already pointed out that in the face of exceptions and pure human error, it is difficult to get such manual memory management absolutely right."better" is subjective.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>> "better" is subjective. And a point of religious contention when it comes to language bigotry. You guys are funny. <<
"better" is not subjective. It is objective. It is based on facts and reasoning.
Perhaps in many cases there is no clear-cut general answer to the question "which is better" but in this specific case nobody has provided any evidence (that hasn't been refuted) for the opposing argument.
If you think that there is language bigotry here, I don't know what to say. We're only talking about a single language.
dynamic is better? that's not at all what I was getting at or meaning to defend. All that was placed before me was to show that there exists at least one case that dynamic is just fine and could be used.
I believe that in a simple function, a buffer can be allocated and freed with no worries. Using a vector for only the purpose of allocation can be defended from an academic standpoint. But really, it isn't necessary and might just be a little bit silly.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
>> All that was placed before me was to show that there exists at least one case that dynamic is just fine and could be used. <<
Actually, the challenge was to find a situation where vector was not better.
I of course agree there are many situations where new[]/delete[] is fine and can be used. I haven't mentioned that in this thread, but have done so at other times. If that was all you were trying to say, then there is no disagreement.
It is my quest to mock the knuckleheaded academic!
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
>> It is my quest to mock the knuckleheaded academic!
Ok. Let me know when you see one.