Welcome to Dream.In.Code
Become a Java Expert!

Join 149,493 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,356 people online right now. Registration is fast and FREE... Join Now!




How to Dynamically Allocate Memory

 
Reply to this topicStart new topic

How to Dynamically Allocate Memory

dazedstate
19 Mar, 2007 - 02:52 PM
Post #1

New D.I.C Head
*

Joined: 17 Mar, 2007
Posts: 18


My Contributions
Does anyone know how to dynamically allocate memory as a single array in the system stack using Java? I cannot use Heap memory for this or the operator new(malloc for c).

In psuedo code(built for Ada) i have this..
CODE

initialize Avail to omega and PoolMax to L(0), the base address of the storage pool
   on request for memory, Pt <= Avail becomes
             if avail /= omega, then
                  begin
                       Pt := Avail, Avail := Link(Avail)
                  else
                       PoolMax := PoolMax + c; where 'c' is noded size
                       if PoolMax > SeqMin then
                           report overflow;
                       else
                           Pt := PoolMax - c
                       end if
              end if


Thanks!
User is offlineProfile CardPM
+Quote Post

Dark_Nexus
RE: How To Dynamically Allocate Memory
19 Mar, 2007 - 02:59 PM
Post #2

or something bad...real bad.
Group Icon

Joined: 2 May, 2004
Posts: 1,309



Thanked: 3 times
Dream Kudos: 625
My Contributions
i am assuming you need integers, if not, the example still works for all types and classes

CODE

Integer array = new Integer[x];


x denotes the number of elements to exist in the array.
where x is an integer, and x > 0

this uses the "heap" however, so i'm not sure if this is what you're looking for exactly.
User is offlineProfile CardPM
+Quote Post

dazedstate
RE: How To Dynamically Allocate Memory
19 Mar, 2007 - 03:46 PM
Post #3

New D.I.C Head
*

Joined: 17 Mar, 2007
Posts: 18


My Contributions
Unfortunately I can't use that, so what I've done is created the class Node within the Stack class that I need to use a "Dynamic Allocation Algorithm" in..
CODE
    
private class Node {
        private String info;
        private Node next;
        public Node(String info, Node next) {
            this.info = info;
            this.next = next;
        }
    }


Then for the push method in the Stack class..
CODE

top = new Node(info,top);


But I don't know if you could call that dynamically allocating memory, so I don't think it's the right way.

I've checked out a couple of books from the library to read..

What I'm working up to is to put multiple stacks in the system memory, then upon overflow, dynamically reallocate the space and repack the stacks.
User is offlineProfile CardPM
+Quote Post

keems21
RE: How To Dynamically Allocate Memory
19 Mar, 2007 - 08:03 PM
Post #4

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
I just started learning C a few weeks ago, and I think I know what you're asking about dynamically allocating memory.

Because everything in Java is done on the fly, you don't need to declare all of your variables before you use them.

For instance, in C you would do something like this:
CODE

#include <stdio.h>
int main()
{
  int x;
  int y = 10;
  int z = x + y;
  for(x = 0; x < y; x++)
    printf("x + y = %d", z);
}


In java, you can just declare all of these as you go:
CODE

public static void main(String[] args)
{
  int y = 10;
  for(int x = 0; x < y; x++)
  {
    int z = x + y;
    System.out.println("x + y = " + z);
  }
}

So as a roundabout way of answering your question, Java dynamically allocates memory on its own.

So, whenever you need to add a node in your chain for the stack, all you need to do is create a new node with your given constructor, then declare
CODE

someOtherNode.next = newNode;

and this will progressively make your stack larger.

Likewise, when you remove a node from the chain, all you need to do is make sure that nothing is pointing to it. Because you're using a stack, I assume that you'd only be worried about removing the last node in the chain, so a good way to do this would be something like:
CODE

secondToLastNode.next = null;

Which would make secondToLastNode the last node in the chain. After this is done, the newly discarded node will be trashed by Java's automatic garbage collection (so you don't have to call free() like you would need to in C).

I hope that this was what you were looking for. Feel free to post any more questions and I'll do my best to help.

This post has been edited by keems21: 19 Mar, 2007 - 08:04 PM
User is offlineProfile CardPM
+Quote Post

dazedstate
RE: How To Dynamically Allocate Memory
21 Mar, 2007 - 12:22 PM
Post #5

New D.I.C Head
*

Joined: 17 Mar, 2007
Posts: 18


My Contributions
Okay, so using what we have so far, how could I create N stacks?

CODE

int n; //number of stacks to be created
int i;
for (i = 0; i == n; i++) {
     stackClass stack;
     stack = new stackClass(M);
}

though I know it doesnt work.

I need to create a new instance of the class, stackClass, 4 times. stack1 stack2 stack3 stack4. how would I do this?

This post has been edited by dazedstate: 21 Mar, 2007 - 12:23 PM
User is offlineProfile CardPM
+Quote Post

keems21
RE: How To Dynamically Allocate Memory
21 Mar, 2007 - 12:53 PM
Post #6

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
QUOTE(dazedstate @ 21 Mar, 2007 - 01:22 PM) *

Okay, so using what we have so far, how could I create N stacks?

CODE

int n; //number of stacks to be created
int i;
for (i = 0; i == n; i++) {
     stackClass stack;
     stack = new stackClass(M);
}

though I know it doesnt work.

I need to create a new instance of the class, stackClass, 4 times. stack1 stack2 stack3 stack4. how would I do this?


Given that you know how many stacks you want, you can do this one of two ways.

The first is to create 4 new stack objects:
CODE

Stack stack1 = new Stack();
Stack stack2 = new Stack();
Stack stack3 = new Stack();
Stack stack4 = new Stack();


The other way, is to create an array of stacks.
CODE

Stack[] stackArray = new Stack[4];
stackArray[0] = new Stack();
stackArray[1] = new Stack();
stackArray[2] = new Stack();
stackArray[3] = new Stack();


Either of these would work equally well. The first one has the advantage in that you can name the stacks anything you want, so depending on what type of information or the type of data you are holding, you can easily differentiate between the stacks. The second method will work well in that you can easily go through the stacks in any type of loop.

If you want to, you could actually have two pointers that point to the same stack object. Look at this:
CODE

Stack[] stackArray = new Stack[4];
stackArray[0] = new Stack();
stackArray[1] = new Stack();
stackArray[2] = new Stack();
stackArray[3] = new Stack();

Stack stack1 = stackArray[0];
Stack stack2 = stackArray[1];
Stack stack3 = stackArray[2];
Stack stack4 = stackArray[3];


In this instance, you still have only 4 stacks, but each stack has two different pointers accessing it. This means that you can do something to the first stack by calling either stackArray[0] or stack1. That way, you have the best of both worlds in that you can clearly see which stack you are accessing by name, but you can also iterate through the array of stacks in some form of a loop.
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: How To Dynamically Allocate Memory
21 Mar, 2007 - 12:56 PM
Post #7

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
first of all your loop needs a condition, and i==n is not a condition which is reasonable.

*although your answer does work keems, it is better to be as dynamic as possible, as the number may not always be 4.

To create 4 stackClass objects:
CODE

int n = 4; //number of stacks to be created
stackClass stack[] = new stackClass[n];
for (int i=0;i<n;++i)
{
     stack[i] = new stackClass(M);
}

you will now have an array of type stackClass with each element of the array being a new stackClass with the value of M passed to it.

This is the best java can do in dynamic memory, you cannot use pointers and other such things that are in C/C++ to have dynamic allocation since they do not exist within java.
User is offlineProfile CardPM
+Quote Post

dazedstate
RE: How To Dynamically Allocate Memory
21 Mar, 2007 - 01:07 PM
Post #8

New D.I.C Head
*

Joined: 17 Mar, 2007
Posts: 18


My Contributions
Thanks for the fast reply guys! That is exactly what I was looking for. smile.gif


My next task is to print the contents of a stack. What I have so far is
CODE

        Node x;
        for (x = top; x.next == null; x = x.next;)
        {
                System.out.println(x.info);
        }


I believe this should work, but I keep getting a ')' expected error.

User is offlineProfile CardPM
+Quote Post

dazedstate
RE: How To Dynamically Allocate Memory
21 Mar, 2007 - 02:27 PM
Post #9

New D.I.C Head
*

Joined: 17 Mar, 2007
Posts: 18


My Contributions
Worked when I put it in it's own method..
CODE

public void print()
  {
    Node x;
  
    for (x = top; x.next == null; x = x.next)
    {
      System.out.println(x.info);
    }
  }


gah, just realized what the problem was, I put a ; at the postcondition when I wasn't supposed to, hehe.

This post has been edited by dazedstate: 21 Mar, 2007 - 02:28 PM
User is offlineProfile CardPM
+Quote Post

keems21
RE: How To Dynamically Allocate Memory
21 Mar, 2007 - 02:38 PM
Post #10

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
CODE

for (x = top; x.next == null; x = x.next;)


That was your error in your other post. There shouldn't be a semicolon after x = x.next. You fixed it in your next post though.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 05:51PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month