在函数中分配后为空指针 [英] Null pointer after allocating in function

查看:202
本文介绍了在函数中分配后为空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中遇到指针问题.我声明了指针 data 并在函数中对其进行了初始化,但是在我的程序离开函数后,数据指针为NULL.我在代码下面提供输出.但是,当我删除行 getline(cin,desc); 指针 data 时,它永远不会为NULL.为什么会发生?我该怎么办才能在函数中使用字符串 getline()分配内存?

I have problem with pointer in C++. I declared pointer data and initializing it in function, but after my program left function data pointer is NULL. I provide output below code. However, when I delete line getline(cin, desc); pointer data never was NULL. Why does it happens? What should I do to allocate memory in function with string got form getline()?

#include <cstdio>
#include <iostream>

using namespace std;

struct Data
{
    string desc;
    int number;
};

void function1(int number, string desc, Data *data)
{
    data = new Data;
    data -> desc = desc;
    data -> number = number;
    printf("DURING: %p\n", data);
}

int main(int argc, char const *argv[])
{
    int number;
    string desc = "TEXT TEXT";
    getline(cin, desc);
    scanf("%i",&number);
    Data *data;
    printf("BEFORE: %p\n", data);
    function1(number,desc,data);
    printf("AFTER: %p\n", data);
    return 0;
}

输出:

之前:0x0

BEFORE: 0x0

持续时间:0x7f9ab25008c0

DURING: 0x7f9ab25008c0

之后:0x0

但是,如果我删除以下行: getline(cin,desc); 输出:

However if i delete line: getline(cin, desc); OUTPUT:

之前:0x103de4068

BEFORE: 0x103de4068

持续时间:0x7f8ed2c04aa0

DURING: 0x7f8ed2c04aa0

之后:0x103de4068

AFTER: 0x103de4068

推荐答案

您正在按值传递指针,因此该函数从main获取指针的副本,并且只能影响其自身的指针副本,而不是main中的指针.

You're passing your pointer by value, so the function gets a copy of the pointer from main, and can only affect its own copy of the pointer, not the pointer that's in main.

因此,在调用该函数之前和之后,指针只是一个未初始化的值,因此它很可能发生不可预测的变化(从技术上讲,将其打印出来或以任何其他方式使用其值会导致未定义的行为).

Therefore, both before and after calling the function the pointer is just an uninitialized value, so it's likely to vary unpredictably (and, technically, printing it out, or using its value in any other way, leads to undefined behavior).

通过引用传递指针以获取修改原始指针的功能:

Pass the pointer by reference to get the function to modify the original pointer:

void function1(int number, string desc, Data *&data)
{
    data = new Data;
    data -> desc = desc;
    data -> number = number;
    printf("DURING: %p\n", data);
}

然后,当您弄清楚这一点时,您可能想完全(几乎)使用原始指针退出.几乎总是有更好的做事方法.在这种情况下,您当前在function1中拥有的代码可能应该在构造函数中:

Then, when you've got this straightened out, you probably want to quit using raw pointers (almost) completely. There are almost always better ways to do things. In this case, the code you currently have in function1 should probably be in a constructor:

struct Data {
    string desc;
    int number;

    Data(string desc, int number) : desc(desc), number(number) {}
};

...然后在main中,您只需定义对象的实例:

...then in main, you'll just define an instance of the object:

int main() { 
    int n;

    std::cin >> n;

    Data d{"TEXT TEXT", n};
}

这篇关于在函数中分配后为空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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