Day 39 of Learn C++ from ChatGPT - Smart pointers still own me :(

Поділитися
Вставка

КОМЕНТАРІ • 5

  • @vivvpprof
    @vivvpprof 27 днів тому

    It's a very interesting project to learn a programming language from chat GPT. I think however it is going to be much easier to get a solid understanding of the basics from a book, which is planned out by a human. AI's answers are not going to be consistent and sometimes will just take you on a tangent, to dusty areas that you certainly don't want to go as a beginner. I already had a good understanding of let's say c++ or VBA or other languages before AI was a thing, and let me tell you when I started interacting with it its answers were just... weird. Assuming they were even on point, which they often weren't.

  • @aakkii5271
    @aakkii5271 29 днів тому

    To understand smart pointers you should learn about ownership and RAII. If you want I can give you a couple examples on explaining when are these pointers used.
    EDIT: I only just now found your channel, so I don't know if there are any rules about helping btw
    EDIT 2: Also I can explain the for loop, but again, idk if you take input outside ChatGPT

    • @codesymph
      @codesymph  29 днів тому +1

      I’m ok with commenters helping and giving advice, it adds contrast to the information I receive when learning. Either than that i try to get all my info from ChatGPT

    • @aakkii5271
      @aakkii5271 29 днів тому

      @@codesymph Ok then, basically a couple of terms:
      1. Resource - a piece of data that can be aquired or disposed(
      ex. memory on heap - new aquires, delete disposes ; file - open aquires, close disposes ; mutex - lock() aquires, unlock() disposes etc.)
      2. Now when you have a Resource you have to dispose of it some time, and in big codebases this gets very tricky(then you can even add multithreading and it gets even more trickier).
      3. Example:
      void f(Data& data){
      Data *transformedData = transform(data);
      if(transformedData.isBroken) return;
      transformedData.display();
      }
      Now the ownership of transformedData is in the pointer transformedData, and he is responsible to deleting the memory associated with it, he created it, he owns it. If i were to do Data *otherPtr = transformedData, you kind of lose the idea of who really owns it, well it's the one that aquired it, he is responsible for deleting it.
      4. Ok, in the above function what are the problems(in terms of memory managment), when you have time, try to answer to see the trickiness(and don't use GPT, better get it wrong and I'll answer). After that, I will explain smart pointers in a couple of lines, and you will see how simple they are really