Saturday, 7 September 2013

wrong memory allocation new char [n]

wrong memory allocation new char [n]

What is wrong with this program?
#include<iostream>
using namespace std;
void main()
{
int n = 5;
char* p = new char [n];
int i;
for(i=0;i<n;i++)
{
p[i] = 'A'+i;
}
cout<<p<<endl;
}
Why do I get "ABCDExxxx" instead of "ABCDE" ? What is wrong with memory
allocation?

No comments:

Post a Comment