heap memory

whats the error in this ?
#include <iostream>
using namespace std;
int main()
{
int size=10;
char *p;
p=new char [size];
char *q;
q=new char [size];
q="Hello";
cout<<*(q+1);
delete []p;
p=nullptr;
delete []q;
q=nullptr;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
	int size=10;
	char *p;
	p=new char [size];
	char *q;
	q=new char [size];
	q="Hello";  // If you want a pointer to a string literal you should
	            // have used a pointer of type const char*. This line is
	            // also a memory leak because you have no way to find 
	            // the memory that q pointed to before, and therefore no
	            // way of deleting it.
	cout<<*(q+1);
	delete []p;
	p=nullptr;
	delete []q; // You are trying to delete a string literal. Only use 
	            // delete on things that you created by using new.
	q=nullptr;
	return 0;
} 
Last edited on
Topic archived. No new replies allowed.