Allocating memory with new - C++ Operator

C++ examples for Operator:new

Description

Allocating memory with new

Demo Code

#include <cstdlib> 
#include <iostream> 

using namespace std; 

int main(int argc, char *argv []) 
{ 
    int *someInts = NULL; 
    int *temp = NULL; 
    int i; /*  w w  w  . j av a2 s.co  m*/

    someInts = new int[10]; 
    if (someInts != NULL) 
    { 
        i = 0; 
        temp = someInts; 
        while (i < 10) 
        { 
            *temp = i; 
            i++; 
            temp++; 
        } 

        i = 0; 
        temp = someInts; 
        while (i < 10) 
        { 
            cout << *temp << endl; 
            i++; 
            temp++; 
        } 
    } 
    else 
    { 
        cout << "Could not allocate memory ." << endl; 
    } 


    return 0; 
}

Result


Related Tutorials