Thread: Dynamic memory allocation

  1. #61
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by Daved View Post
    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.)
    Code:
    char buf[v];
    
    //compared to 
    
    std::vector<char> v_buf(v);
    char *buf=&v_buf[0];
    seriously... there's a difference
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  2. #62
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For any normal programmer, I don't think there would be a big difference.
    And the advantages outweighs the disadvantages.
    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.

  3. #63
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you've got no advantage in the case of variable-length arrays.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #64
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Automatic memory management (at the very least, it will delete the memory it allocates).
    No manual handling of exceptions (you do eventually need to resize sometimes, right?).
    Can holder larger arrays than stack allows.
    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.

  5. #65
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by Elysia View Post
    Automatic memory management (at the very least, it will delete the memory it allocates).
    No manual handling of exceptions (you do eventually need to resize sometimes, right?).
    Can holder larger arrays than stack allows.
    memory management / exceptions are not a problem with the C99 variable length.... only the stack concern may hold... but I'll have to verify that its allocated on the stack. I don't believe that's technically possible
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #66
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> seriously... there's a difference <<

    I hardly think the difference between using the &buf[0] versus buf is worth choosing a non-portable solution.


    >> you've got no advantage in the case of variable-length arrays.

    Portability.

  7. #67
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by FillYourBrain View Post
    memory management / exceptions are not a problem with the C99 variable length.... only the stack concern may hold... but I'll have to verify that its allocated on the stack. I don't believe that's technically possible
    Are we discussing the C++ extension or the C99 variable-arrays here?
    If it's the C++, I'm siding with Daved.
    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.

  8. #68
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Honestly I was introduced to variable-lengths in this thread. So I'm not quite sure where it comes from. But portability won't be an issue if the future holds to this standard.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #69
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Are we discussing the C++ extension or the C99 variable-arrays here?

    Does it matter? Obviously we're talking about C++ since we're on the C++ board and comparing to vector. Memory management is an advantage of vector over new[]/delete[]. Portability is an advantage of vector over VLAs.

    >> But portability won't be an issue if the future holds to this standard.

    I'm not sure if they will be included in the C++ standard since C++ has an alternative (vectors), so until they are implemented by VC++ and other compilers and until they are included in a standard draft or technical report they aren't portable enough.
    Last edited by Daved; 10-01-2008 at 12:46 PM.

  10. #70
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    all I'm getting at is inline variable length is preferable to any form of using new. vector included
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #71
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> all I'm getting at is inline variable length is preferable to any form of using new. vector included

    I already mentioned the efficiency question, but that situation is relatively rare and if you don't have such a reason then the portable solution is better.

  12. #72
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by FillYourBrain View Post
    Honestly I was introduced to variable-lengths in this thread. So I'm not quite sure where it comes from. But portability won't be an issue if the future holds to this standard.
    Well, I hope it never makes it into the standard. I really don't.
    We're better off without it.

    Quote Originally Posted by Daved View Post
    >> Are we discussing the C++ extension or the C99 variable-arrays here?

    Does it matter? Obviously we're talking about C++ since we're on the C++ board and comparing to vector. Memory management is an advantage of vector over new[]/delete[]. Portability is an advantage of vector over VLAs.
    Yeah, I was thinking C++ but got a little confused over the whole forth and back here.

    Quote Originally Posted by FillYourBrain View Post
    all I'm getting at is inline variable length is preferable to any form of using new. vector included
    This is where our opinions differ
    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.

  13. #73
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Its kinda funny. I worked several different coding teams in my career. Ada, C, C++, and Java. These people vary in their world view so wildly that it could drive you crazy. All of them are language bigots. All of them are WAY to stringent on their philosophy of proper coding. I've really come back around to a "to each his own" policy lately.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  14. #74
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I've really come back around to a "to each his own" policy lately.

    I'm generally ok with that. Especially for people who know what they're doing. But on a board that helps beginners learn the language I don't see the problem explaining the advantages and disadvantages of different options, especially when there is a clear rationale to prefer one over the other.

    As you might have noticed, at the beginning of this conversation I asked if you knew of a good situation of when to use dynamic arrays. I asked because I know that there are many skilled programmers out there with different habits and situations that might have a scenario that I haven't heard before. It's important to be aware of the other possible solutions. But if there is logic behind one choice and not the other, it is important to make that distinction.

  15. #75
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    The push-back that I have on that is basically driven by my recent experience with Java people. They are really well versed on OOP and design patterns but when it comes to low-level stuff, they're monkeys. Sometimes C++ people fall that way too.

    It is silly to think that every possible container class necessary must already exist in the standard library. Coming down against reinventing the wheel is one thing, assuming that all small building blocks are already invented is another. Didn't the patent office make such a claim half a century ago?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

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