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!
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*)
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.
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
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 ?
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)
Thank You Sir , your content is rare and precious on RUclips!
best channel i've watched on youtube. thanks
Best tutor for c. Should have made a course and publish on udemy/coursera
There are a few courses on udemy, but I prefer hosting them on RUclips or my website instead
This is so disgusting,thank you to show how the memory works
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!
There's a video on this but it's partially wrong, I will soon be correcting it
Can you explain why converts &t to unsigned char*, not unsigned short*, or just char*?
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*)
Awesome as always.
Are there any plans to cover assembler?
If it's requested a lot I might dive into a bit of assembly
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.
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
Think you so much, I just missed the “ * “ outside the bracket.
address is not store there. * is used to dereference the value stored in the address so the value is store in data
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.
this is good..!!
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 ?
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)