Sunday, 26 July 2015

  • Sunday, July 26, 2015
C++ new operator example: C++ code using new operator to dynamically allocate memory on heap.

C++ programming code

#include <iostream>
 
using namespace std;
 
int main()
{
   int n, *pointer, c;
 
   cout << "Enter an integer\n";
   cin >> n;
 
   pointer = new int[n];
 
   cout << "Enter " << n << " integers\n";
 
   for ( c = 0 ; c < n ; c++ )
      cin >> pointer[c];
 
   cout << "Elements entered by you are\n";
 
   for ( c = 0 ; c < n ; c++ )
      cout << pointer[c] << endl;
 
   delete[] pointer;
 
   return 0;
}
If you allocate memory using new then it will remain allocated until the program exits unless you explicitly deallocate with delete. Above program contains only one function so memory will be deallocated after program exits but we have specifically used delete as it is a good programming practice to deallocate memory which is not required further in the program.

Related Posts:

  • How OLX earns money hello readers, Very interestingly today i got a interesting question "How does olx earns money"?? There are many myths around market that they earns money from the users to whom they provide services, but it is false. Firs… Read More
  • Shekhar Basu appointed as Chairman of Atomic Energy Commission Renowned scientist Shekhar Basu has been appointed as the Chairman of Atomic Energy Commission (AEC) and Secretary of Department of Atomic Energy (DAE). He will replace R K Sinha who superannuates on 23 October 2015. … Read More
  • Download July 2015 Current Affairs PDF Dear Aspirants, Scopian team has prepared a Current Affairs 2015 PDF covering all the latest current affairs happened around India and the World.  The Capsule includes Awards & Recognitions, Scie… Read More
  • Veteran Music Director Ravindra Jain Passed Away Veteran music director and lyricist Ravindra Jain passed away in Mumbai, Maharashtra. He was 71. He was a soulful musician and an accomplished lyricist and renowned vocalist and never had let his visual disability hinder h… Read More
  • Indo-China Joint Military Exercise Hand-In-Hand 2015 India-China Joint Military Training Exercise HAND-IN-HAND 2015 will be conducted at Kunming Military Academy of Yunnan province in China from 12 to 23 October 2015.  It will be the fifth joint military exercise in the… Read More
Related Posts Plugin for WordPress, Blogger...

Vacancy

RBI has announced the recruitment for 134 GRADE B OFFICERS.
 The examination will be in two phases.
 The detailed advertisement will be published on 5th October.

Popular Posts