-2

I have this pseudo code. This code may not be 100% correct. My question is when you declare an object using the heap? which is the proper way to access the following data. Meaning if you declare something on the heap should InfoMap also be declared on the heap? I know both would work so does that mean the compiler will figure out the best way to handle this?

class InfoMap
{
    InfoMap(){}     
    QString name;
    int age;
}
class InfoData
{
  InfoMap RetrieveInfoList()
  {
    InfoMap map;
    map.name = "name1";
    map.age = 21;
    return map;
  }   
  InfoMap* RetrieveInfoList2()
  { 
    InfoMap *map = new InfoMap();
    map->name = "name1";
    map->age = 21;
    return map;
  }
}

In a class that uses InfoData

void SomeClass::RetrieveData1()
{
    InfoData *data = new InfoData();
    InfoMap *map = data->RetrieveInfoList();
    qDebug() << map->name << map->age;
}

void SomeClass::RetrieveData2()
{
    InfoData *data = new InfoData();
    InfoMap map = data->RetrieveInfoList2();

    qDebug << map.name << map.age;
}
Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
adviner
  • 3,295
  • 10
  • 35
  • 64
  • 4
    Simply never use `new` and `delete` directly, unless you're 100% sure you need to, and none of the standard container classes or smart pointers supports your use case! – πάντα ῥεῖ Jan 02 '16 at 17:55
  • Why would my question be down voted when I clearly said its not 100% correct. I'm only writing the pseudo code to get my general question across. Who ever down voted should learn to read the question – adviner Jan 02 '16 at 18:01
  • This isn't pseudo-code. This is very specifically C++, but it doesn't even try to be correct C++ while you are asking questions that are very C++-specific. – Ulrich Eckhardt Jan 02 '16 at 18:11
  • @adviner _"Why would my question be down voted when I clearly said its not 100% correct."_ Learn to read the policies! If you're asking with a specific language tag, you don't provide pseudo code, period. Ask [tag:language-agnostic] then. – πάντα ῥεῖ Jan 02 '16 at 18:12
  • Im not gonna spend any time arguing. I asked a question and put a quick sample code and explaining that its not 100% correct. I tried to put a sample to explain what I'm asking to get my idea across. Thanks for those who have helped clear up my question. – adviner Jan 02 '16 at 18:16
  • @adviner Quick question jumping at Stack Overflow, getting an answer, and jump out again isn't helpful/great contribution in the long term view. Stack Overflow isn't your personal help-desk, though many users behave to support this. I'll leave my downvote, because I think exactly what the tooltip says: Low research efforts, not useful for future research. – πάντα ῥεῖ Jan 02 '16 at 18:22
  • Possible duplicate of [When should I use the new keyword in C++?](http://stackoverflow.com/questions/655065/when-should-i-use-the-new-keyword-in-c) – GingerPlusPlus Jan 02 '16 at 18:31

4 Answers4

1

No, you don't have to do that. However, to prevent memory leaks you must call delete eventually on everything that you have allocated with new. The difference between creating an object on the stack:

InfoData data;

And the heap:

InfoData *data = new InfoData();

Is the lifetime of the object. An object on the stack will be destroyed as soon as it goes out of scope. An object on the heap won't be destroyed as long as there is no delete call. And thus, if you forget to do this you will leak memory.

As for your code, there is no reason for you to use pointers / new, you should just resort to stack storage:

class InfoMap
{
 public:
    InfoMap()
    {
    }

    QString name;
    int age;
};

class InfoData
{
 public:
  InfoMap RetrieveInfoList()
  {
    InfoMap map;
    map.name = "name1";
    map.age = 21;
    return map;
  }
};

In a class that uses InfoData

void SomeClass::RetrieveData()
{
    InfoData data;
    InfoMap map = data.RetrieveInfoList();
    qDebug() << map.name << map.age;
}

This way object destruction is done for you by the compiler.

If you do feel the need to have dynamic allocation you should always prefer smart pointers over raw new / delete.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

My question is when you declare an object using the heap.

Usually when you need to pass or return pointers to the (newly created) objects. Or maybe if they are too big to put on the stack.

In all other cases allocating objects on the heap could be avoided. This also eliminates the need to delete then, and avoids memory leaks.

does that mean the compiler will figure out the best way to handle this?

The compiler will do what you ask it to do in this case.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
0

Both solutions are good and will work. The difference between them the is:

1) In the first case (when you alloc a InfoMap object on the stack RetrieveInfoList function and then you return it) at the return statement the copy constructor of the InfoMap object will be called an a copy of the original object will be returned

2) In the second case a InfoMap object is aloceted on the heap and a poiter to it will be returned.

So the main difference is that in the first case you get a copy of the object and in the second you get the original object. In your case, considering the structure of InfoMap, is only a issue of performace (in the first case is an addtional copy performed). But if InfoData would have contained a resource that couldn't have been handled correctly by the default copy constructor the code would't behave the same the the two cases.

Ilie NEACSU
  • 530
  • 3
  • 12
0

OP, there's no immediately obvious reason here for you to be using new and delete; you could (and should) be using the stack, not the heap, for what you are doing. The stack is going to de-allocate the memory you're using for you by automatically destroying what you create at the end of its scope, whereas using "new" makes it so that the memory is not de-allocated automatically, and needs to be freed via "delete" (which I don't see you using. You should really look up "memory leaks"). So no, don't declare InfoMap on the heap.

Mars
  • 29
  • 2