Dynamic memory allocation

This is a discussion on Dynamic memory allocation within the C++ Programming forums, part of the General Programming Boards category; The point is, "never" is a pretty strong word. And for something as fundamental as dynamic allocation?...

  1. #76
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    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

  2. #77
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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.

  3. #78
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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.

  4. #79
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It has been already mentioned before but the variable sized arrays are not a magic bullet (though it might be for certain uses):

    Code:
    #include <iostream>
    
    int main()
    {
        int x;
        std::cin >> x;
        int v[x];
    }
    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).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #80
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Daved View Post
    >> 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[].
    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.

  6. #81
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #82
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by C_ntua
    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.
    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.

    Quote Originally Posted by C_ntua
    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.
    Perhaps you would like to read Stroustrup's answer to the FAQ Why are the standard containers so slow?

    Quote Originally Posted by C_ntua
    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.
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #83
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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?

  9. #84
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    "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

  10. #85
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    "better" is subjective.
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #86
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> "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.

  12. #87
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    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

  13. #88
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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.

  14. #89
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    It is my quest to mock the knuckleheaded academic!
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  15. #90
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> It is my quest to mock the knuckleheaded academic!

    Ok. Let me know when you see one.

Page 6 of 8 FirstFirst 12345678 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21