What is line 5, 6, 7 telling us ?
Doubt in my code
hello @yashsharma4304
int* ptr = new int[100];
cout<<&ptr<<endl;
//&ptr will give address of ptr variable
cout<<*ptr<<endl;
// since ptr is having base address of array, *ptr will give u value present at that address (at index 0) which is grabage.
cout<<ptr<<endl;
// this will print base address of array becuase ptr is holding base address of array
Ok so if I write this line after assigning the values to dynamic array then it will print 0. But why the base address of dynamic array is changing everytime ? Everytime I run the program does the dynamic array
allocated with new memory ?
yeah the address that u r getting may or may not be same.
it will depend on what all memory blocks are free in ur system accordingly os will allocate memory at appropriate location and give its base address
Ok thank you very much for resolving my doubt
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.