C++的new和类、c new 指针数组

西西软件下载最安全的下载网站、值得信赖的软件下载站!
→ C++中new 和 delete 的使用
V5.7.0.1绿色版
类型:卸载删除大小:662KB语言:英文 评分:5.0
new和delete可以有效、直接的进行动态内存的分配和释放。运算符new返回指定类型的一个指针,如果分配失败(如没有足够的内存空间)时,则返回0.如:  1 double *p;
3 *p = 3.1;
//将值存放在开辟的单元中系统自动根据double类型的空间大小开辟内存单元,并将地址放在指针p中。当然,也可以在开辟内存单元时对单元里的值进行初始化。则,上述代码等价于:1 double *p;
2 p = new double(3.1);运算符delete是释放new请求的内存单元。delete p将指针变量p指向的内存单元释放,指针变量p仍然有效,它可以重新指向另一个内存单元。new也可以为数组分配内存单元,当释放时,也可以告诉delete数组有多少个。如:1 int *p;
2 p = new int[10];
3 delete [10]p; //告诉delete数组有多少个元素,或delete []p;例子: 1 #include &iostream&
2 #include &cstring&
6 int main()
char *p = new char[6];
strcpy(p,&Hello&);
cout && p &&
cout && p &&
16 }运行结果:1Hello运行环境:  ubuntu 12.10  gcc版本 4.7.2
05-1604-2004-0301-2901-2801-2701-2201-2101-1701-13
阅读本文后您有什么感想? 已有23人给出评价!
名称大小下载新手园地& & & 硬件问题Linux系统管理Linux网络问题Linux环境编程Linux桌面系统国产LinuxBSD& & & BSD文档中心AIX& & & 新手入门& & & AIX文档中心& & & 资源下载& & & Power高级应用& & & IBM存储AS400Solaris& & & Solaris文档中心HP-UX& & & HP文档中心SCO UNIX& & & SCO文档中心互操作专区IRIXTru64 UNIXMac OS X门户网站运维集群和高可用服务器应用监控和防护虚拟化技术架构设计行业应用和管理服务器及硬件技术& & & 服务器资源下载云计算& & & 云计算文档中心& & & 云计算业界& & & 云计算资源下载存储备份& & & 存储文档中心& & & 存储业界& & & 存储资源下载& & & Symantec技术交流区安全技术网络技术& & & 网络技术文档中心C/C++& & & GUI编程& & & Functional编程内核源码& & & 内核问题移动开发& & & 移动开发技术资料ShellPerlJava& & & Java文档中心PHP& & & php文档中心Python& & & Python文档中心RubyCPU与编译器嵌入式开发驱动开发Web开发VoIP开发技术MySQL& & & MySQL文档中心SybaseOraclePostgreSQLDB2Informix数据仓库与数据挖掘NoSQL技术IT业界新闻与评论IT职业生涯& & & 猎头招聘IT图书与评论& & & CU技术图书大系& & & Linux书友会二手交易下载共享Linux文档专区IT培训与认证& & & 培训交流& & & 认证培训清茶斋投资理财运动地带快乐数码摄影& & & 摄影器材& & & 摄影比赛专区IT爱车族旅游天下站务交流版主会议室博客SNS站务交流区CU活动专区& & & Power活动专区& & & 拍卖交流区频道交流区
小富即安, 积分 2676, 距离下一级还需 2324 积分
论坛徽章:1
class_xxx ** p = new class_xxx*[100]();
我已经测试过这种方式可以将 100个class_xxx * 成员 初始化成 0,
class_xxx ** p = new class_xxx*[100];
这种方式是不确定的
请问一下这种行为 new class_xxx*[100]();&&有什么官方说明吗?
&&nbsp|&&nbsp&&nbsp|&&nbsp&&nbsp|&&nbsp&&nbsp|&&nbsp
腰缠万贯, 积分 9262, 距离下一级还需 738 积分
论坛徽章:14
我记得是有的,但以 幻之上帝 的答案为标准^_^
小富即安, 积分 2676, 距离下一级还需 2324 积分
论坛徽章:1
@幻之上帝&&何许人也
富足长乐, 积分 5396, 距离下一级还需 2604 积分
论坛徽章:0
本帖最后由 幻の上帝 于
15:46 编辑
15 A new-expression that creates an object of type T initializes that object as follows:
— If the new-initializer is omitted, the object is default-initialized (8.5); if no initialization is performed,
the object has indeterminate value.
— Otherwise, the new-initializer is interpreted according to the initialization rules of 8.5 for direct-initialization.
5 To zero-initialize an object or reference of type T means:
— if T is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression, converted to T;103
— if T is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is init
— if T is a (possibly cv-qualified) union type, the object’s first non-static named data member is zeroinitialized and padding is init
— if T is an array type, each element is zero-
— if T is a reference type, no initialization is performed.
103) As specified in 4.10, converting an integral constant expression whose value is 0 to a pointer type results in a null pointer value.
6 To default-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the
initialization is ill-formed if T has no accessible default constructor);
— if T is an array type, each element is default-
— otherwise, no initialization is performed.
If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.
7 To value-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor is called.
— if T is an array type, then each element is value-
— otherwise, the object is zero-initialized.
An object that is value-initialized is deemed to be constructed and thus subject to provisions of this International Standard applying to “constructed” objects, objects “for which the constructor has completed,” etc., even if no constructor is invoked for the object’s initialization.
15 The initialization that occurs in the forms
as well as in new expressions (5.3.4), static_cast expressions (5.2.9), functional notation type conversions (5.2.3), and base and member initializers (12.6.2) is called direct-initialization.
16 The semantics of initializers are as follows. The destination type is the type of the object or reference being initialized and the source type is the type of the initializer expression. If the initializer is not a single (possibly parenthesized) expression, the source type is not defined.
— If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized (8.5.4).
— If the destination type is a reference type, see 8.5.3.
— If the destination type is an array of characters, an array of char16_t, an array of char32_t, or an array of wchar_t, and the initializer is a string literal, see 8.5.2.
— If the initializer is (), the object is value-initialized.
— Otherwise, if the destination type is an array, the program is ill-formed.
— If the destination type is a (possibly cv-qualified) class type:
&&— If the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, constructors are considered. The applicable constructors are enumerated (13.3.1.3), and the best one is chosen through overload resolution (13.3). The constructor so selected is called to initialize the object, with the initializer expression or expression-list as its argument(s). If no constructor applies, or the overload resolution is ambiguous, the initialization is ill-formed.
&&— Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof are enumerated as described in 13.3.1.4, and the best one is chosen through overload resolution (13.3). If the conversion cannot be done or is ambiguous, the initialization is ill-formed. The function selected is called with the initializer expres if the function is a constructor, the call initializes a temporary of the cv-unqualified version of the destination type. The temporary is a prvalue. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the obje see 12.2, 12.8.
— Otherwise, if the source type is a (possibly cv-qualified) class type, conversion functions are considered.
The applicable conversion functions are enumerated (13.3.1.5), and the best one is chosen through
overload resolution (13.3). The user-defined conversion so selected is called to convert the initializer
expression into the object being initialized. If the conversion cannot be done or is ambiguous, the initialization is ill-formed.
— Otherwise, the initial value of the object being initialized is the (possibly converted) value of the initializer
expression. Standard conversions (Clause 4) will be used, if necessary, to convert the initializer expression to the cv-unqualified version of
no user-defined conversions are considered.
If the conversion cannot be done, the initialization is ill-formed. [ Note: An expression of type
“cv1 T” can initialize an object of type “cv2 T” independently of the cv-qualifiers cv1 and cv2.
const int b =
—end note ]
省略初值符就是默认初始化,对于 内建指针内建指针及其数组 就是不初始化,具有未决值。
显式初始化同声明时同时初始化(这个规则就一堆了,上面把和LZ问题不相关的像zero-initialization/value-initialization都去掉了免得太罗嗦,有需要的翻8.5)。
题外话,new的坑爹语法……
4 [ Note: parentheses in a new-type-id of a new-expression can have surprising effects. [Example:
new int(*[10])(); // error
is ill-formed because the binding is
(new int) (*[10])(); // error
Instead, the explicitly parenthesized version of the new operator can be used to create objects of compound types (3.9.2):
new (int (*[10])());
allocates an array of 10 pointers to functions (taking no argument and returning int. —end example ] —end note ]
编辑:还是把[&&code&&]去掉好了
编辑2:具体行为还是涉及到value-init,还是加上算了……
大富大贵, 积分 15632, 距离下一级还需 4368 积分
论坛徽章:0
new/delete这一对函数不好用,看得出来是站在对象语义的角度。
而且没有renew这种类似realloc的功能。
delete更是没法用,要接受[]这种语法。
小富即安, 积分 2676, 距离下一级还需 2324 积分
论坛徽章:1
— If the initializer is (), the object is value-initialized.先看一段代码:
#include &iostream&int main(){
string s("i love you!");
cout&&*str&&"**********"&&
return 0;}
就会报错;然后要是改成:
#include &iostream&int main(){
string s("i love you!");
string *str=
cout&&*str&&"**********"&&
return 0;}
就OK了。所以我的问题是:C++中在定义string *str时候,必须要 new string吗? 就是必须要string *str = new string吗?
它为嘛不会去自动分配空间? 哪位大侠能否详细介绍下string类型指针的用法……
<dd style="float:cursor:" title="声誉值:
该问题被发起重新开启投票
投票剩余时间:
之前被关闭原因:
该问题被发起删除投票
投票剩余时间:
距离悬赏到期还有:
参与关闭投票者:
关闭原因:
该问题已经被锁定
锁定原因:()
保护原因:避免来自新用户不合宜或无意义的致谢、跟帖答案。
该问题已成功删除,仅对您可见,其他人不能够查看。
C++中在定义string str时候,必须要 new string吗? 要
就是必须要string *str = new string吗?也可以指向已经存在的string对象。如string str = &strE它为嘛不会去自动分配空间?它只是一个指针,没有明确指向的时候,它是一个野指针。
这都是基础问题
string *和string *str=的区别1 前者只是定义了一个指针变量,叫str,并没有赋值,此时是个野指针,指向的内存空间不确定2 后者是先在申请一段内存空间,然后把这段内存空间的地址赋值给指针变量str
说的对,补充一下string *这样只是声明了一个string类型的指针,其中的内容编译器都会填充一些值,vs下是填充0并且该指针指向的内存区域是不可访问的。第一段程序若想正确运行,可以添加这一句:str=&s;//这样就使得str指向了s的地址。另外,C++string类重载了赋值操作符=
我觉得c++里:如果只用string*定义指针str,出来的是个野指针,没有指向为为string类型而开辟的空间;然后使用*str =是直接将s里的字符串赋给str所指向的空间,而非将str所指向移到s上,由于没有为string类型开辟空间,所以就会报错。如楼上所说,换成str = &s;应该就会解决。只是个人想法,若不对,请指出。
不是您所需,查看更多相关问题与答案
德问是一个专业的编程问答社区,请
后再提交答案
关注该问题的人
共被浏览 (7385) 次君,已阅读到文档的结尾了呢~~
C++指针与动态分配内存new关键字专题
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
C++指针与动态分配内存new关键字专题
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口

我要回帖

更多关于 new 一个对象报空指针 的文章

 

随机推荐