A c ++ question about the field pointer and scope

Source: Internet
Author: User

Preface:

I asked a question a few days ago:

Http://home.cnblogs.com/q/27511/

Results The online comments are still not correct. Here we record the results of my test and solutions.

1. The original question is: when P leaves the scope, shouldn't p be a wild pointer? Why is the test function output still correct?

#include <iostream>using namespace std;class A{    public:    virtual void func(){ cout << "A func()" << endl;}};void test(){    A* p;    {        A a;        p = &a;    }    p->func();}int main(){    test();    return 0;}

Result output a func ()

The answer is as follows:

1. This method of A is the key ..

This method does not allow access to task. That is, the this pointer is not accessed. Therefore, the call is valid.

If there is a field accessing a in the method, it will not work ..

This is not correct. The following code accesses the this pointer and the result is still the output a_ I value.

#include <iostream>using namespace std;class A{    private:    int a_i;    public:    void func(){ cout << "A:a_i " << this->a_i << endl;}    A(int i){this->a_i = i;}};void test(){    A* p;    {        A a(5);        p = &a;    }    p->func();}int main(){    test();    return 0;}

The output is a_ I 5.

Changing the method for generating the result into a pointer to a new one does not work. There is no essential difference.

class A{    private:    int a_i;    public:    void func(){ cout << "A:a_i " << this->a_i << endl;}    A(int i){this->a_i = i;}};void test(){    A* p;    {        p = new A(3);    }    p->func();}int main(){    test();    return 0;}

The output is a_ I 5.

Scope:

Scope:. func (); out of scope # include <iostream> using namespace STD; Class A {PRIVATE: int a_ I; public: void func () {cout <": a_ I "<this-> a_ I <Endl;} A (int I) {This-> a_ I = I ;}}; void test () {A * P; {A (5); P = & A;}. func (); P-> func ();} int main () {test (); Return 0 ;}

Result: compilation failed.

Is it a wild pointer? The answer must be, look at the following

Class A {PRIVATE: int a_ I; public: void func () {cout <"A: a_ I" <this-> a_ I <Endl ;} A (INT I = 0) {This-> a_ I = I ;}}; int main () {A * P; // wild pointer p-> func (); return 0 ;}

A is not initialized, but the result is:

A: a_ I 0

I have been reviewing C ++ over the past few days. I thought about it. In fact, the C ++ object model contains the following:

The non-virtual method of the class does not exist in the memory layout of the object. In fact, the C ++ compiler converts the method into a global function, and the above will be converted to: a_func_xxx (), the object pointer is implicitly passed to the function as the first parameter, so the above p-> func (); the Global function a_func_xxx () is called (), P is passed to the func () function implicitly as this pointer, so the result is correct.

The following is a response:

#include <iostream>
using namespace::std;
class A
{
private:
int a_i;
public:
void func(){ cout << "A:a_i " << this->a_i << endl;}
A(int i){this->a_i = i;}
};
void test(){
A* p;
{
p = new A(3);
delete p;
}
A* b=new A(23);
cout << "==!!" << endl;
p->func();
}
int main()
{
test();
return 0;
}

A * B = new A (23); used the previously allocated memory again

The following reply has a question about the stack, which I always thought I understood:

# Include <iostream>

Using namespace STD;

// The pointer address is in the stack (except for the first address), but the content pointed to by the pointer is in the heap,
// So it is not cleared

Char * get_str ()
{
Char * STR = {"ABCD "};
Return STR;
}
// The variables in the stack are temporary. When the current function is executed,
// The related temporary variables and parameters are cleared, so the returned pointer is random.
// But STR [] is processed as a pointer and stored in the heap.
Char * get_str2 ()
{
Char STR [] = {"ABCD "};
Return STR;
}
Int main (INT argc, char * argv [])
{
Char * P = get_str ();
Cout <* P <Endl;
Cout <* (p + 1) <Endl;
Cout <p <Endl;
Cout <"=============================" <Endl;

Char * P2 = get_str2 ();
// When * P2 is executed for 1st times, the content of * P2 can still be obtained because the first address of the P2 pointer is returned.
/*
You can try to get * (P2 + 1 ).
*/
Cout <* P2 <Endl;

// Data is still available when * P2 is called for the first time, but no data is available for the first call,
// Indicates that the pointer has been destroyed after cout
Cout <* P2 <Endl;
Cout <* (P2 + 1) <Endl;
Cout <* P2 <Endl;
Cout <P2 <Endl;
Return 0;
}

But this: the answer is that it can run correctly. Because it is a constant string stored in the static memory area, the returned result is okay. So, you have to check the problem.

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

char *fun(void)
{
char *p="hello";
return p;
}
int main(void)
{
char *s;
s=fun();
printf("%s\n",s);
}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.