some declaretion about the democode:
#include <iostream>
using namespace std;
int main()
{
int * a = (new int[5]) + 1; // usually we write code like this int * a = new int[5];
// here new int[5]; is place allocation, for example the first address is 3000
// then (new int[5]) + 1; is address 3004, why 3004 not 3001, because every int
// need 4 byte, so 3000 + 4 = 3004
// with this for loop with this pointer array initialized
for(int i = 0; i<5; i++)
{
*(a+i-1) = i; // because a is not the first address, but the second, so we need minus 1, so that a - 1 is the first address
cout<<a[i-1]<<" ";
}
delete [] a; // it is very import, when you use new, then not forget use delete to frei the allocation.
// when allocation for an array, then delete [] name, otherwise delete name;
return 0;
}
(new int[5]) 开辟的内存会被初始化吗?还是垃圾值?
我感觉您的例子无法说明我那个问题
而且这样是否存在所谓的“指针泄露”问题呢?
To your question, you are right, before return 0; we should insert a statement, delete [] a; otherwise there is space lack problem.