Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7596251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:52:28+00:00 2026-05-30T21:52:28+00:00

(Not sure if this title is the best description…) Hi all. I need a

  • 0

(Not sure if this title is the best description…)

Hi all. I need a help with the following code. In process function, I want cc and cc_sub (which is obtained as a field in cc) to show different contents (as they are originally made so) but they show the same. Storage instances are made in makeObj function. Could someone tell me what’s wrong and how to work this around? Thank you.

Int process(const Storage cc) {
    Storage cc_sub = (Storage&) cc.data;  // Here
    cout << ... << endl;         // Contents from cc and cc_sub are the same though they are not supposed to be.
}

class Storage {
    public:
        Storage() {
            this->data = NULL;
        }
        Storage(const Storage& obj) { //Copy Constructor. Result is the same even if I removed this entire clause.
            this->data = obj.data;
        }
        void* data;
        void setData(Storage dPtr) {
            this->data = &dPtr;
        }
};

Storage makeObj(std::string* s) { // I want to keep the return value as value instead of pointer, 
                       // since when it returns pointer the values pointed by reference behaved unexpectedly (guess it's because of temporary object type of error)... 
    Storage cc;
    :
    cc.setData(makeObj(&subString)); 
    return cc;
    :
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:52:30+00:00Added an answer on May 30, 2026 at 9:52 pm

    The odd behavior you’re seeing is due to manipulating temporary Storage variables, both within setData and makeObj. The outcome of such manipulation is undefined in C++.

    Try the following:

    void process(const Storage cc) {
        Storage cc_sub = *((Storage*)cc.data);
        cout << ... << endl;
    }
    
    class Storage {
        public:
            Storage() {
                this->data = NULL;
            }
    
            Storage(const Storage& obj) {
                this->data = obj.data;
            }
            void* data;
            void setData(Storage* dPtr) {
                this->data = dPtr;
            }
    };
    
    Storage* makeObj(std::string* s) {
        // Allocate memory on the heap so that cc lives beyond this function call
        Storage* cc = new Storage;
        :
        // Populate the sub-storage with a heap-allocated variable
        cc->setData(makeObj(&subString));
        return cc;
    }
    

    Note, the code as currently written makeObj will cause a stack overflow if called as it has no way of ending its recursion…

    To cHao’s suggestion here’s a more complete program using shared_ptr (Microsoft Visual Studio 2008 version – you may need to change #include <memory> and/or std::tr1::shared_ptr depending on your compiler). I’ve changed the type of the data from void* to Storage (shared_ptr) and added a string member to better show the differences:

    #include <string>
    #include <iostream>
    #include <memory>
    
    using namespace std;
    using namespace std::tr1;
    
    class Storage {
        public:
            Storage() {
            }
            Storage(const Storage& obj) {
                this->data = obj.data;
            }
        string s;
        shared_ptr<Storage> data;
            void setData(shared_ptr<Storage> dPtr) {
                this->data = dPtr;
            }
    };
    
    void process(const shared_ptr<Storage> cc) {
        shared_ptr<Storage> cc_sub = cc.get()->data;
        cout << "Parent string: " << cc.get()->s << endl << 
                "Child string: " << cc_sub.get()->s << endl;
        cout << "Parent data pointer: " << cc.get()->data << endl << 
                "Child data pointer: " << cc_sub.get()->data << endl;
    }
    
    shared_ptr<Storage> makeObj(string* s) {
        shared_ptr<Storage> cc(new Storage);
        if (s->length() != 0) {
            string subString = s->substr(0, s->length()-1);
            cc->s = subString;
            cc->setData(makeObj(&subString));
        }
    
        return cc;
    }
    
    
    int main(int argc, char* argv[]) {
        string s = "hello";
        shared_ptr<Storage> storage = makeObj(&s);
        process(storage);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for sure this is not the best title. I'm creating a system to generate
Not sure what the best title would be for this question, or even if
I'm not sure how best to word the Title of this, so if someone
I'm not sure how to put this best into a title, but this is
Not sure I have the best title for this question. Feel free to modify
I'm not the sure title of this question is the best, but hopefully this
My apologies for an inaccurate title, but I'm not sure what this is called
Not entirely sure of a good title for this, feel free to edit it
Not sure if I've just missed something but this doesn't work: $(this).children('td.threadtitle a').html('thread title');
Disclaimer: not sure this is WordPress related or not. I'm following a simple tutorial

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.