C ++输出问题. [英] C++ Output question.

查看:68
本文介绍了C ++输出问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的导师提供的一段代码,用于分析和说明其行为的原因. 作为一个初学者,我对代码的运行情况感到有些惊讶,因为指向类对象的指针已设置为NULL.
我很少用代码玩,在网上搜索后得出结论,对该函数的调用被隐式地实现为void funct(A * this),这就是为什么它可以正常工作的原因.

This is piece of code given by my mentor to analyse and state the reason behind its behaviour.
Being a beginner, I was a little surprised to see this code running fine, as pointer to the object of class has been set NULL.
I played with the code little, searched the web and came to a conclusion that the call to the function is being implicitly implemented as void funct(A* this) that is why it is working fine.

#include<iostream>

using namespace std;

class A
{
    public:
        int a;
        void func (void)
        {
            cout << "hello world " <<endl;
        };
};

int main()
{
    A* ptr = NULL;
    ptr->func();
    return 0;
}



但是我的导师告诉我,这不是这段代码可以正常工作的原因,如果这是导致代码崩溃的原因,请补充.并添加了
上面代码中的cout语句为"a".



But my mentor told me that this is not the reason for this piece of code working fine and added if this is the reason then why does this code crashes. And added
"a" to the cout statement in the above code.

void func (void)
{
     cout << "hello world " << a <<endl;
};



现在我有些困惑,无法以其他任何方式看到它.
任何人都可以提供我可以解决的任何方向或暗示.



Now I am a little puzzled and not able to see it in any other way.
Can anyone provide any direction or any hint taking which I can work forward to solve it.

推荐答案

类成员函数的内存将在运行时分配,并且所有实例的类将使用相同的功能.

访问不访问成员变量的成员函数不会导致访问冲突.
因为没有与该类的实例相关的内存访问.

Memory of a class member function will be allocated at runtime, and all instance of the class will use the same function.

Accessing a member function which does not access a member variable will not cause an access violation.
Because there is no memory access related to the instance of the class.

void A::func (void)
        {
            cout << "hello world " <<endl;
        }; 


将被编译为


will be compiled as

void func (A* this)
        {
            cout << "hello world " <<endl; // this is no where accessed here.
        };


由于不能从func访问this in,因此调用类A的func 不会产生访问冲突.

但是,如果func访问任何成员变量,则将创建访问冲突.原因是将发生对NULL 指针的内存访问,这将创建访问冲突.


Since this in not accessed from func, calling func of class A will not create access violation.

But if func accesses any member variable, then it will create an access violation. The reason is memory access to a NULL pointer will happen, and it will create an access violation.

void A::func (void)
        {
            cout << "hello world " <<a<<endl;
        }; 


将被编译为


will be compiled as

void func (A* this)
        {
            cout << "hello world " <<this->a<<endl; // Here this is used and it will create access violation.
        };


要了解为什么会发生这种情况,您应该知道什么是类,什么是对象.

如果您是A类,
如果您尝试执行语句sizzeof(A),那么您将意识到这将是数据成员的大小之和

这本身意味着,仅数据成员是任何对象的一部分,而相应的功能则不是.

那么,如果函数不是对象的一部分,那么任何函数如何访问数据成员呢?

这就是著名的 this 指针出现的地方.

因此,如果有这样的代码

To understand why this happens , you should know what is a class, what is an object.

In case of your class A,
if you try to execute the statement sizzeof(A) , then you will realize it will be the sum of size of data members

This itself mean that , only data members are part of any object and the corresponding functions are not.

Then how does any function accesses the data members if it is not a part of object ?

This is where famous this pointer comes into picture.

Hence if there is a code like

A *ptr = new A();
ptr->func();



此代码将由编译器转换为



This code will be converted by compiler to

A *ptr = new A();
func(ptr);   //Here ptr points to the object.



因此,您的func函数将使用传递的ptr(即 this 指针)访问数据成员.



Hence your func function will access data members using the passed ptr (i.e. this pointer )

void func( A* this )
{
printf("Some text");
}




如果我们选择将ptr的初始化替换为 NULL ,那么我们将向函数传递 NULL 指针.




if we chose to replace initialization of the ptr to NULL then we will be passing a NULL pointer to function.

A *ptr = NULL;
ptr->func();



编译器会将这段代码转换为



Compiler will convert this code to

A *ptr = NULL;
func(ptr);



在func中,如果您尝试访问任何数据成员



In func, if you try to access any data member

A::func()
{
printf( "a = %d", a);
}



编译器会将这段代码转换为



compiler will convert this code to

func( A* this )
{
   printf("a = %d", this->a);
}



现在您可以观察到,因为我们正在向函数发送NULL指针.它必须崩溃:)



Now you can observe that , as we are sending NULL pointer to the function. It has to crash :)


阅读此 link [ ^ ]在堆栈溢出中,这将清除很多问题,并在您的脑海中引发很多问题:)
read this link[^] in stack overflow it will clear up lots of issue and also raise lots of question in your mind too :)


这篇关于C ++输出问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆