Am working on a big project and am at a particular point within it that requires me to do a couple of things with an array:
Array size [1048]
1)How would I break up an array into 10 sections and further divide those sections into 100 spaces u know like:
0-99, 100-199, 200-299.... 900-999
2) create a queue to keep track of the 10 available sections
3) when you allocate one[space] take it off the queue
4) and return that base memory space... 0, 100, 200

Thanks

Recommended Answers

All 14 Replies

Use a two dimensional array with 10 positions in the first dimension to hold the base values and 100 positions in each of the 10 base positions. The base values will then act as handles to the 100 positions based at that base position.

I had a lot of techical problems earlier trying to edit my post and and couldn't get this up here in time. Here's my attempt

private String[][]Ram = new Ram[10][100]

public void set_ramLoc(int i, String r) 
{
   Ram[i][j] = r;
}
 //return base location of object
public String get_ramLoc(int i) 
{
   return Ram[i][j];
}
public memUsed()
{
   Queue allocated = new allocated();
   //keep track of spaces
   //delete queue when space is allocated

}

verruckt24 thnx for the input. My two-dimensional array now reflects that. I originall had something along the lines of

private String[][]Ram = new Ram[10][1048]
Ram[0] = String[0-99]
Ram[1] = String[100-199]
Ram[2] = String[200-299]
Ram[3] = String[300-399]
Ram[4] = String[400-499]
etc.

2) create a queue to keep track of the 10 available sections

Wouldn't you just want to allocate memory sequentially ? In that case you would not have to keep track of available locations just keep a pointer to the first available location. Then in the method that allocates memory take the number of locations as an input param and bump the pointer by those many positions. In the end return the initial position of the base pointer (before this chunk was allocated) to the calling method.

3) when you allocate one[space] take it off the queue
4) and return that base memory space... 0, 100, 200

These are taken care off automatically.

Wouldn't you just want to allocate memory sequentially ? In that case you would not have to keep track of available locations just keep a pointer to the first available location. Then in the method that allocates memory take the number of locations as an input param and bump the pointer by those many positions. In the end return the initial position of the base pointer (before this chunk was allocated) to the calling method.

I would be better off using pointers to track the non allocated spaces? Ok i read u're explanation but i have no idea how to start writing that in code...more like am not confident in myself to start writing that

To The OP,

It may be helpful for you to perhaps explain in more depth what you are trying to accomplish. I don't understand what you are trying to do here:

private String[][]Ram = new Ram[10][1048]
Ram[0] = String[0-99]
Ram[1] = String[100-199]
Ram[2] = String[200-299]
Ram[3] = String[300-399]
Ram[4] = String[400-499]
etc.

You mentioned arrays and splitting them up, and queues, and "returning the base memory space", as well as allocation. Java has a garbage collector, so normally you add/remove elements to/from a queue and Java does all the memory management for you.

To The OP,

It may be helpful for you to perhaps explain in more depth what you are trying to accomplish. I don't understand what you are trying to do here:

private String[][]Ram = new Ram[10][1048]
Ram[0] = String[0-99]
Ram[1] = String[100-199]
Ram[2] = String[200-299]
Ram[3] = String[300-399]
Ram[4] = String[400-499]
etc.

You mentioned arrays and splitting them up, and queues, and "returning the base memory space", as well as allocation. Java has a garbage collector, so normally you add/remove elements to/from a queue and Java does all the memory management for you.

That was my attempt at implementing a two dimensional array and I was saying it was wrong and i've made the necessary corrections in the code snippet i posted earlier. I am am trying to add certain data into an array and i need to keep track of none allocated spaces within the array so i know what spaces are filled and what aren'tby returning a base memory space like 0, 100, 200, etc.

I dunno how else to explain it without writing 2 pages detailing what the project is about.

That was my attempt at implementing a two dimensional array and I was saying it was wrong and i've made the necessary corrections in the code snippet i posted earlier. I am am trying to add certain data into an array and i need to keep track of none allocated spaces within the array so i know what spaces are filled and what aren'tby returning a base memory space like 0, 100, 200, etc.

I dunno how else to explain it without writing 2 pages detailing what the project is about.

I don't see the corrected snippet. If there is corrected code that you haven't posted, please post it. I see this:

private String[][]Ram = new Ram[10][100]

which seems like it should be this?

private String[][]Ram = new String[10][100];

This won't work either:

Queue allocated = new allocated();

This is closer to correct, but won't work:

Queue allocated = new Queue();

since Queue is abstract. You could do something like this though:

Queue allocated = new PriorityQueue();

Here, what's j?

public String get_ramLoc(int i) 
{
   return Ram[i][j];
}

VernonDozier thanks or u're reply and proof read. I wrote that code snippet late night and it was my first attempt at implementing the solution.
Here's the updated code:

private String[][]Ram = new String[10][100]
 
public void set_ramLoc(int i, int j, String r) 
{
   Ram[i][j] = r;
}
 //return base location of object
public String get_ramLoc(int i, int j) 
{
   return Ram[i][j];
}
public memUsed()
{
   Queue allocated = new Queue();
   //keep track of spaces
   //delete queue when space is allocated
 
}

Ok so i was thinking about going with verruckt24 suggestion to use pointers to track what i need to track and not a queue. Is there a reason why u suggested using a priority queue in this context. I still don't know how to start this part: "I am am trying to add certain data into an array and i need to keep track of none allocated spaces within the array so i know what spaces are filled and what aren't by returning a base memory space like 0, 100, 200, etc.."

I suggested a PriorityQueue not because I thought it was particularly the best type of Queue for the job, but because this won't compile:

Queue allocated = new Queue();

Queue can't be instantiated, so you have to use one of the implementing classes. I just picked one of them to make it compile. See the link below for the Queue class.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Queue.html

I'm still trying to visualize what the project is, so it's hard to offer any suggestions on how to do it, or whether you need to use a Queue (was this your idea or part of the assignment?).

Is this a school assignment from an Operating Systems course trying to emulate what goes on "under the hood" regarding memory allocation or is this a practical assignment where good memory management is required? Again, Java has a garbage collector, so unlike C, often good memory management isn't needed and you just let the garbage collector do its thing and Java manages everything for you.

Regarding using pointers, I am assuming that verruckt24 is referring to keeping track of the array indexes of free and used 100-sized arrays, rather than a traditional C-style pointer, which wouldn't work in Java.

Assuming that you have your 2-D array of String called Ram (total number of Strings = 1000, divided into ten blocks of 100 Strings) and the following blocks of 100 are filled:

0, 2, 5, 7

and that's what you are keeping track of, you could have a Collection (Vector, LinkedList, etc.) of Integer that keeps track of the numbers above (remove them at deallocation, insert them at allocation, check them before allocating or deallocating). So in the above case, I'm guessing that verruckt24 is suggesting that you keep track of index 1, since it is the lowest index not used yet, and allocate that block when you next allocate a memory block of 100.

Anyway, this is all just my best guess of what you are trying to do and of what verruckt24 was suggesting. I could be way off. You need the 2-D array itself and then you need some way of keeping track of what's been allocated and what hasn't. Presumably that would involve using a Collection and that's why you thought of a Queue.

This is an OS assignment and everything you said is exactly what am trying to do at this moment but to no avail. Not the greatest at coding and thats why i need something to start with.

This is an OS assignment and everything you said is exactly what am trying to do at this moment but to no avail. Not the greatest at coding and thats why i need something to start with.

Start with the project specification. It will describe what is required and what is optional. If the spec demands a one-dimensional array, use a one-dimensional array. If the spec requires a two-dimensional array, use a two-dimensional array. If it requires a Queue, use a Queue. Some things will be specified and you have no choice but to comply with those specifications to the letter. Other parts are up to you and you get to decide how to organize everything. You will have been provided with a tester/driver program or you'll need to create one of your own. It needs to be able to handle all possible requests and to reject requests if they cannot be granted. You also need to know whether you are required to create a GUI for this. Find out for sure exactly what the parameters are, in particular whether you are to use a 1-D array or a 2-D array. Find out exactly how memory is requested and whether you can assume that it is always requested in 100 element blocks.

The devil is in the details, so without more details, I can't offer any more than the general advice above.

This method of storing memory in blocks of 100 was an idea from a team member. Basically what we are effectively trying to do is load a process from the virtual disk space( process are read from a file and stored here each with its own process info) into memory(ram). Each process is about 75 words or so and we want to limit the amount of process in memory to 10. Hence our implementation. Each process has a process ID...one of the process info.

With that said how do i start coding this:

Assuming that you have your 2-D array of String called Ram (total number of Strings = 1000, divided into ten blocks of 100 Strings) and the following blocks of 100 are filled:

0, 2, 5, 7

and that's what you are keeping track of, you could have a Collection (Vector, LinkedList, etc.) of Integer that keeps track of the numbers above (remove them at deallocation, insert them at allocation, check them before allocating or deallocating).

This method of storing memory in blocks of 100 was an idea from a team member. Basically what we are effectively trying to do is load a process from the virtual disk space( process are read from a file and stored here each with its own process info) into memory(ram). Each process is about 75 words or so and we want to limit the amount of process in memory to 10. Hence our implementation. Each process has a process ID...one of the process info.

With that said how do i start coding this:

Assuming that you have your 2-D array of String called Ram (total number of Strings = 1000, divided into ten blocks of 100 Strings) and the following blocks of 100 are filled:

0, 2, 5, 7

and that's what you are keeping track of, you could have a Collection (Vector, LinkedList, etc.) of Integer that keeps track of the numbers above (remove them at deallocation, insert them at allocation, check them before allocating or deallocating).

There's not enough detail to answer this question. Without a skeleton of the functions themselves and the specific requirements, as well as the overall design, it's hard to suggest a specific code implementation. So decide what type of Collection you want to use, learn how to use them, and go from there.

http://java.sun.com/docs/books/tutorial/collections/index.html

The number of elements (10) is so small that it may not even matter which you choose. Just pick one, get comfortable with it, and use it. Once you are comfortable with how to use it, THEN try to integrate it into your large project.

Oh ok thnx for the link and everything else. I'll see what i can do and if i have any questions or need help with any particulars i'll post it here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.