Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
My understanding of mmap is that when used on a file it essentially reserves space for that file in memory so that it's able to access it as fast as possible as soon as you need it. but what happens when you mmap a device like dev/mem where it IS the memory, does it then use some other memory to map that memory or is it smart enough to realize that it is mapping ram and doesn't need to store it in memory? What about if you map a RAM disk where it is still memory but it's not grouped in with the regular memory?
/dev/mem is the physical memory. It won't double your address space, it will add the amount of physical memory your machine has to your address space, but your actual memory usage will not go up.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm looking for a reliable way to find unused memory in a C program's process since I need to "inject" some data into somewhere without it corrupting anything.
Whenever I find an area with only zeros in it, that's a good sign. However, no guarantees: It can still crash. All the non-zero memory is most likely being used for sure so it cannot be overwritten reliably (most memory has some kind of data in it).
I understand that you can't really know (without having the application's source code for instance) but are there any heuristics that make sense such as choosing certain segments or memory looking a certain way? Since the data can be 200KB this is rather large and finding an appropriate address range can be difficult/tedious.
Allocating memory via OS functions doesn't work in this context.
Without deep knowledge of a remote process you cannot know that any memory that is actually allocated to that process is 'unused'.
Just finding writable memory (regardless of current contents) is asking to crash the process or worse.
Asking the OS to allocate some more memory in the other process is the way to go, that way you know the memory is not used by the process and the process won't receive that address through an allocation of its own.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
This is maybe a more general question. I have a pointer in my code to store some data. The size of the pointer is determined only while the program is executed and needs to be allocated dynamically.
So I'm using
calloc()
to allocate memory and set it to zero. After the run of the program I'm using
free()
to free it.
Is this usage of memory in a good manner? Or is there something more "nice" one could do?
For dynamic memory allocation, the steps you had mentioned is correct, as #Stargateur pointed use malloc(), if you don't need to initialize the memory allocated.
Also take care to free the allocated memory, on all possible exit condition of the program.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
The question I am asking is extremely simple. Lets just say I wanted to read a large file(6GB) without having the heap run out of memory. How would I do that. (What I am mainly asking is if there is a method to read part of the file clear the buffer and read the next part of the file)
The memory capacity and availability is platform and operating system dependent.
Some operating systems allow for memory mapping a file, in which the operating system manages the reading of data into memory for you.
Reading without overflow is accomplished by using block reading (a.k.a. fread in C and istream::read in C++). You tell the input function how much to read in the block and the function returns the quantity actually read. The block size should be less than or equal to the memory allocated for the data. The next read will start a the next location in the file. Perform in a loop to read in all the data.
Also, verify there is a reason to hold all the data in memory at the same time. Most programs only hold a small portion of the data for a limited time.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How is memory allocated using malloc() ? Who allocates the memory OS or the compiler? Once the memory is freed using free() can it be used by other processes ?
In an OS there are 4 memory regions Heap,Stack,Text and Data. When you use malloc the OS provides the memory from the heap region. Compiler isn t responsible for allocating this memory. When you use free the memory block is returned back to the heap.
Usually, the heap memory is directly supplied by a runtime sub-allocator underneath whatever the OS provides. The sub-allocator is process-specific and does not require a kernel call. If the heap needs more, it has to resort to a syscall to get another chunk from the OS.
It's implementation-specific whether the sub-allocator ever releases chunks back to the OS.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i need to allocate 8 kb to an array and need to get the starting and ending address of the array. then i need to check those virtual memory address are aligned contiguously or not in the actual physical memory. how to do that. please help. thanks in advance.
Use MmAllocateContiguousMemory function. It allocates a range of physically contiguous memory.