Next Fit Memory Allocation Issue [closed] - c

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
I am trying to write an algorithm to simulate next-fit memory allocation and I have a specific doubt that I have not been able to find the answer too.
My situation:
A process has been added to the memory at the memories 1/2 way point. Now another process of size 50 wants to be added to the memory. All the holes after the spot we are currently at are all less than size 50. I know that the algorithm will check every hole after the 1/2 way point to see if there is enough space for this new process of size 50. Now my question is, after it has reached the end of memory, will it go back to the start of memory to see there is a big enough hole BEFORE the 1/2 way point where we initially started off.

Yes, that's why it's sometimes also called "rotating-first-fit".
Otherwise you'd be pretty soon running "out of memory" anyway ;-)

Related

C char arrays for user input [closed]

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
I rarely log on and am very new to the C language, therefore I apologize if this is a duplicate question or if this is a silly query.
I'm currently learning C and am hitting a wall with strings. I understand that char arrays are used in place of strings in the language. My question is, is there a better way than to assign an arbitrary value when declaring a char[] for user input(i.e setting the size of the array to one value when the user might enter more or less than that amount of characters)?
If you're on a POSIX system (basically anything that's not Windows), you can use getline(3) to do this. It will automatically allocate a buffer of the right size for you. Otherwise, you'll have to guess a length, allocate that, then read the input up to that length, and if you guessed wrong, use realloc to increase your guess and try again.

What does "memory allocation" actually mean in C [closed]

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 4 years ago.
Improve this question
I was in an interview recently where I was given a piece of paper with a few function signatures and asked to fill in the code, I was also instructed not to "allocate memory".
The question was relatively simple (Smallest value in a list) so I solved it recursively which the interviewers seemed unimpressed by, they seemed to suggest that I could have declared variables on the stack but i was nervous and regrettably didn't press the interviewer on it.
What does it mean to "allocate" memory in C?
Allocate memory means you keep for your program an amount of memory situated in the HEAP section. On the contrary, when you don't allocate memory, new variables are stored in the STACK section.
See What and where are the stack and heap?

Finding unused memory in process memory [closed]

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.

How would I read the contents of a large file in the heap without memory errors [closed]

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.

Is it a good practice to initialize a dynamically allocated structure with all 0 using memset(), if yes what are the benefits? [closed]

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
In a client-server system where its possible that server may populate only some of the IE and rest has to be treated as default value (0) at client side.
For such a system, is it a good idea to do memset(.., 0, ...) of dynamically allocating memory for the received message before copying the contents of message?
There is no need to initialise memory which immediately gets overwritten after initialisation.
Also unused memory does not need to be initialised. ("unused" here means, that it will never be read.)
All other memory needs to be initialised. Whether this would be done by using memset() and writing 0s to it depends on the specific context and use-case.

Resources