Re: Noob learns to code in 3 months
Posted: September 8th, 2017, 2:07 pm
hmm that makes me think I might've misunderstood him. Will try to message him and see if he can clarify
Learn to program or die trying
https://forum.planetchili.net:443/
I had a debate on The Cherno's youtube channel a while back about this. I called him out for claiming to teak C++, but instead of using std::string he used const char*.chili wrote:This is actually kind of worrying...Yumtard wrote:Teacher mentioned that it's preferred to pass char pointers than std::string references so I changed all the std::strings om my code to char*
https://stackoverflow.com/a/23383614/3773305
This is his only response to my initial comment, but others did reply one in favor of teach strictly C++ and the other defending the youtuber.The Cherno wrote: I usually don't reply to comments that start out with something as stupid as "you didn't write exactly the code that I would have so I stopped watching halfway through the video", but since you took the time to write such a long comment I thought I'd reply. If you have your own ideas, great, you don't need to watch my video or other people's videos at all. However this C++ series is for people who actually want to learn how to write good code.
Yes, I could have used std::string. However why on earth would I go through the process of constructing an entire object and causing memory allocations for something as simple as providing std::cout with constant data that would simply be stored in the binary? This may seem like a petty optimization, but there's absolutely nothing wrong with using a const char ptr in C++ code. You should probably also know that most C library features are much faster than their C++ equivalents, which is why everyone in the AAA games industry relies mostly on them.
This series is not your typically "hey lets learn C++ by limiting ourselves only to the newest features". No. This series is about using all that C++ has to offer to write the fastest and "best" code possible. Of course the word "best" is subjective, but it's based on my personal experience in the industry and on the way that I like to do things.
So if you're willing to actually listen you might learn something new from this series, and that is my hope, however if you start disputing "stylistic" differences then that's not going to do anyone any good.
Courier wrote: You're right because std:string is a type that stores char-like elements with known size and capacity, while const char* is just a pointer to const char data that is expected to be null terminated. I think nowadays for C++ is highly recommended to stick to "modern C++" and leave low level details for later, just like one would do in any other language. Kate Gregory in “Stop Teaching C" at 9:45 talks exactly about this point
albinopapa wrote: Yeah the const char * thing doesn't necessarily mean a C string either. It could just be a memory pool for instance. Granted, I've seen others around the internet using std::string in a similar way, but for the most part, if your functions request a 'const std::string &' you know what data the function is requesting. This comes into play when you aren't the one creating the functions
The last sentence about std::make_unique having a price confuses me. I've read up on it and it's no different than calling std::unique_ptr<type>(new type) with added benefits of exception safety if I recall correctly.eatplayhate wrote: Using a const char* as a null terminated string is an idiomatic approach and wouldn't confuse anyone, especially if you have the context of a function named "Log". The small-string optimization in std::string is not mandated by the standards committee and is not present in many STL implementations including libstdc++ (although yes, it is present in the MSVC++ runtime and libc++).
Speaking as a game developer, one of the issues I see with programmers who become a bit militant about "modern" C++ is that they are missing out on many of the things that make C++ such a useful language for high performance applications - it is a mixed paradigm language which allows you to express fairly complicated ideas via things like templates and lambdas, while also allowing you to seamlessly go down to a lower level (C-style) subset of the language when you need or want to.
In this particular instance, using a std::string would require changing the function signature to use a const reference (because passing strings by value is needlessly expensive), and because the small string optimization isn't guaranteed, it may very well cause a heap allocation just for the privilege of packaging a constant block of char data in an object which doesn't add anything to the equation as the function is simply forwarding it to another system which is quite happy with a null terminated char array. In addition, taking a const char* is perfectly functional for users who have std::strings, as there is a simple and effectively free conversion from std::string to const char* via the c_str() method.
On your other examples - I would be very surprised if when he gets to the point of needing a dynamically resized array that he would do anything other than using a std::vector, but bear in mind that it is just one option available to you - many games do not use the "normal" STL because they want either custom allocator behaviour or guaranteed performance characterists (see EASTL, which is now open sourced). I also regularly use manually created arrays, either via new() (often to allow me to pack several types of objects together) or even using alloca() when the size is bounded but the count is still variable.
Finally, I have no problems with people using or wanting to use things like std::make_unique or whatever, but often these come with a price which is rarely considered, and it is important to have all the tools available to you that will allow you to make the best choice.
You pay with your time and effort. For 350 dollars a month - at least where I live - I could survive in a shared apartment without electricity eating potatoes only(I guess in Sweden it is the same). Sleeping under a bridge might be an option but not in winter -haven't tried that yet though so maybe it's possibleYumtard wrote: Just wanna correct your last sentence. "That is what you're paying for" to "That is what you're getting paid for"
I live in Sweden so I get about a 350 dollar salary to study
Quite rude that cherno guy.albinopapa wrote:
I had a debate on The Cherno's youtube channel a while back about this. I called him out for claiming to teak C++, but instead of using std::string he used const char*.
The Cherno wrote: I usually don't reply to comments that start out with something as stupid as "you didn't write exactly the code that I would have so I stopped watching halfway through the video",[..] If you have your own ideas, great, you don't need to watch my video or other people's videos at all. However this C++ series is for people who actually want to learn how to write good code.
[..]
So if you're willing to actually listen you might learn something new from this series, and that is my hope, however if you start disputing "stylistic" differences then that's not going to do anyone any good.
This is ofc true And yeah I obv have to take student loans in order to pay the bills since I wont have the time/energy to make any money on the side.krautersuppe wrote: You pay with your time and effort. For 350 dollars a month - at least where I live - I could survive in a shared apartment without electricity eating potatoes only(I guess in Sweden it is the same). Sleeping under a bridge might be an option but not in winter -haven't tried that yet though so maybe it's possible
Code: Select all
class Inventory
{
public:
void AddGun(WeaponData* data_in)
{
if (Empty())
{
pFirst->pNext = new Gun(data_in);
pLast = pFirst->pNext;
pLast->pNext = nullptr;
}
else
{
pLast->pNext = new Gun(data_in);
pLast = pLast->pNext;
pLast->pNext = nullptr;
}
}
bool Empty();
private:
Weapon* pFirst = nullptr;
Weapon* pLast = nullptr;
};
This optimization is present in msvc and gcc's libstd as well afaik. Generally called short string optimization (SSO).albinopapa wrote: I think they also have a short vector, where if you only need a few items, it uses the stack but once it grows beyond the stack allocated size, it allocates on the heap making smaller vectors faster and still allowing growth and versatility.