English 中文(简体)
界定控制结构各组成部分的变量
原标题:defining variables in blocks of control structures
  • 时间:2012-05-20 15:48:10
  •  标签:
  • c++

如果我界定了控制结构内的一个变量,那么这种控制结构的实行,而不是在整个执行封闭职能时,是否存在?

我如何监测我方案的确切记忆使用及其变化(即:通过制造和销毁变量改变记忆的使用)?

added later: in following code I know v scope is if block, but I want to know whether v is created/destroyed in the memory at the start/end of the if block or at the start/end of the function func?

void func ()
{
    if (true)
    {
        int v;//automatic storage class
        v = 1;
    }
}
最佳回答

如果我界定了控制结构内的一个变量,那么这种控制结构的实行,而不是在整个执行封闭职能时,是否存在?

取决于您的<><> > 声明。 页: 1

The variable is only accessible in the scope where you declare it. It can be accessed beyond tht scope if you explicitly pass it around but if it remains valid would depend on the storage type of the variable.
For eg: static variables remain alive throughout the program lifetime, while,
returning address of an automatic variable from an function would result in Undefined Behavior because the variable does not remain valid after the function returns.

Good Read: What is the difference between a definition and a declaration?

我如何监测我方案的确切记忆使用及其变化(即:通过制造和销毁变量改变记忆的使用)?

我认为,你希望获得关于动态分配的物体的信息,因为自动物体的寿命仅够长,将自动销毁,从而不会造成任何问题。

For dynamic objects You can use memory profiling tools like valgrind with Massif or you could replace new and delete operators for your class and collect the diagnostic information.


<>光线> 处理更新的Q。

下面的代码I知道v scope is if >,但我想知道,在功能的起步或起/起步时,在记忆中是否建立了<代码>v<>/code>?

v is created when the scope in which it is declared starts and the statement where it is declared gets executed. v is destroyed once the scope ends i.e } is reached.
This concept is used for forming the basis of one of the most widely used concepts in C++ known as Resource Allocation is Initialization(RAII). Every C++ programmer absolutely must know about it.

简单的做法是通过这一小改动证明和销毁这些物体:

#include<iostream>
class Myclass
{
    public:
        Myclass(){std::cout<<"
In Myclass Constructor ";}
        ~Myclass(){std::cout<<"
In Myclass Destructor";}
};

void func()
{
    std::cout<<"
Before Scope Begins";
    if (true)
    {
        Myclass obj;//automatic storage class
    }
    std::cout<<"
After Scope Ends";
}

int main()
{
    std::cout<<"
Before Calling func()";
    func();
    std::cout<<"
After Calling func()";
    return 0;
}

产出如下:

Before Calling func()
Before Scope Begins
In Myclass Constructor
In Myclass Destructor
After Scope Ends
After Calling func()

问题回答

如果地方变量is物体,则该物体的寿命在被宣布变数的区块结束时终止。 例:

Foo x;            //  lifetime of "x" begins

{
    some_stuff();

    Foo y;        //  lifetime of "y" begins

    Foo & a = x;  //  local variable "a", no new object

    more_stuff();

}                 //  lifetime of "y" ends; "y" and "a" go out of scope

                  //  (lifetime of "x" ends with its surrounding scope,
                  //   or end of program if it was global)




相关问题
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?

热门标签