-1
Class C {
 struct Something {
   string s;
   // Junk.
 }
 // map from some string to something.
 map<string, Something> map;

 // Some more code:
 const Something *Lookup(string k) const {
   const something *l = SomeLookUpFunction();
   cout << l;
   cout << &l->s;
   cout << l->s;
   return l;
  }
}

// Some Test file
const C::Something *cs = C::Lookup("some_key");
cout << cs;
cout << &cs->s;
cout << cs->s;

The weird thing is this outputs:
* For lookup fucntion:
0x9999999
0x1277777
some_string

* For test code
0x9999999
0x1277777
000000000000000000000000000000000000000000 ....

In test file it gives a very long string of zeros, but the addresses are the same. Any idea what could be going wrong ?

dhruv
  • 19
  • 5
  • is `lookup` static member function or not? – Michał Walenciak Mar 08 '16 at 08:21
  • 4
    Its weird that it creates any output at all. I wouldn't even expect it to compile. Please provide a minimal, complete and verifiable example. – Markus Mayr Mar 08 '16 at 08:22
  • C is a class that contains struct Something. – dhruv Mar 08 '16 at 08:22
  • 1
    We really have to see definition of SomeLookUpFunction() – Jojje Mar 08 '16 at 08:23
  • 2
    It's good to provide minimal example but it must be still working. `lookup` function is probably static according the way you call it. I assume the cause for the failure is that you temporarily allocate instance of `C` class in `someLookupFunction` or `lookup` function which no longer exists (and neither all its data) when returned. – Zbynek Vyskovsky - kvr000 Mar 08 '16 at 08:24
  • Most like, `SomeLookupFunction` returns a pointer into an object that ceases to exist when the function returns. Show us the code to `SomeLookupFunction`. – David Schwartz Mar 08 '16 at 08:25
  • lookup is not static. SomeLookUpFunction basically looksup in the map, which is a member of Class C. My problem is that before returning the pointer everything, runs prints out fine. When I use the returned pointer, the address is the same, however value is something else. – dhruv Mar 08 '16 at 08:27
  • @dhruv Often, when a function "basically" does something, what it *actually* does is something that is wrong. Post the definition of this function. – molbdnilo Mar 08 '16 at 08:38
  • Sorry, for posting an question not formed properly, I was really in the middle of something, and got stuck really badly. Anyways, I got the hint about what could be possibly wrong from the answer. Thanks anyways. I will update the question properly in a while. – dhruv Mar 08 '16 at 08:47

1 Answers1

0

Since you have not shared code for function SomeLookUpFunction, I have to guess that you are returning pointer to local object of type Something. This is a bad idea, see similar QA.

To start fixing your code, you should start by returning simple object, instead of pointer, as shown below:

 // Some more code:
 const Something lookup(string k) const {
   const something l = SomeLookUpFunction(); // return simple object
   cout << &l;
   cout << &l.s;
   cout << l.s;
   return l; // same object 
  }

Of course you should improve the code by providing copy constructors for type something and even improving your map.

Community
  • 1
  • 1
vcp
  • 962
  • 7
  • 15
  • Yes, I am returning a pointer to a local object. I suspected it might be the case. Thanks for answering. – dhruv Mar 08 '16 at 08:33