void test() { int* s; int* p = new int; int* q = p; int* r = new int; *p = 8; s = p; *q = 25; *r = *s; q = r; cout << "*p = " << *p << endl; cout << "*q = " << *q << endl; cout << "*r = " << *r << endl; cout << "*s = " << *s << endl; }
*p = 25 *q = 25 *r = 25 *q = 25
When the function returns.
When it is explicitly deleted using the delete operator.
The physical size is the amount of memory allocated. The logical size is the amount that is in use, which might be smaller than the physical size.
A dangling pointer is a pointer to memory that has either been destroyed (either automatically or by using delete) or that has never been allocated at all.
A memory leak occurs when a program loses all pointers to memory that it allocated using new, without deleting that memory. The memory becomes inaccessible, but cannot be reused.
char* p; p = NULL; *p = 'x';
The operating system kills the program when *p is used. (The program gets some kind of memory fault. On a Unix system, it gets a segmentation fault, which is a kind of memory fault. On a Windows system, it gets a general protection fault.)
Yes, an array can be allocated using new. Write
p = new char[20](assuming that variable p has already been declared to have type char*).
char *s; cin >> s; cout << s;
It will compile. But the cin object tries to read a word and store it at the address in variable s. Since no memory address has been put into s, variable s holds a junk address. The results are unpredictable. It might cause a memory fault, or it might do a variety of other bad things.
The pointer has type Giraffe*.
You can compare two null-terminated strings using strcmp.
if(strcmp(s,t) == 0) x = 1;
strcpy(d,s);