首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个新对象具有相同的指针地址

多个新对象具有相同的指针地址
EN

Stack Overflow用户
提问于 2014-10-24 16:43:18
回答 1查看 1.4K关注 0票数 0

我是C++的新手,所以如果这是个愚蠢的问题,我马上道歉,但我的生活似乎无法弄清楚到底发生了什么。本质上,我输入一个创建对象的助手函数,然后返回指向每个对象的指针。它工作正常,但出现的问题是,有时指针值与函数调用的最后一次迭代完全相同。

例如:

我经常看到一些东西..。

代码语言:javascript
复制
0x7fff5d1d0f10
0x7fff5d1d0d80
0x7fff5d1d0d80 <- same as the last pointer
0x7fff5d1d0fe0
0x7fff5d1d0fe0 <- same as the last pointer

这是不明确的行为吗?我非常非常感谢所有的帮助!

指针是此助手函数的返回值(对不起,有点冗长):

代码语言:javascript
复制
w5::iMessage* w5::getMessage(std::ifstream& file, char limiter){

    std::string result;

    int line_no = 0;

    std::string line;
    while (getline(file, line)){
        if(line[0] == 'T' or line[0] == 'e'){
            iMessage * message;
            message = nullptr;
            std::string user = "";
            std::string reply = "";
            std::string tweet = "";

            if (line[0] == 'T'){
                int length = line.length();
                int _user = line.find("T");
                int _reply = line.find("@");
                if(_reply != std::string::npos){
                    int first_space = line.find_first_of(" ");
                    int second_space = line.find_first_of(" ", _reply);
                    //user
                    //std::cout << line.substr(_user+1, _reply-2) << std::endl;
                    user = line.substr(_user+1, _reply-2);
                    //reply
                    // std::cout << line.substr(_reply+1, second_space-_reply)  << std::endl;
                    reply = line.substr(_reply+1, second_space-_reply);
                    //tweet
                    //std::cout << line.substr(second_space+1)  << std::endl;
                    tweet = line.substr(second_space+1);
                    Twitter twitter(user, tweet, reply);
                    // std::cout << &twitter << std::endl;
                    message = &twitter;
                    // std::cout << message << std::endl;
                    return message;

                }else{
                    int _tweet = line.find(" ");

                    //user
                    //std::cout << line.substr(_user+1, _tweet) << std::endl;
                    std::string user = line.substr(_user+1, _tweet);

                    //tweet
                    if(_tweet != std::string::npos){
                        // std::cout << line.substr(_tweet+1)  << std::endl;
                        std::string tweet = line.substr(_tweet+1);
                        if(tweet != ""){
                            Twitter twitter(user, tweet, "");
                            iMessage * message;
                            // std::cout << &twitter << std::endl;
                            message = &twitter;
                            // std::cout << message << std::endl;
                            return message;
                        }
                    }
                }


            }

            if(line[0] == 'e'){
                int length = line.length();
                int _from = line.find("From:");
                int _to = line.find(" To:");
                int _date = line.find(" Date:");
                int _body = line.find(" Body:");
                std::string to = "";
                std::string from = "";
                std::string date = "";
                std::string body = "";
                //from
                //std::cout << line.substr(_from+5, _to-_from-4) << std::endl;
                to = line.substr(_from+5, _to-_from-4);
                //to
                // std::cout << line.substr(_to+4, _date-_to-3) << std::endl;
                from = line.substr(_to+4, _date-_to-3);
                //date
                // std::cout << line.substr(_date+6, _body-_date-6) << std::endl;
                date = line.substr(_date+6, _body-_date-6);
                //body
                // std::cout << line.substr(_body+6) << std::endl;
                body = line.substr(_body+6);
                Email email(from, to, body, date);
                // std::cout << &email << std::endl;
                message = &email;
                // std::cout << message << std::endl;
                return message;
            }

            result += line + "\n";
            line_no++;
        }
    }

    iMessage *message;
    message = nullptr;
    return message;

}

这些方面存在的问题如下:

代码语言:javascript
复制
        Email email(from, to, body, date);
        // std::cout << &email << std::endl;
        message = &email;
        // std::cout << message << std::endl;
        return message;

由于某种原因,&email的指针值似乎与最后一次迭代相同,而只是一个新的指针值。函数的其他返回点也存在同样的问题。

'iMessage消息‘是一个抽象的基类。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-24 16:47:50

您正在堆栈上创建对象并返回它们。这是一件很糟糕的事情,因为堆栈上的对象将被销毁,当创建对象的作用域被留下时;( b)您返回的内存不是要返回的。

您需要分配对象以返回堆中的对象,并通过指针返回它们,然后在处理完它们之后清理它们。您可能希望考虑返回某种智能指针来管理正在分配的动态内存的生存期。

所以,而不是

代码语言:javascript
复制
    Email email(from, to, body, date);
    // std::cout << &email << std::endl;
    message = &email;
    // std::cout << message << std::endl;
    return message;

你想这么做:

代码语言:javascript
复制
    message = new Email(from, to, body, date);

    return message;

当前实现有时显示相同的内存地址的原因很简单,因为这些对象碰巧是在堆栈上的同一个位置创建的。错误在于,您正在返回指向这些基于堆栈的对象的指针,而不是在堆上分配,并返回一个可以超过函数调用的对象。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26552476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档