Is it possible to allocate a memory that is next to an allocated memory?
for example,
there is a pointer that points to an allocated memory like this:
int a = 10;
int* ptr = &a;
so lets suppose &a is 0x0000004 it's next address according to its datatype will be 0x0000008. when I do
int* ptr2 = (ptr1 + 1);
ptr2 will be pointing to 0x0000008 address, but as it is unallocated memory, we cannot use it. Is there a way I can allocate that memory? I have tried doing.
int a = 10;
int* ptr = &a;
int* ptr2 = &(ptr[1]);
ptr2 = (int*) malloc(sizeof(int));
but obviously, this allocates a new memory and not 0x0000008. I have also tried using "new" keyword, but it is same as malloc(), so it doesn't as work as well.
As for the reason why I want to do this or where I want to use this. There is no specific reason. It is just an idea and I want to learn more about it.
I am using Windows 11 OS and Visual Studio 2022 IDE.