Code About Us Tutorial


  Pointers and References
    What are Pointers?
    Declaring Pointers
    The Indirection and Dereference operator
    What is a Reference?
    Using References
    Using Pointers and References
    The new Keyword
    The delete Keyword
    Passing By Reference
    Passing by Value


   
 

Home > Pointers and References > The new Keyword

December 1, 2009 3:15 am

   

The new Keyword

Okay, now that you have the feel for Pointers and References we can go over more advanced topics. This next part we will talk about using the keyword new to create objects. The new keyword creates memory for the pointer itself, the advantage of using the new keyword is it will keep you from having dangling pointers. Dangling pointers are pointers that have no space in memory to allocate. Here is an example of using the new keyword with a pointer.

 

 
double *n;			//creates a pointer 
n = new double;		//creates space for 1 double
*n = 435.3;			//Assigns value to pointer

I like to write it all as one statement like this:

 

 
double *n = new double;
*n = 435.3;

But you can do it either way you choose. Please Rate this Code:


    Comments for: The new Keyword
Annonymouse User says:
creating an array dynamically can be done this way. int *intvalues=new int[dim]; for(int i=0;i




Add Comment:
Name:
Email:


 «