new_delete_指针

//use pointer to change value
*p_updates=*p_updates+1;
cout<<"now updates="<<*p_updates<<endl;

long *fellow//create a pointer-to-long
*fellow=2333//place a value in never-never land

//use_new.cpp
#include <iostrteam>
int main()
{
	using namespace std;
	int nights=10001;
	int *pt=new int;
	*pt=1001;
	cout<<"nights value ="<<nights<<" : location "<<&nights<<endl;
	cout<<"int value ="<<*pt<<": location ="<<pt<<endl;
	cout<<"size of pt="<<sizeof(pt)<<endl;
	cout<<"size of *pt="<<sizeof(*pt)<<endl;

	double *pd=new double;
	*pd=100.0;
	cout<<"double value ="<<*pd<<": location="<<pd<<endl;//100的地址
	cout<<"location of pointer pd:"<<&pd<<endl;//指针本身是一个变量,有地址
	cout<<"size of pd="<<sizeof(pd)<<endl;//结果为4?????
	cout<<"size of *pd="<<sizeof(*pd)<<endl;//结果为8?????
	return 0;
}
//使用delete

int *ps=new int;//allocate memory with new
...//use the memory
delete ps;//free memory with delete when done

//这将释放ps指向的内存,但不会删除ps指针本身。例如可以将ps重新指向另一个新分配的内存块。
//一定要成对的使用new和delete,否则会发生内存泄漏,也就是说被分配的内存再也无法使用了,如果内存泄漏严重,则程序将由于不断寻找新的内存而停止

int *ps=new int;//ok
delete ps;//ok
delete ps//not ok now
int jugs=5;//ok
int *pi=&jugs;//ok
delete pi;//not allowed,momory not allocated by new

//不要尝试释放已径释放的内存块(会发生未知的错误)。更不能使用delete来释放通过声明变量所获得的内存(而对空指针使用delete是安全的)

int*ps=new int;//allocate memory
int *pq=ps;//set second pointer to same block
delete pq;//delete with second poniter

//注意,使用delete的关键在于,将他使用于new分配的内存。这并不意味着要使用于new的指针,而是使用于new的地址
//一般来说,不要创建两个指向同一个内存块的指针,因为这将增加错误地删除同一个内存块两次的可能性。但是在返回指针函数的时侯,使用另一个指针确实很有道理

//使用new创建动态数组
int *psome=new int [10];//必须指定值
delete [] psome;//删除,而且是从psome的第一个元素删除,如果改变psome的位置删除工作也会发生变化

//谨记:
//不要使用delete来删除不是new分配的内存
//不要使用delete释放同一个内存两次
//如果使用new[]为数组分配内存,应是用delete []来释放
//使用new为一个实体分配内存,应是用delete(没有[])来释放
//对空指针使用delete是安全的

//使用动态数组
#include <iostream>
int  main()
{
    using namespace std;
    double *p3=new double[3];
    p3[0]=0.2;
    p3[1]=0.5;
    p3[2]=0.8;
    cout<<"p3[1] is "<<p3[1]<<".
";
    p3=p3+1;
    cout<<"NOW p3[0] is "<<p3[0]<<" and ";
    cout<<"p3[1] is "<<p3[1]<<".
";
    p3=p3-1;
    delete []p3;
    return 0;
}
//下面的代码行指出了数组名与指针明的本质区别
p3=p3+1;//okay for pointers,wrong for array names;

//不能改变数组名,但指针是变量,因此可以修改他的值,多注意一下p3+1的效果。表达式p3[0]现在指的是数组的第二个值。因此将p3+1导致他指向第二个元素而不是第一个
//将他减一后指针将指向原来的地址,这样为delete[]提供了准确的地址(疑问:不进行-1操作会不会再删除的时候把p3之前的地址内存都保留下来?)
#include <iostream>
int  main()
{
    using namespace std;
   double wages[3]={1000.0,2000.0,3000.0};
   double *pw=wages;
   cout<<sizeof(wages)<<" = sizeof wages array"<<endl;
   cout<<sizeof(pw)<<" = sizeof pw pointer"<<endl;
   return 0;
}

//指针和字符串
//一般来说给cout提供一个指针,它必然将打印一个地址,但对于char类型的指针,cout将显示的是一个字符揣,如果要显示字符踹的地址,就必须加上强制转化类型(int*),且*不能在()外面
//要想获得字符踹的一个副本,首先分配内存来存储该字符踹,可以用数组或new来完成,但用new能根据字符踹的长度来分配内存空间,列:

ps=new char [strlen(...)=1];
strcpy(ps,...);//在这儿复制得到的(int*)ps和...的地址也是不相同的

//使用new来创建动态结构
#include <iostream>
struct inflatable
{
    char name[20];
    float volume;
    double price;
};
int  main()
{
    using namespace std;
    inflatable *ps =new inflatable;//可以加数组
    cin.get(ps->name,20);//可以读取空格
    cin>>(*ps).volume;//优先级
    cin>>ps->price;
    cout<<ps->name<<' '<<ps->price<<' '<<ps->volume<<endl;
   return 0;
}
//看下面一个程序体会并学会应用(相比数组对内存的优势)
#include <iostream>
#include <cstring>
 using namespace std;
char *getname(void);
int main()
{
    char *name;
    name = getname();
    cout<<name<<" at "<<(int *)name<<"
";
    delete []name;

    name=getname();
    cout<<name<<" at "<<(int *)name<<endl;
    delete []name;
    return 0;
}
char  *getname()
{
    char temp[80];
    cin>>temp;
    char  *pn=new char[strlen(temp)+1];
    strcpy(pn,temp);
    return pn;
}
//静态存储方式一种是在函数外面定义它,一种是在函数内用static动态存储则是new(必须和delete配对使用,否则会引起严重的内存泄漏,导致本程序或和该内存附近的存储空间程序崩溃)

原文地址:https://www.cnblogs.com/sxy201658506207/p/7586294.html