Is it possible that malloc () allocates a buffer that overlaps another buffer allocated with mmap ()?

I plan to use mmap () to allocate a buffer close to a specific address.

I am worried that the buffer allocated with mmap () will overlap other buffers allocated with malloc () or the new operator (C ++). Is it possible?

+4
source share
3 answers

No, this is not happening.

The heap supported by the malloc function is located in virtual mappings that were installed via brk or mmap , so memory areas could be reused if the kernel issued the same block twice through mmap .

+1
source

If you use MAP_FIXED to require mmap to create a mapping at a specific address, then yes it is possible that you are overwriting an existing mapping, such as space allocated by malloc , part of partitions or partitions of a shared library, etc. In principle, there is always an error in using MAP_FIXED if you have not already received the address range by calling mmap without specifying MAP_FIXED (so you know that it belongs to you); in this case, you can intentionally rewrite parts of the display using MAP_FIXED ).

Other answers to everything seemed to miss the fact that you said "close to a specific address", which to me means MAP_FIXED . If you are not using MAP_FIXED , please specify how you get the mapping "close to a specific address".

+6
source

You must allocate the memory that is mapped using malloc. malloced memory will not overlap. So no, everything will be fine with me.

+1
source

Source: https://habr.com/ru/post/1396877/


All Articles