N
Nishi
hi..
I was experimenting with new and delete operators. I ended up
getting something I didnt understand. I allocated memory to a char
pointer, and even after freeing the memory, I still got 'hello world'
printed twice. Also if I replace char* with int*, the freeing of
memory will generate the second output 0. Why does this happen?? If
using int*, freeing of memory means the value is reset to 0, why
doesnt the same happen in char * ??
using namespace std;
#include <iostream>
#include <conio.h>
char* allocate(char *, int);
int main()
{
char *str;
str = allocate(str, 100);
str = "hello world";
cout << str;
delete []str;
cout << endl << str;
getch();
return 0;
}
char * allocate(char *s, int size)
{
s = new char[size];
return s;
}
I was experimenting with new and delete operators. I ended up
getting something I didnt understand. I allocated memory to a char
pointer, and even after freeing the memory, I still got 'hello world'
printed twice. Also if I replace char* with int*, the freeing of
memory will generate the second output 0. Why does this happen?? If
using int*, freeing of memory means the value is reset to 0, why
doesnt the same happen in char * ??
using namespace std;
#include <iostream>
#include <conio.h>
char* allocate(char *, int);
int main()
{
char *str;
str = allocate(str, 100);
str = "hello world";
cout << str;
delete []str;
cout << endl << str;
getch();
return 0;
}
char * allocate(char *s, int size)
{
s = new char[size];
return s;
}