printf(MEMORY) | How to print memory to the console

Sdílet
Vložit
  • čas přidán 14. 02. 2018
  • I always liked the idea of looking at what is happening behind the scenes with my program. Sure, you could use a debugger, but that's just not that fun and it may hide some details. Today we look at how you can print every byte of a variable to the console.
    Feel free to ask questions in the comments below!
    ---
    IDE used in the video: sourceforge.net/projects/orwe...
    Check out my course over on Udemy: www.udemy.com/quickstart-guid...

Komentáře • 19

  • @shrinidhi14
    @shrinidhi14 Před 2 lety +6

    Thank You Sir , your content is rare and precious on CZcams!

  • @mehmetcivan
    @mehmetcivan Před 2 lety +1

    best channel i've watched on youtube. thanks

  • @shadmanahmed271
    @shadmanahmed271 Před rokem +1

    Best tutor for c. Should have made a course and publish on udemy/coursera

    • @CodeVault
      @CodeVault  Před rokem +2

      There are a few courses on udemy, but I prefer hosting them on CZcams or my website instead

  • @CJKuo
    @CJKuo Před rokem +1

    This is so disgusting,thank you to show how the memory works

  • @chenwu9788
    @chenwu9788 Před 2 lety +1

    Can you explain why converts &t to unsigned char*, not unsigned short*, or just char*?

    • @CodeVault
      @CodeVault  Před 2 lety +5

      It's unsigned because we're reading each byte so the first bit doesn't represent the sign of some number.
      It's char because sizeof(char) == 1 byte so, if you do any pointer operations it will only multiply the operation by 1 (which is the same as not multiplying)... For example:
      int someVariable; // suppose the address of it is 1000000
      unsigned char* p1 = &someVariable;
      unsigned short* p2 = &someVariable;
      // Here both p1 and p2 point to the same address
      p1 = p1 + 1; // p1 is now pointing at 1000001
      p2 = p2 + 1; // p2 is now pointing at 1000002
      So, with p2, I cannot make it point to 1000001 by just addition (I'd have to cast it to a char*)

  • @throatwobblermangrove7508

    Awesome as always.
    Are there any plans to cover assembler?

    • @CodeVault
      @CodeVault  Před 2 lety +2

      If it's requested a lot I might dive into a bit of assembly

  • @davidali133
    @davidali133 Před rokem

    I kinda expected you to explain
    typedef struct __attribute__((__packed__)) {
    int test;
    char k;
    char str[10];
    int* p;
    short sh;
    } Thing;
    Anyways, I love your series! Keep up the good work!

    • @CodeVault
      @CodeVault  Před rokem

      There's a video on this but it's partially wrong, I will soon be correcting it

  • @user-pp5fc8ro4p
    @user-pp5fc8ro4p Před rokem

    Hi, I have a question.
    In line 30, you have made “data” become the address of each byte, and print it in line 31.
    Why does it print the value store in the address but not address itself?
    Thank you.

    • @CodeVault
      @CodeVault  Před rokem +1

      data stores the value of each byte, not the address.
      data = *(((unsigned char*) &t) + i);
      That first character (the *) dereferences the whole structure and returns the value at the address. Thus printf simply prints the value stored at that address

    • @user-pp5fc8ro4p
      @user-pp5fc8ro4p Před rokem

      Think you so much, I just missed the “ * “ outside the bracket.

    • @nikhilpradhan7404
      @nikhilpradhan7404 Před rokem

      address is not store there. * is used to dereference the value stored in the address so the value is store in data

    • @noweare1
      @noweare1 Před 5 měsíci +1

      I think one of the most confusing things about c is they use to define a pointer variable using * and also use * as a dereference operator.

  • @deltaoui
    @deltaoui Před 3 měsíci

    Nice tutorial, just one thing : I think you went too quickly on explaining the extra bytes with 00 after the last attribute of the struct. I am not sure I understand why it was added. Is it related to the fact that we always go in (8, 16, 32, 64, 128,..) in memory and you can't have something in between ?

    • @CodeVault
      @CodeVault  Před 2 měsíci

      It's to do with memory alignment. Basically each struct member is always going to be allocated to a memory address divisible by its size. This improves the speed at which the CPU caches the memory (amongst other things)

  • @MrNaishadh
    @MrNaishadh Před 3 lety +2

    this is good..!!