Thread: Dynamic memory allocation

  1. #46
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Calling resize (or specifying a size in the constructor) allocates the contiguous memory immediately, just like new[]. That is guaranteed by the standard.

  2. #47
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    the only disadvantage exists if the resize is not guaranteed to allocate the memory before access.
    resize() is guaranteed to allocate the memory. Not only does it allocate the memory, but it initialises the objects to be inserted.

    The size of the allocated memory internal is not the same as the "size" of the vector.
    That is often true, but this is also true for new[]/delete[], unless you take pains to scale the dynamically allocated array to just the right size.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #48
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I hear you guys and I'll take your word for it, but I gotta tell ya. We're developing an unnatural fear of things that aren't automatically deleted or garbage collected. I say this having spent two years working with java programmers. These people have lost (or never had) important understanding of the code they write.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #49
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't see the logic in using a more cumbersome tool just so that users "understand" what they're doing better. I think any tool will be mismanaged by unqualified programmers. The language (and best practices of that language) should be designed primarily to make the qualified programmer's job easier.

  5. #50
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    new/delete more cumbersome? really?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #51
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I hear you guys and I'll take your word for it, but I gotta tell ya. We're developing an unnatural fear of things that aren't automatically deleted or garbage collected.
    In the face of exceptions and pure human carelessness, I would say that it is a healthy fear. Perhaps you have not read Stroustrup's answer to the FAQ How do I deal with memory leaks?

    EDIT:
    new/delete more cumbersome? really?
    Consider this function:
    Code:
    void foo(size_t n)
    {
    	Widget* widgets = new Widget[n];
    	bar(widgets, n);
    	delete[] widgets;
    }
    Suppose bar() might throw an exception. How would you fix the potential memory leak? Would you even recognise that there is a potential memory leak?
    Last edited by laserlight; 10-01-2008 at 12:12 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #52
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I will concede that it may be safer. For simple methods that read from a network stream though I really don't see the need to worry about it.

    It should be noted that the original poster's variable length arrays does not have the possibility of memory leaks and could be used instead.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #53
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> new/delete more cumbersome? really? <<
    Of course. You have to remember to call delete.

    >> It should be noted that the original poster's variable length arrays does not have the possibility of memory leaks and could be used instead. <<
    But why should they, when vector is standard?

  9. #54
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by FillYourBrain View Post
    I will concede that it may be safer. For simple methods that read from a network stream though I really don't see the need to worry about it.
    Look at laserlight's edit
    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.

  10. #55
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by Daved View Post
    But why should they, when vector is standard?
    That's easy. Using an array is cleaner. It acts like a fixed length array. Why dynamically allocate inside a vector?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #56
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you need a fixed-length array, you should be using std::tr1::array instead!
    Of course, it isn't dynamic and must be determined at compile time.
    And if it isn't determined at compile time, it's dynamic!
    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.

  12. #57
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by Elysia View Post
    If you need a fixed-length array, you should be using std::tr1::array instead!
    Of course, it isn't dynamic and must be determined at compile time.
    And if it isn't determined at compile time, it's dynamic!
    Um, I was talking about variable-length arrays

    edit:
    like the original poster's question.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  13. #58
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by FillYourBrain View Post
    Um, I was talking about variable-length arrays

    edit:
    like the original poster's question.
    So you say, but you also mentioned:
    That's easy. Using an array is cleaner. It acts like a fixed length array. Why dynamically allocate inside a vector?
    An array acts like fixed, and vector does not?
    I don't see the difference!
    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.

  14. #59
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    The variable length vector allows for the buffer of variable length without the possibility of memory leak. Within the scope of a function, this produces the least overhead and is easiest to read. That's what I'm talking about.

    Since its apparently going into the standard or already in it, its a pretty smart addition
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  15. #60
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> That's easy. Using an array is cleaner. It acts like a fixed length array. Why dynamically allocate inside a vector? <<

    I don't think it's any cleaner. It may be more efficient, but I'm not sure that would be an issue in most cases.

    If you find that a variable length array is noticeably more efficient in your application and you know you only need to build with gcc, then I wouldn't have a problem using a variable length array over vector for that situation.

    >> Within the scope of a function, this produces the least overhead and is easiest to read. That's what I'm talking about. <<

    I don't understand this point. How is it easier to read than a vector? (Note: I posted an example earlier of the two lines needed for the vector.)

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, 04: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, 09:32 AM