CS50 Lecture 4 Memory
This week’ lecture is about addresses. Whenever we create a variable it will have an address on ocmputer’s memory. When we create an integer, computer allocate 4 byte space for this integer. Which means 32 bits. If we want to express this value with binary form it would be so hard to follow. that’s why computer engineers create a new counting system called Hexadecimal. In hexadecimal we have 16 digit to express any number. As hexadecimals looks so similar to decimal numbers weuse 0x notation before we write hexadecimal number. As we can express more numbers with one digit, when we write 0xFF it is equal to 255 in decimal which is the largest number that can be written with 8 bit and 1 byte. So we can express 1 byte with two hexadecimal digits.
Another topic explained on the lecture was Pointers. David says on the lecture that pointers are just the addresses. For example, when we pass data to a function, we pass the data with value. Means we just send a copy of the data. When we use pointers, the address of the data, we just send the actual data. It allows us to control the actual data’s value.
If you look at the code below; we have a function called pointMe. And we check the value of a before and after we run the function. As you can see from the output, the value of a changed after the function runs as we have send the address of the instead of value of a. This is the most useful usecases for pointers.
#include <stdio.h>void pointMe(int *p);int main(void)
int a = 5;
printf("a before: %i\n", a);
printf("a before: %i\n", a);
}void pointMe(int *p)
*p = 10;
a before: 5
a before: 10
In the later part of the lecture David explained some of the use cases for pointers. In C the most popular way to use pointers is strings. In C there is no data type called strings. He explains how we create strings in C with using pointers.
The other popular use case for pointers is FILE IO operations. This is also the key part for the problem set. When we open files we assign a pointer to a file. So we just do some operations on the address of the file instead of file itself.
I don’t know if I wrote it on the other posts but this problem set is hard. You should really understand the pointers and you should be careful about the sequence of your code. The first problem is Filter. We have two version of this problem, the harder version includes edge filter with other filter types. The output of filter will manuplate your image like make it grayscale, blur, sepia, mirror vision and show edges on the image. It is so motivating to see your code makes a real world image filtering.
On the Second problem you will be a CSI crime investigator.
Your mission is to find .jpeg code pieces on a file and try to complete the image with this pieces. During solve this problem and also the Filter problem you will get an intiution about how computer worlds recognize files and how distinguish them.
My solutions for the lab and problem set is on the link below. Hope to see you on Week 5. Happy Coding.