Thread: Dynamic memory allocation

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Another problem with those variable-sized arrays seems to be that they are limited in size (you can have only so much stuff on the stack). Crashes for me if i input one million.
    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).

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Luciferek
    Besides, are there standards related to C++ code? Where do i find those?
    A PDF copy of the 2003 edition of the C++ Standard (ISO/IEC 14882:2003) can be purchased from the online ANSI store for 30 USD.

    Quote Originally Posted by Luciferek
    Besides why are compilers picky in what they allow and what they do and don't allow? Isn't C++ a C++?
    Language extensions are allowed, hence users can choose when compiling. Then, compilers are typically do not fully conform to the C++ Standard, so there are edge cases where things are different when compiled on different compilers, even if the Standard says that they should be the same (or perhaps a language feature is not even available).

    Quote Originally Posted by Luciferek
    So, if I create a compiler that will bastardize the C++ syntax to the maximum it will be ok?
    Since "maximum" is an exaggeration, I think that Microsoft has beaten you to it with Managed C++ and its successor C++/CLI.

    Quote Originally Posted by Luciferek
    By the way, the compiler I used to compile it is Dev C++.
    Dev-C++ is not a compiler, but an IDE whose default compiler is the MinGW port of g++.
    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. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> So, In other words... There is no real advantage to using a new and delete keywords except
    that it is accepted as a standard? <<

    I can think of no advantage to use new and delete for arrays, ever. (Well, almost never.) Use vector.

    I don't have a problem with people using a compiler extension in some cases. Many extensions are things that will end up being standardized in the future, and the reason they get standardized is because users use them when they are merely extensions and the community forms a common practice. In this case, variable length arrays are standard in C (which is why gcc allows them), so it's not like you're using some far-fetched feature that will never work anywhere but your current environment.

    That said, I don't see the point of using variable length arrays in C++ when the much better option of vector is standard and available.

  4. #19
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yeah, but vector might not have the same performance as an array. Don't know, haven't tested it. Though they should be the same at least for big arrays.

    >>I can think of no advantage to use new and delete for arrays, ever. <<
    Isn't a vector also allocated on the stack? What if you want a very big piece of memory? You will have to use new/delete even if you just allocate a vector. Right?

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by C_ntua View Post
    So you are saying this in non-standard in C++?
    Yes, it is.

    Quote Originally Posted by C_ntua View Post
    In any case lets see what are the difference.
    Is the space in both cases allocated from the heap?
    No. Variable arrays are allocated on the stack.

    Quote Originally Posted by C_ntua View Post
    Since the compiler doesn't know the value of a variable then the space cannot be allocated in compile time. So if you time a[v] then the space I assume is allocated at run time.
    My point it, that the "illegal" way function like a new and an automatic delete when the variable goes out of scope, or it is something different?
    Indeed, it must be done at run-time, which, I believe, is why it isn't allowed by the standard. And I don't see it being allowed for anything except perhaps backwards compability in the future.
    And yes, since they are on the stack, they behave like anything else on the stack.

    Quote Originally Posted by Luciferek View Post
    So, In other words... There is no real advantage to using a new and delete keywords except
    that it is accepted as a standard?
    The stack is also limited, and it has limited life-time, so there are two other reasons to use new/delete.

    Besides why are compilers picky in what they allow and what they do and don't allow? Isn't C++ a C++?
    So, if I create a compiler that will bastardize the C++ syntax to the maximum it will be ok?
    The standard defines what C++ is. The compilers' job is to follow that standard.
    Otherwise we would have a thousand compilers each with their own syntax and features.
    Why is this a good thing? I think you get the point.

    More I learn C++, more i get angered by it!
    Bjarne must have been smoking crack or something!

    By the way, the compiler I used to compile it is Dev C++.
    I doubt that. You see, there are many solutions to the problem.
    First is std::vector. It can take a variable at runtime and define its size.
    Second is smart pointers. You can use new to allocate enough storage and hold it in a smart pointer. The smart pointer will free the memory for you.
    Some examples of smart pointers are std::auto_ptr, std::tr1::shared_ptr, std::tr1::weak_ptr.

    Quote Originally Posted by anon View Post
    Another problem with those variable-sized arrays seems to be that they are limited in size (you can have only so much stuff on the stack). Crashes for me if i input one million.
    Wouldn't surprise me, since the stack is only about 1 MB.
    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.

  6. #21
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by C_ntua View Post
    Isn't a vector also allocated on the stack? What if you want a very big piece of memory? You will have to use new/delete even if you just allocate a vector. Right?
    the vector's internal allocation (the array itself) is on the heap. I don't really agree that you should never use old school arrays. But generally you are safer if you use the stl for most containers.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by C_ntua View Post
    Yeah, but vector might not have the same performance as an array. Don't know, haven't tested it. Though they should be the same at least for big arrays.
    No, it will be on par with native arrays, unless extra checks are done to see if you aren't going out-of-bounds.
    I know VC++ produces the exact same code for a vector with operator [] and native arrays (when disabling the extra checks and in release mode, of course).

    >>I can think of no advantage to use new and delete for arrays, ever. <<
    Isn't a vector also allocated on the stack? What if you want a very big piece of memory? You will have to use new/delete even if you just allocate a vector. Right?
    Nope, std::vector allocates its storage on the heap.
    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. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by C_ntua
    Yeah, but vector might not have the same performance as an array. Don't know, haven't tested it. Though they should be the same at least for big arrays.
    For the comparison to be valid, it has to be between a dynamically allocated array and a vector. If you compare a statically sized array to a vector, then of course working with the statically sized array would be faster, but it also has its limitations.

    Quote Originally Posted by C_ntua
    Isn't a vector also allocated on the stack? What if you want a very big piece of memory? You will have to use new/delete even if you just allocate a vector. Right?
    No, the contents of a vector is a dynamically allocated array (it would be tough to do it otherwise since the contents must be contiguous). The vector container itself could be on the stack, of course.
    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

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Yeah, but vector might not have the same performance as an array. Don't know, haven't tested it. <<

    A (dynamic) array might not have the same performance as a vector. In fact, chances are that a vector will be faster than a dynamic array if you need it to resize because it will be difficult for you to do a better job of writing that code than the experts who implement vector.

    >> Isn't a vector also allocated on the stack? <<
    The vector itself is allocated on the stack, but the memory it uses for its data is dynamically allocated "on the heap".

    >> What if you want a very big piece of memory? You will have to use new/delete even if you just allocate a vector. Right? <<
    Nope. That's the point of vector. To manage arrays allocated with new. It handles all that stuff internally.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you need a fixed array, there's also boost::array (or std::tr1::array). It replaces the "native" C-style array.
    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.

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I don't really agree that you should never use old school arrays. But generally you are safer if you use the stl for most containers. <<

    The comment was specific to dynamic arrays using new[]/delete[]. Can you come up with a situation when it makes more sense to use those? I've honestly never heard one.

  12. #27
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by Daved View Post
    The comment was specific to dynamic arrays using new[]/delete[]. Can you come up with a situation when it makes more sense to use those? I've honestly never heard one.
    Sure, reading off a socket when the data structure provides a size for the following data. Same can be said for reading from a file. Plus, there are times when its appropriate to write a non standard container class.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Sure, reading off a socket when the data structure provides a size for the following data.

    Why is new[]/delete[] better for that? I don't think it would be any more efficient, since vector does the same thing under the hood. The vector would be easier to use, though, since you don't have to worry about when to delete the memory.

  14. #29
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by Daved View Post
    Why is new[]/delete[] better for that? I don't think it would be any more efficient, since vector does the same thing under the hood. The vector would be easier to use, though, since you don't have to worry about when to delete the memory.
    because reading off a socket requires a buffer. Unless of course you go one byte at a time.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  15. #30
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can get a buffer from vector, too.
    Code:
    std::vector<char> buffer(numBytes);
    sock.read(&buffer[0], numBytes);

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