20121001

How to use new and delete in C++

//C++ program to use new //How to use new and delete in C++
int main()
{
// allocate memory for the string
//char *string1 = (char *) malloc(100*sizeof(char));
char *string1;
string1=new char;

// write some data to the string
strcpy(string1, "Hello String world xyz abcdef zxy!");

// display the string
//printf("%s\n", string1);
cout<<"String is:"<<string1<<endl;

// remove the string from memory
//delete string1;
return 0;
}
//C++ program to use new //How to use new and delete in C++
//C++ program to use new //How to use new and delete in C++

//Program in c++ to learn new and delete with example
//how to use new  and delete with example in C++
#include
int main()
{
char name[10]="xyz";
cout<<("\n You entered: \n", name);

char *p;
p=new char[strlen(name)]- 1;// Note the Sysntax
strcpy(p,name);
cout<<("\n copied string is: \n", name);

return 0;
}


Previous                             Home                               Next

No comments:

Post a Comment