0

Can I reallocate memory without change the address in C?

I want to create a stack, and increase the size of it dynamically. I don't mind if the size must be multiple of page size. Is it possiple?

Or can I allocate multiple pages on different address and map them into a single continuous space?

mxmxlwlw
  • 31
  • 6
  • When you say "stack", do you mean like a process stack, or the data-structure stack? – Some programmer dude Dec 05 '22 at 09:14
  • 1
    @some-programmer-dude It acts more like a process stack, not a data-structure. So it needs space to grow. – mxmxlwlw Dec 05 '22 at 10:34
  • What if you request block to grow but no room in address space? Allocate as much as you need at once. If memory overcommit is enabled, physical memory is mapped on demand. When you use allocated block in address space, kernel already maps different pages into single continious space. – dimich Dec 05 '22 at 16:05
  • Yes, physical memory can be mapped to logical memory. The problem is I can't decide the space at once, and I want the stack grow gradually. – mxmxlwlw Dec 06 '22 at 02:54
  • 1
    @mxmxlwlw Imagine you map additional pages to your block in address space. What happens to data in already allocated blocks which overlaps with your stack? – dimich Dec 06 '22 at 16:51
  • Yes, you are right. This is not a good way to go. Thank you! – mxmxlwlw Dec 12 '22 at 07:54

0 Answers0