English 中文(简体)
记忆管理
原标题:memory management

这是关于C++代码的记忆管理问题。

using namespace std;
#include <iostream>
#include <string.h>

int main()
{
 string a="first";
 string *b= new string;
 *b=a;
 a="second";
 cout << *b << ", " << a;
 delete b;
 return 0;
}

We can deallocate the blocks of memory that stored the string that b pointed to. I m assuming this means that b no longer has any meaning once this is done. We can deallocate b to free some memory. Why can we not deallocate a? I know that we can only delete/free pointers, but the string a must take up some memory somewhere. Is there some way that we can free the memory that the string a takes up? If there are enough strings initialized in the same way that a is initialized, wouldn t it be possible that memory runs out?

问题回答

插图<代码>a在标签上宣布。 你们可以人工解救,但一旦离开范围,就会自动释放(例如,在披露职能时)。 如果你需要释放这一记忆中功能,那么,就能够动态地宣布这种记忆(正如你对<代码>b所做的那样)。

胎体包括一个小物体,其中含有显示数据储存点,其寿命由物体管理。 通常不需要担心物体本身所积累的记忆,但如果大体如此,你可能想在不销毁物体的情况下释放储存。

电话clear(,或从单载体中分配,可能不会免除储存。 确保其自由的方式是用新建造的临时工来挖掘护卫;临时司机将释放。

string().swap(a); // replaces `a` with an empty string

你们也可以向任何标准集装箱这样做。

这是一种ward。

<string.h> is a C Header. 它界定了<条码>载<>。 它像你版本的<代码><iostream>直接或间接地包括<string>,或存在错误。

在任何地方,包括仅读的记忆部分,照相扼杀(由高额引述标记划定的照片)可能相当多。 (这确实是记忆,但你ve着大量的文本,以便产生重大影响:例如War and Peacetn t to take a full meg.) 在此情况下,正在以这一价值开始采用<>代码:>载,之后再分配价值。 <代码>std:string 处理它使用的记忆。

在C++中,几乎没有任何理由对<条码>提出点:显示。 <代码>std:string 没有内容就占用了很多空间,它为内容本身管理记忆。 您是否将此与<代码>char*混为一谈?

www.un.org/chinese/ga/president 这暴露了记忆。 newed for b, 但仍没有分配到delete上,因此,在方案期间将保留。

然后,一旦您指定了<代码>a至b的地址,请上delete b;。 这是“巴德理想”,而且很可能以可能难以预测的方式来推展重要的东西。 仅delete/code> 缅怀您在new上获得的。 (deleteing的重要内容不是b>是个点,应当删除,但该代码指出的记忆并非通过new。)

记忆管理就是这样做的。 一段直截了当的字面被分配到其他地方。 你们都知道,你竟然试图通过任何手段来改变这一条或条码。 使用价值而不触及其余部分。 缩略语 在职能或其他部分宣布的变数一旦脱离范围就会被销毁(不过,无论哪一个可能点能够自动打赢,只要它能够打上智能点或管理自己的记忆或任何东西)。 缩略语 记忆,在<代码>deleted之前,不会放弃点值。 如果您有t newed memory, 不要有t delete

自动物体(如你的物体)在范围外被销毁。 见下文:

int main()
  {

    {
    string a="first";
    string *b= new string;
    *b=a;
    a="second";
    cout << *b << ", " << a;
    delete b; //b gets freed
    iii //a gets freed because it has gone out of scope   
 /* You can write more code here that does not need a */
 return 0;

iii

分配给a的记忆将自动在功能恢复后释放。

<代码>a此处自动在“stack”上分配,并在超出范围后自动销毁/重新分配。

当你使用<条码>新/<条码>的活着记忆时,它就被分配到“跳跃”上,而“跳跃”只是方案可以利用的记忆。 你们必须手工管理,而该方案在何时消除。

如果你真心怀着记忆,那么你就应该只使用有活力的记忆分配。

Edit: This gives a more complete explanation than what I am trying to say: http://en.wikipedia.org/wiki/Malloc#Rationale





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签