Stack and Heap Memory | Coding Concepts

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 ม.ค. 2025

ความคิดเห็น • 19

  • @ismailtemuroglu3432
    @ismailtemuroglu3432 5 หลายเดือนก่อน +4

    It was a simple and understandable video, I liked it.

  • @jonathanfogel6265
    @jonathanfogel6265 5 หลายเดือนก่อน

    Thank you! I've had to look this up many times because It's easy to confuse details between the two. This was a very concise explanation, with bite sized examples that helped make your points.

  • @AllemandInstable
    @AllemandInstable 5 หลายเดือนก่อน +1

    I really like these, it is actually quite difficult to find concise, beginner friendly explained concepts of fundamentals of programming which have much better leverage on getting good than "learning a language"
    I wonder whether how to efficiently store data, when to cache results, ..., is also considered to be "coding concepts"
    anyway looking forward for the next videos

  • @JonatanMachado
    @JonatanMachado 5 หลายเดือนก่อน

    Great explanation ❤

  • @aidan-131
    @aidan-131 4 หลายเดือนก่อน

    nice vid mate, cheers

  • @lukasderivia
    @lukasderivia 5 หลายเดือนก่อน

    Cool video cheers

  • @ThomazMartinez
    @ThomazMartinez 5 หลายเดือนก่อน +1

    when does something go into stack or heap? maybe video?

    • @CodingWithTom-tn7nl
      @CodingWithTom-tn7nl  5 หลายเดือนก่อน +2

      This depends on the language.
      c/c++: variables will be on the stack unless you specify.
      Javascript and other garbage collected langauges:
      basic types: Number, Boolean, BigInt - stored on the stack
      arrays and objects - stored on the heap but have references on the stack.
      In some languages classes are stored on the heap and structs on the stack but it always depends on the language.
      This is all done for you in garbage collected languages.

  • @ThomazMartinez
    @ThomazMartinez 5 หลายเดือนก่อน

    so delete only removes from heap but not stack and for stack it does not matter?

    • @CodingWithTom-tn7nl
      @CodingWithTom-tn7nl  5 หลายเดือนก่อน +1

      Delete will free the heap memory that you specify. Also the reference to that heap memory will be uninitialized on the stack:
      int* number = new int; //creates heap memory
      *number = 1; //sets the value
      std::cout

  • @ThomazMartinez
    @ThomazMartinez 5 หลายเดือนก่อน

    I'm comming from JS, so Vector is new thing, what is diff between vector and array?

    • @CodingWithTom-tn7nl
      @CodingWithTom-tn7nl  5 หลายเดือนก่อน +1

      in c++ a vector is an array that can change in size. So you can add and remove elements.
      For standard arrays in c/c++ you can't change the size of them.
      In Javascript all arrays can be resized which means they are heap allocated. It works very differently in compiled vs interpreted languages.

  • @ThomazMartinez
    @ThomazMartinez 5 หลายเดือนก่อน

    Heap and stack both stored in RAM yes?

    • @CodingWithTom-tn7nl
      @CodingWithTom-tn7nl  5 หลายเดือนก่อน

      Yes, they are essentially the same thing just stored in a different part of ram, but the mechanics are different.
      Stack memory is automatically given to you and freed automatically when the program ends.
      Heap memory needs to be asked for and won't be freed automatically.

  • @ThomazMartinez
    @ThomazMartinez 5 หลายเดือนก่อน

    So is stack then memory of CPU when they have L3 caches and so on? And heap is in RAM?

    • @CodingWithTom-tn7nl
      @CodingWithTom-tn7nl  5 หลายเดือนก่อน +1

      Both stack and heap memory are stored in RAM. They are just in stored in a different part so might not be close together.
      When data is accessed often, it can be put into the cache on the CPU. This is quicker than accessing data from RAM so has a performance benefit. Both stack and heap memory data can both be stored in cache memory but stack memory is usually accessed more often and more likely to be put into cache.
      Data is always stored in RAM and is copied into cache memory to increase performance.

    • @ThomazMartinez
      @ThomazMartinez 5 หลายเดือนก่อน

      @@CodingWithTom-tn7nl oh, but why then stack is faster? and good reference on web i can read about this?

    • @CodingWithTom-tn7nl
      @CodingWithTom-tn7nl  5 หลายเดือนก่อน

      @@ThomazMartinez Stack memory takes no time to create.
      Creating a stack variable:
      -Move the stack pointer down
      -Set the data
      Heap memory takes a lot more to create:
      Creating a heap variable:
      -Ask the operating system for more memory. You need to specify the size of the memory and the operating system needs to find a spare block of memory of that size.
      -Wait for the memory to be given to you.
      -Create a variable that references to the heap memory on the stack.
      -Set the data
      Generally creating the variable is the slow part. When it comes to accessing and changing the data it can depend and sometimes the difference is very small.
      As stack memory is closer together and accessed more often it is more likely to be stored in cache memory which increases performance.

    • @ThomazMartinez
      @ThomazMartinez 5 หลายเดือนก่อน

      @@CodingWithTom-tn7nl wow thank you so much for all this info, looking forward for more content like this,