What's going on here it is working but it is still ambiguous. Can anyone tell me more on this or atleast point me to some relevant Sources to know more about it
#include <iostream>
#include <string>
int main()
{
std::string br="\n";
long *a=new long;
*++a=121;
*++a=545454;
*++a=232;
std::cout<<*a<<br<<a<<std::endl;
a--;
//delete a;
std::cout<<*a<<br<<a<<std::endl;
a--;
//delete a;
std::cout<<*a<<br<<a<<std::endl;
delete a;
pause;//macro for windows("pause")
}
This is working because a
is a long pointer pointing to a memory location in heap. Heap consists of a huge chunk of memory to be used at run time. In short, While incrementing a
, a
points to the memory location next(may not be contiguous) to the one allocated by new.
Some References:
https://stackoverflow.com/a/44822977/8584523
https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/