I'm performing the problem set from pset4 to the (filter less) part. I wish to use malloc() to my advantage to speed up image filtering and maybe check50 will accept my solution to the problem.

I stored every color of the first pixel and each color of the last pixel using the malloc(). I do not know if is a good solution to use malloc().

My doubt

How do I display using printf() the RGB value that is stored in memory with malloc()?

See that in my terminal, the first values are the integers without the malloc And below the memory values with the malloc

Thank You

enter image description here

enter image description here


You seem to be confused about malloc(). malloc allocates memory from heap. You can store anything inside this allotted memory location.

Specifically it expects the following input: number of bytes of memory you want to be allocated.

int *ptr;
ptr = malloc(sizeof(int));
*ptr = 300;
printf("Address of ptr: %p\n", ptr);
printf("Value inside ptr: %i\n", *ptr);

Here int size = 4 bytes So malloc allocates 4bytes of memory and returns the address, which is referenced by pointer ptr

*ptr de-references the pointer to display the value inside this location = 300

Also l_r, l_g, ... are pointers. You get the value by de-referencing the pointer as *l_r

But *&l_r is same as l_r, where you go to address of pointer l_r and print the address it's pointing/referencing to.

  • Thank you for the answer, thank you very much for your help. Your help was very important for me and everyone to complete the problems of the CS50. Thank you again.
    – Code Maker
    Apr 22 '21 at 16:33
  • @CodeMaker not sure if this is legit or sarcasm
    – C--
    Apr 22 '21 at 16:40
  • it is very very legit. CS50 does not have teachers to help us, so people who are willing to help us are of great importance for the conclusion of our problems in CS50. So, thanks again :)
    – Code Maker
    Apr 22 '21 at 16:44
  • @CodeMaker Also forgot. if you're storing RGB values, you should use BYTE *l_r, .. to prevent overflows
    – C--
    Apr 22 '21 at 16:44
  • ok, and Another question arose. If I am going to declare 3 different pointers. Do I need to declare 3 separate malloc ()?
    – Code Maker
    Apr 22 '21 at 16:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .