Usually we initialize the dynamically allocated array values to NULL using the memset.
We can do it while we allocate memory itself, by adding parenthesis () after the size.
Sample Code snippet:
int* nIntializedArray=new int[3]();
Example:
We can do it while we allocate memory itself, by adding parenthesis () after the size.
Sample Code snippet:
int* nIntializedArray=new int[3]();
Example:
#include "stdafx.h" #include <iostream> #include <string> #include <conio.h> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int i=0; cout<<"Uninitialazied Arrary of int* \r\n\r\n"; int* nUnIntializedArrayptr=new int[3]; for( i=0; i<3; i++) { cout<<nUnIntializedArrayptr[i]<<"\r\n"; } delete[] nUnIntializedArrayptr; cout<<"\r\n\r\n"; cout<<"Initialazied Arrary of int* \r\n\r\n"; int* nIntializedArray=new int[3](); for( i=0; i<3; i++) { cout<<nIntializedArray[i]<<"\r\n"; } delete[] nIntializedArray; cout<<"UnInitialazied char* \r\n\r\n"; char* sztxt=new char[3]; cout<<sztxt<<"\r\n"; delete[] sztxt; cout<<"Initialazied char* \r\n\r\n"; char* szIntialTxt=new char[3](); cout<<szIntialTxt<<"\r\n"; delete[] szIntialTxt; getch(); return 0; }
No comments:
Post a Comment