(三)A tiny C-like library之Dynamic storage allocation

Dynamic storage allocation (动态存储负载)

You never know the maximum amount of storage you might need
由于根本无法得知CStash可能需要的最大存储空间,
for a CStash , so the memory pointed to by storage is allocated from
所以由storage指向的内存空间将分配在heap中。
the heap. The heap is a big block of memory used for allocating
heap是用来在运行时对占用内存小的变量进行分配的大块内存。
smaller pieces at runtime. You use the heap when you don't know
当你写程序但并不知道将用多少内存空间时,heap是不错的选择。
the size of the memory you'll need while you're writing a program
That is, only at runtime will you find out that you need space to
也就是说,只有到运行时,你才能知道你需要200个变量。
hold 200 Airplane variables instead of 20. In Standard C, dynamic-
在标准C语言中,对内存的动态分配用到函数malloc(),calloc(),realloc(),
memory allocation functions include malloc( ), calloc( ), realloc( ),
与free()。
and free( ). Instead of library calls, however, C++ has a more
sophisticated (albeit simpler to use) approach to dynamic memory
不过,在C++中,我们并不是调用库函数,而是用一种更为成熟的机制(但同时也相当好用)
that is integrated into the language via the keywords new  and delete.
来动态分配内存,这种机制是通过关键字new和delete引入到C++中的。
The inflate() function uses new to get a bigger chunk of space for
在inflate()方法中,用new来为CStash获得一大块内存空间。
the CStash. In this situation, we will only expand memory and not
用new我们也只是扩展了内存,但并不能将其缩小,在此过程中将调用assert()
shrink it, and the assert( ) will guarantee that a negative number is
方法来保证随着value值的增加传给inflate()的参数非负。
not passed to inflate( )  as the increase value. The new number of
inflate()方法结束后,Storage中包含元素的个数将计算出并赋值给newQuantity.
elements that can be held (after inflate( ) completes) is calculated as
newQuantity, and this is multiplied by the number of bytes per
随后此值再与每个元素所包含的字节数相乘得到newBytes的值,newBytes中的
element to produce newBytes, which will be the number of bytes in
值也就是内存中所含字节数。
the allocation. So that we know how many bytes to copy over from
 依此,我们就知道将需要从旧地址处拷多少个字节的数据,oldBytes也将
the old location, oldBytes  is calculated using the old quantity.
通过调用旧的quantity来计算得出。
The actual storage allocation occurs in the new-expression, which is
对storage实际内存占有量的计算是在new-expression中完成的,这个new表达式
the expression involving the new  keyword:
如下所示:
new unsigned char[newBytes];
 
The general form of the new-expression is:
new Type;
new表达式的一般形式为:new Type;
in which  Type describes the type of variable you want allocated on
此中的Tpye表示你将分配到堆中的变量类型。
the heap. In this case, we want an array of unsigned char that is
在我们当前所讨论的例子中,我们想要元素类型为unsigned char长度为newBytes的数组
newBytes long, so that is what appears as the Type. You can also
这也就是变量Type的实际意义。
allocate something as simple as an int by saying:
new int;
 我们也可以用new int;来分配内存,尽管这样的方式很少采用,不过它还是
and although this is rarely done, you can see that the form is
合法。
consistent.

A new-expression returns a pointer to an object of the exact type
new表达式将返回一指向你想要类型的指针,即如你写了一new Type表达式,
that you asked for. So if you say new Type, you get back a pointer
将得到一个指向此Tpye的指针。
to a Type.If you say new int, you get back a pointer to an int. If

you want a  new unsigned char  array, you get back a pointer to the
first element of that array. The compiler will ensure that you assign
编译器保证将此返回值赋给正确的类型。
the return value of the new-expression to a pointer of the correct
type.

Of course, any time you request memory it's possible for the
当然,在已没有内存可用的情况下再想获得内存的话肯定会出错的。
request to fail, if there is no more memory. As you will learn, C++
随后,我们将会看到,C++另有一套机制会在内存分配失败时起作用。
has mechanisms that come into play if the memory-allocation
operation is unsuccessful.

Once the new storage is allocated, the data in the old storage must
在新的storage分配后,旧storage中的数据将会拷贝到这个新的存储空间中。
be copied to the new storage; this is again accomplished with array
此处的拷贝也是以数组索引的形式进行的。
indexing, copying one byte at a time in a loop. After the data is
copied, the old storage must be released so that it can be used by
数据拷贝完成后,旧的存储空间将会得到释放以便程序中别的数据使用。
other parts of the program if they need new storage. The delete 
关键字delete的功能正好与new相反,在释放由new生成的存储空间时必将用到这个关键字。
keyword is the complement of  new, and must be applied to release
 
any storage that is allocated with  new (if you forget to use delete,
(要是你忘了释放内存,那个storage仍不可用,当这种我们常说的内存泄漏发生多处时,
that storage remains unavailable, and if this so-called memory leak
happens enough, you'll run out of memory). In addition, there's
内存将会被耗完)。另外,在释放数组时,还有一特殊的用法,即你不仅仅要告诉编译器
special syntax when you!ˉre deleting an array. It's as if you must
你将释放指向一个对象的指针,而是指向一级对象的指针:在要释放的数组名前
remind the compiler that this pointer is not just pointing to one
加一[]即可。
object, but to an array of objects: you put a set of empty square
brackets in front of the pointer to be deleted:
  delete []myArray;

Once the old storage has been deleted, the pointer to the new
一旦旧的storage被删除后,指向新storage的指针将……,其成员quantity
storage can be assigned to the storage pointer, the quantity is
也将做出相应的调整,到此inflate()方法就完成了他的工作。
adjusted, and inflate( ) has completed its job.  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值