简体   繁体   English

取消引用此指针有什么问题?

[英]What is wrong with derefrencing this pointer?

While debugging i found that my program stops at: "cout << *ptr ; cout << "\\n";" 在调试时,我发现我的程序在以下位置停止:“ cout << * ptr; cout <<” \\ n“;” what is wrong with this code? 此代码有什么问题?

#include<iostream>

using namespace std;
int *ptr = 0;
void myfun(void);

int main()
{
    void myfun();
    for(int j = 1; j < 3; j++)
    {
        ptr = ptr-j ;       
        cout << *ptr ; cout << "\n";

    }
    return(0);  
}

void myfun(void)
{
    int x[3] = {11,12,13};
    for(int i = 0; i <3; i++)
    {
        ptr = &x[i];
        ptr = ptr+1;
    }
}

You initialized your pointer with zero 您将指针初始化为零

int *ptr = 0;

which means that it is a null pointer. 这意味着它是一个空指针。

Then you are applying pointer arithmetic to a null pointer. 然后,您将指针算术应用于空指针。 The behavior is undefined. 该行为是不确定的。 The you are dereferencing the nonsensical pointer obtained in that way. 您正在取消引用以这种方式获得的毫无意义的指针。 The behavior is undefined. 该行为是不确定的。

Note that 注意

void myfun();

in main is not a function call, it is a function declaration. main不是函数调用,而是函数声明。 A call to myfun (which was apparently your intent) would look as follows 调用myfun (显然是您的意图)如下所示

myfun();

Why did you put that void there in your version of the code? 为什么要在代码版本中添加void

Your variable ptr is a wild pointer. 您的变量ptr是一个野指针。

Although you let the ptr porint to array x, when the myfun() finish and return the memory of x had been destory. 尽管您将ptr porint设置为数组x,但当myfun()完成并返回x的内存时,该内存已被破坏。 So in the main function, ptr become wild pointer and your program will crash. 因此,在主函数中,ptr成为野指针,您的程序将崩溃。

You should know that the local variable will be destoried when they are not in their scope. 您应该知道,本地变量不在其范围内时将被销毁。

main function doesn't have access to the data, as scope of the array x[3] is myfun(void). main函数无权访问数据,因为数组x [3]的范围是myfun(void)。 After myfun call (it should be myfun(); instead of void myfun(); ), ptr will be pointing to an unallocated memory location. myfun调用之后(应该为myfun();而不是void myfun(); ),ptr将指向未分配的内存位置。

To print the data in main function, you can declare the int x[3] as a global variable. 要在main函数中打印数据,可以将int x[3]声明为全局变量。

int *ptr = 0;
void myfun(void);

int x[3] = {11,12,13};

int main()
{
    int *temp_ptr;
    myfun();
    temp_ptr = ptr;
    for(int j = 1; j <= 3; j++)
    {
        temp_ptr = ptr-j ;       
        cout << *temp_ptr ; cout << "\n";

    }
    return(0);  
}

After following the above answers and a little bit of googling I found out the exact solution to my problem: To return variable sized array from a function. 在遵循了以上答案并进行了一些谷歌搜索之后,我找到了解决问题的确切方法:从函数返回可变大小的数组。

This is the code: 这是代码:

#include<iostream>

using namespace std;
int * myfun(void);

int main()
{
    int *ptr;
    ptr = myfun();
    int len = (sizeof(ptr)/sizeof(*ptr));
    for(int j = 0; j <= len; j++)
    {
        cout << ptr[j];
    }
    return(0);  
}
int * myfun()
{
    static int x[3] = {11,12,13};
    return x;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM