7 ms·
> If you actually want to measure the memory allocation in C++, then you need to ask the system to give you s bytes of allocated and initialized memory. You can
by saagarjha 7y ago
> If you actually want to measure the memory allocation in C++, then you need to ask the system to give you s bytes of allocated and initialized memory. You can achieve the desired result in C++ by adding parentheses after the call to the new operator
Is this an actual thing? You can force reification of overcommitted memory just by calling operator() on it?
- ajtulloch 7y agoIt's not `operator()`, it's value initialization. If you're particularly masochistic you can read more about it at https://en.cppreference.com/w/cpp/language/value_initialization https://en.cppreference.com/w/cpp/language/value_initializat....
- saagarjha 7y agoAh, that makes more sense. Thanks.
- Someone 7y agoI don’t think that guarantees the memory gets committed. The compiler/standard library combination is allowed to know how the OS behaves, so if it, say, initializes new pages with 0x00, it can use that information to skip the initialization of that memory.
- inetknght 7y agoThe example given: char *buf = new char[s](); Put into Compiler Explorer: https://gcc.godbolt.org/z/QAs9gz However, doing this is a very strong code smell. At the very least, using `new` and assigning to a raw pointer is a sign that the C++ developer is managing memory manually and is likely to hit a lot of problems including memory leaks or segmentation faults. Also many would forget that this is calling `operator new[]()`, not `operator new()` and might confuse with placement-new `operator new(...)` or `operator new[](...)`. And the developer might also forget that `new` could throw an exception... [0] The developer should instead be using, at a minimum, a `std::unique_ptr<char[]>` [1]. Or, IMO, a `std::vector<char>` which reminds the developer not only of the pointer but also of the count of bytes which have been allocated (.capacity()) and also of the valid range which has been initialized (.size()). IMO, if the developer wanted a pointer to a byte array then it's a lot easier to use `malloc()` than trying to remember all the different ways you can get screwed by `operator new`: std::unique_ptr<char, std::function<void(char*)>> m{ (char*)std::malloc(count), std::free }; [0]: https://en.cppreference.com/w/cpp/language/new https://en.cppreference.com/w/cpp/language/new [1]: https://en.cppreference.com/w/cpp/memory/unique_ptr https://en.cppreference.com/w/cpp/memory/unique_ptr
- burfog 7y agoI gave up on C++ in part because the language changes too much. I paid for lessons on it. The std:: stuff didn't exist, templates were a recent invention, and people were starting to agree that putting the overload keyword everywhere was bad style. Mostly, people were just excited about // comments and cout. I keep hearing about how C++ is supposed to be done, and it seems to change every year. I don't know what people do with the older code as it becomes unfashionable with "very strong code smell". Updating it is risky busywork, kind of like porting to Python 3, with potential for serious bugs. Leaving it in place will make developers differently unhappy. It isn't easy getting everybody on a team to agree on what subset of C++ is OK to use. People will sneak in their must-have feature. You'd have better luck getting agreement between emacs and vi.
- ncmncm 7y agoC++ makes no pretense of ever planning to be "done". "Done" is another word for dead. Even COBOL is still evolving. Failing to keep up with evolving languages is called stagnation. Everyone is free to stagnate, but I do not advise it. You use the subset of the language supported by the compiler you have. Code using newer features is better because the new features were added for sound engineering reasons, not just to be different.
- lmm 7y agoIt's the opposite of sound engineering. Every version of C++ contains a new feature that does 11% of the proper solution because the feature in the last version only did 9% of the proper solution. Despite all that churn the language is still as far behind ML as ever.
- ncmncm 7y agoI see that you have not been keeping up.
- throwaway17_17 7y ago