Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"new" function 1

Status
Not open for further replies.

teser

Technical User
Mar 6, 2001
194
US

Please explain in as much detail as possible what the new function does.

I see it in linked list programs.

ex:
p= new mynode;
 
new is an operator that allocates memory. The amount of memory allocated depeends on the size of what it is allocating. "new" is used to dynamically allocate memory at runtime so the memory size can change over time and adjust itself to the needs of the program. Unlike with arrays, new does not need sequential blocks of memory to store data, it can have memory locations all over the place.

For an abstract example, lets look at a map of US. With an array, we could call it massachusetts, we could make it have as many elements as there are towns. This would all be packaged up in sequential memory. No need for dynamic allocation because we know what we have before we start.


Now lets suppose that we know we want towns but we are not restricted to just one state AND we will never use more then 100 towns. Overall, there are tons of towns across the US but allocating memory for each town in each state would be time consuming and tough on the memory.

Looking at the US as a bunch of memory, we could have a pointer to BOSTON, Los Angeles, Orlando, ... But looking at the big picture, we are all over the place as opposed to one location (Massachusetts).

we could use new to create a list (a flight pattern lets say) where we add each "NEXT" node in the list as a new destination to fly to.

node* head = new node("Boston");
head->next = new node("Orlando");
node* cur = head;
cur = cur->next;

cur->next = new node("Austin");


We have a flight pattern that has taken just tiny blocks of memory (Boston,Orlando,Austin) and looking at a map of the US we could see 3 little dots representing where we took our memory from. When we have flown to the places, we delete the memory we used as we go along. So, upon departure from boston, we "delete" the boston node and head now points to Orlando. Now that block of memory located at boston has been freed up so someone else can go to boston.


I hope the abstract look at memory sort of explained it, here is what actually happens.

As the program is started, all know memory is allocated. Each spot in memory has an address (a town). When we want a piece of memory we say node* p = new node; We dont care where that memory comes from, just as long as we get it. We give it waht ever attributes it needs and we keep track of it. As we go along we find we need more memory and we say p->next = new node;. With a list implemetation as seen here, we keep track of all the memory by adresses. p may have the address 175 p-> next may be 3432. Then suppose we say p->next->next = new node; and that gets a memory address of 2000 because at some point between the first allocation and the second, some memory freed up at 2000 that could hold enough room for a node. Once we are done, we go thru our list buy going to each "next" node and deleting as we go along.

I hope that helped. New was very hard for me to understand when I first started programming. If i could draw here I would so I could give some examples that helped me.

Good luck,
Matt
 
Many thanks for your detailed answer!
 
new is not a function, but an operator. new allocates memory for variables. for example, to allocate memory to an integer pointer, the code is

int *p;
p = new int;

this allocates memory sufficient to hold an integer. to allocate sufficient memory for say 10 integers, the code is
int *p;
p = new int[10];

this is like an array of 10 ints (just like int a[10]).

memory allocated by new can b deallocated with delete operator. for example the above memory can b released as

delete p;

new can also allocate memory to user defined classes and structures. suppose there is a class called Employee, memory can b allocated as

Employee *e = new Employee

if u want to call a constructor of the Employee class (assume it takes an integer parameter), then u can give

Employee *e=new Employee(5);

this allocates memory, also calls the constructor.

the above code is not the same as
Employee *e=new Employee[5];
this means e is like an array of 5 Employee objects, but in the above code e is just a single Employee object.
hope u find this useful.

luv
Karthik.

 
hi i forgot to mention that in the linked list - only a head pointer is supplied to a linked list which is arbitrarily long. and the aim is to find out if it is looped ,in a more efficient manner - rather than by checking each pointer againsta all the other pointers which would be a quadratic function.
help!!!
longliz
 
How do you implement this:

void insertAfter(NodePtr nPtr);

also
bool insert( int position, const nodeEntry & item, NodePtr headerPtr)
 
The design of the node should be something like

struct Node
{
int data;
Node* next;
};

typedef Node* NodePtr;

The insert after, I assume, does not have everyting it needs OR it is supposed to return a value. One other option is that you are using class Node and not struct Node.

The basic idea of inserting after is

Node* insertAfterMe;
Node* whatToInsert;

whatToInsert->next = insertAfterMe->next;
insertAfterMe->next = whatToInsert;


Hope that helps

Matt

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top