簡體   English   中英

多個新對象具有相同的指針地址

[英]Multiple New Objects Have The Same Pointer Addresses

我是C ++的新手,所以如果這是一個愚蠢的問題,我會立即道歉,但是我似乎一輩子都無法弄清楚發生了什么。 本質上,我輸入一個創建對象的助手函數,然后返回指向每個對象的指針。 它可以正常工作,但是彈出的問題是有時指針值與函數調用的最后一次迭代相同。

例如:

我會經常看到一些東西...

0x7fff5d1d0f10
0x7fff5d1d0d80
0x7fff5d1d0d80 <- same as the last pointer
0x7fff5d1d0fe0
0x7fff5d1d0fe0 <- same as the last pointer

這是不確定的行為嗎? 我將非常非常感謝您的幫助!

指針是此輔助函數的返回值(抱歉,它有點冗長):

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;

}

這些問題存在於以下行:

        Email email(from, to, body, date);
        // std::cout << &email << std::endl;
        message = &email;
        // std::cout << message << std::endl;
        return message;

由於某些原因,&email的指針值似乎與上一次迭代相同,而不是新的指針值。 函數的其他返回點中存在相同的問題。

“ iMessage消息”是抽象的基類。

您正在堆棧上創建對象並返回它們。 這是一件不好的事情,因為堆棧上的對象將a)在創建它的作用域被保留時被銷毀,並且b)您要返回的內存不是您要返回的。

您需要分配對象以在堆上返回並通過指針返回它們,然后在完成處理后將其清理。 您可能希望考慮返回某種智能指針來管理分配的動態內存的生命周期。

所以,代替

    Email email(from, to, body, date);
    // std::cout << &email << std::endl;
    message = &email;
    // std::cout << message << std::endl;
    return message;

您想這樣做:

    message = new Email(from, to, body, date);

    return message;

當前實現有時顯示相同的內存地址的原因僅是因為恰好在堆棧的同一位置創建了對象。 錯誤是您要返回指向這些基於堆棧的對象的指針,而不是在堆上分配並返回可以超出函數調用壽命的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM