newºÍdelete
newºÍdeleteÓÃÓÚ¶¯Ì¬¿ª±Ù¿Õ¼äºÍÊÍ·Å¿Õ¼äºÍCÓïÑÔÖÐmallocºÍfreeµÈ¼Û£»
//malloc ºÍfreeµÄʹÓã»----º¯Êý /*int main() { int *p; char *ps; p=(int *)malloc(sizeof(int)); //¶¯Ì¬¿ª±ÙÒ»¸öintÀàÐ͵Ŀռä *p=5;
ps=(char*)malloc(sizeof(char)*5);//¶¯Ì¬¿ª±ÙÎå¸öÔªËØÊý×éµÄ¿Õ¼ä
//ÊÍ·Å¿Õ¼ä free(p); free(ps); return 0; } */
//newºÍdelete¹Ø¼ü×Ö int main() { int *p; char *ps; p=new int(8);//¶¯Ì¬¿ª±ÙÕûÐ͵Ŀռä,¿ÉÒÔ²ÉÓôËÐÎʽ¶Ô±äÁ¿³õʼ»¯£» printf("p=%d\n",*p); ps=new char[12];//ΪÊý×鿪±Ù¿Õ¼ä
//ÊÍ·Å¿Õ¼ä delete p; delete []ps;//ÊÍ·ÅÊý×éµÄ¶¯Ì¬·ÖÅäµÄ¿Õ¼ä
return 0; } |