Members BookmarksPolls Fresher Jobs Funny Pictures MCA Projects New Member FAQ  



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

Advertisements


website counter



C ++ Concept-Memory Management Using New and Delete


Posted Date: 12 Jun 2008    Resource Type: Articles/Knowledge Sharing    Category: Computer & Technology

Posted By: Girish Patil       Member Level: Diamond
Rating:     Points: 3



Memory Management Using New and Delete
The statement int a[100]; reserves memory for 100 integer elements. This very simple approach for data storage, but they have very serious drawback : the memory get allocated before running & arrays requires a continuous memory block. We could not use variables to allocate memory-using array. Even though this good enough when we know number of elements are to be used. In many cases we don’t know much memory we need until run time. C++ provides us approach to obtain blocks of memory using new operator.

The new operator returns a pointer that points the section of memory just large enough to hold the required elements.
The delete operator returns the system whatever memory was pointed by variable

#include
#include

void main()
{
char *str = “Vishwanet Computers Pvt. Ltd. Kolhapur “;
int I = strlen(str);
char *ptr;
ptr = new char[I+1]; //allocate memory & returns pointer
strcpy(ptr, str);
cout<< str <delete ptr; // de-allocate memory
}

Pointers to Objects
Pointers can point to objects. When we don’t know how many objects are to be created, we have to use new operator to create objects at runtime.

#include
class stud
{
private:
char *nm;
int total;
public:
void getdata()
{
cout<< “Enter Name : “;
cin>>nm;
cout<< “Enter Total : “;
cin>>total;
}
void dispdata()
{
cout<<”\n Name : “<< nm;
cout<<”\n Total : “<}
};

void main()
{
stud *s;
s-> getdata()
s-> dispdata()
}


The ‘this’ pointer
Pointer named as this which points to the object itself. A this pointer refers to an object that currently invokes a member function. For example, the function call a.show() will set the pointer ‘this’ to the address of the object ‘a’.
Example

#include

class thispointer
{
private:
char a[10];
public:
void disp()
{
cout<<”The address of object is : “< }
};


void main()
{
thispointer t1,t2;
t1.disp();
t2.disp();
}

Virtual Functions
Virtual means existing in effect but not in reality. This is an important for polymorphism, i.e. giving different meaning to the same thing.
Function & Operator overloading are the examples of compile time polymorphism. The overloaded member functions are selected for invoking by matching arguments, both by type & number of arguments. The compiler knows these functions a particular call at compile time this is called as early or static binding. It means the object is bound to its functions call at compile time.
Virtual function is an example of run time polymorphism. This type of polymorphism the function call is invoked at run time. It is called as Dynamic Binding. Dynamic binding requires use of pointers to objects.
Example:

class base
{
public:
virtual void show() //virtual function
{
cout<<”In base”;
}
};
class derived1 : public base
{
public:
void show()
{
cout<<”\n Derived 1”;
}
};

class derived2 : public base
{
public:
void show()
{
cout<<"\n Derived 2";
}
};

void main()
{
base *b; derived1 d1; derived2 d2;
b = &d1;
b->show();
b = &d2;
b->show();
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: C ++ Concept-Pointers Arithmetic
Previous Resource: C ++ Concept-Pure Virtual Function
Return to Discussion Resource Index
Post New Resource
Category: Computer & Technology


Post resources and earn money!
 
Related Resources


Contact Us    Privacy Policy    Terms Of Use   

SpiderWorks Technologies Pvt Ltd. 2006 - 2007 All Rights Reserved.