Reputation: 21
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.
Upvotes: 1
Views: 986
Reputation: 44264
Is it possible to allocate a memory that is next to an allocated memory?
The answer is: No
There is nothing in the C standard that describes how variables are placed in memory. It all depends on the implementation being used so you can't write portable C code controlling how variables are placed in memory.
You can write your own memory allocator which can give the user control over allocation within the memory owned by your memory allocator but it can't be made to work together with variables with automatic/static storage duration like int a;
Upvotes: 1
Reputation: 222312
The C standard does not provide any method to allocate memory at a specific address.
“Allocating memory” is just making arrangements for the memory not to be used for any other purpose. To provide an allocation service, the C implementation has some plan about how to use memory. Some space is planned for constants, some is planned for static objects (variables defined outside of functions or with static
), some is planned for automatic objects, some is planned for instructions of the program, and so on. Some region of the address space is planned for be used with malloc
. The library software that implements malloc
and related routines keeps a database of what portions of that region of address space are available for allocation. When you call malloc
, they choose some space in that region and update that database to indicate that space is no longer available.
To give you space adjacent to an existing variable, the C implementation would have to have some way of recording that that space is not available for any other use. And it would have to ensure that space is not already in use for something else. In typical C implementations, there is simply no provision made for that. All the parts of space planned for things other than dynamic allocation have their own plans about how they are used, and there is no provision for carving out any pieces of them for dynamic allocation.
In special-purpose environments, a C program may be written to manage some or all of its own address space, or the operating system or environment may provide support for this. Even in such cases, the memory is generally managed by arrangements made during system design and building of the software, not during program execution.
Upvotes: 2
Reputation: 21
When you do this (in a function)
int a = 10;
int *ptr = &a;
you're pointing to "stack memory". Stack memory is managed by the compiler, and is "allocated" when you declare variables.
int a = 10;
int b = 10;
int *ptr = &a;
Suppose &a
is equal to 0x0000004. Depending on your compiler, &b
may in fact be equal to 0x0000008, meaning ptr + 1
may in fact be "allocated" already! Whether or not that's true, though, will vary based on compiler and platform. I wouldn't rely on it in real code, but it's fun to play around with.
If you want to allocate new memory on the stack, you can use alloca, which is kind of like malloc but for stack memory. Many stacks grow downwards, so you might be able to allocate ptr - 1
by calling alloca(sizeof(int))
.
Upvotes: 1
Reputation: 278
You should familiarize yourself with the concept of memory layout and which data go where. Your int a
variable will end up in the initialized data segment whereas the dynamic data you are trying to malloc
will go into the heap, so they will have different addresses in memory. (By luck you might hit the heap from your initialized data, especially in the past, and even the stack segment, this is how buffer overflow attacks work, but now with all kind of randomization and memory protection mechanisms.) So, TL;DR, no you can't.
Upvotes: 1