Can we write int* p = 0x61ff08? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can we write int* p = 0x61ff08?

I write a program to print the address of a variable using pointers and delete the pointer to cause intentional memory leak, I copy the address that is print on the consol and copy it to the another program like i have written in the question and try to print it on consol but it didn't work as expected. So can we do this in any other way?

5th Nov 2020, 7:23 AM
Abhishek Dimri
Abhishek Dimri - avatar
3 Answers
+ 7
When doing int *p = 0x61ff08 you are trying to put an integer value to a pointer which is not allowed thus will generate an error. If you really want the pointer to point to that address then you can type cast the address to (int*) But still you if you try to access that memory location{in order to print it to console in your case}, program will throw a segmentation fault as you are not allowed to do so as that memory location is not allocated to you.
5th Nov 2020, 7:38 AM
Arsenic
Arsenic - avatar
+ 5
Abhishek Dimri pointer just points to a memory location. It doesn't matter what it contains. Just like any other variable is meant to store any value of its data type.
5th Nov 2020, 11:30 AM
Arsenic
Arsenic - avatar
+ 1
Arsenic what if that memory location is NULL?
5th Nov 2020, 8:09 AM
Abhishek Dimri
Abhishek Dimri - avatar