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.
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
The syntax of malloc is malloc(num*sizeof(data_type))
I wonder why you need to specify a size at all. You could just specify a "large enough" number and everything will run fine.
The syntax is malloc(size). The only reason you're writing it as, for instance malloc(10*sizeof(int)) is because you're very rarely interested in the exact amount of bytes, but rather the amounts of elements. This also makes it portable and easier to change.
If you know that the size of int is 4 bytes (can vary from system to system) it is perfectly valid to write malloc(40) instead of malloc(10*sizeof(int)) but this can cause bugs if you recompile for a different system, or later decides that you need a long instead of an int.
And you need to specify a size. When you're invoking malloc(20) you will get a pointer that points to a chunk of memory that's 20 bytes. If you later realize this is not enough, you can use realloc to change the size. But you do need to specify a size, cause otherwise the OS will simply not know how much memory to give to you. Also, if malloc fails, it will give you a NULL pointer. One reason to fail is that you ask for more memory than currently available.
You CAN ask for more memory than you need. But what's the reason? The consequence will be that your program will demand more memory. That's not a good thing.
Sidenote:
You MAY get more memory than you request. malloc guarantees that you get at least the amount you ask for, unless the allocation fails. However, you should - for obvious reasons - never count on it.
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 is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
When you load character from an array in mip does the data still exist at that position in the array ? if not, how can you loop thru the array and get each character within the array ? thanks (:
Though your question seem silly, it is actually a very legitimate question!
Form an outside perspective modern memories have a non-destructive readout.
This means that reading a memory location doesn't destroy the data held there.
So reading from an array won't destroy the item read.
Out of curiosity it is funny to note that internally, depending on the memory technology, reading may be a destructive operation (the common DRAM and the old Magnetic core memory are an example1) and that there exists (and existed) destructive memories.
MIPS could run in a system with destructive readout, that would be tricky however since MIPS is a Von Neumann architecture, instructions are read from the same memory where data are.
So reading an instruction would also destroy it.
Though one can arrange a mixed system where code is run from a non destructive memory and data is in a destructive one, such configuration is so unusual that you can safely assume that it wont never happen.
1 Read-only memory like ROM, PROM and in general non-volatile memories have non destructive reading (so do Flash ROMs). In general memory that stores "charges" have destructive readouts.
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
So I wonder what happens to the memory that is used in functions. I am writing a multi-threaded program and I wonder what happens if I just call a functions and its memory after it returns.
"Automatic" storage -- variables you declare directly rather than explicitly allocating from the heap -- is obtained from the stack, and essentially goes away when the function exits.
Anything you explicitly malloc() MUST eventually be explicitly free()d, once and only once. It's your responsibility to structure your code so that happens correctly. If you don't intend to use it after the function exits, you should free it before the function exits. If it's part of a larger data structure, or being returned to the caller, you need to design your program to be aware of this and clean up after itself when that block of memory is no longer needed.
If you allocate, you must free -- or must document clearly whose responsibility it is to free the memory when they're done with it.
(Note that this is very different from Java and other "garbage-collected" languages, where memory is automatically recovered when nobody is actively using it.)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
What are the advantages/disadvantages of mapping a whole file once vs. mapping large blocks when needed in an algorithm?
Intuitively, I would say it makes most sense just to map the whole file and then let the OS take care of reading/writing to disk when needed instead of making a lot of system calls since the OS is not actually reading the mapped file before accessed. At least on a 64 bit system where the address space isn't an issue.
Some context:
This is for an external priority heap developed during a course on I/O algorithms. Our measurements shows that is slightly better to just map the whole underlying file instead of mapping blocks (nodes in a tree) when needed. However, our professor does not trust our measurements and says that he didn't expect that behaviour. Is there anything we are missing?
We are using mmap with PROT_READ | PROT_WRITE and MAP_SHARED.
Thanks,
Lasse
If you have the VM space, just map the whole file. As others have already said, this allows the OS the maximum flexibility to do read-aheads, or even if it doesn't it will bring in the data as required through page faults which are much more efficient than system calls as they're being made when the system is already in the right kernel context.
Not sure why your professor doesn't expect that behaviour, but would be good to understand his rationale?