回复:(loriwang)一个动态分配的内存地址的疑问
#include <iostream>
using namespace std;
int main()
{
char* ca = new char[4];
cout << ca << endl; // cout content of ca[] until it reaches a '\0' char
printf("%p\n", ca); // prints ca's address in the heap
int* i = new int[4];
cout << i << endl;
/** heap grows up: 00383538 < 00383548.
You see there is some unused bytes between the two variables.
This is a disadvantage of dynamic allocation --- you may create
a lot of fragments in the heap memory.
*/
/** output:
X8
00383538
00383548
0012FF6C
0012FF68
Press any key to continue . . .
*/
// stack grows down: 0012FF6C > 0012FF68
// both i and ca live on the program stack
cout << & ca << endl;
cout << & i << endl;
delete [] ca;
delete [] i;
}