0

I wrote this code and could someone explain how many objects are created in heap and stack? Is myStudent object in heap or stack? Second question, is main method itself and the things inside of main method stored in stack?

class Student
{
public:
    Student()
    {
        id = 0;
    }
private:
    int id;
};
Student studentCreator()
{
    Student* s = new Student();
    return *s;
}
int main()
{
    Student myStudent = studentCreator();
    return 0;
}
trincot
  • 317,000
  • 35
  • 244
  • 286
  • where do you think they are stored? and why would you write code like this? don't use `new` and raw owning pointers – 463035818_is_not_an_ai Oct 22 '20 at 19:26
  • 6
    There is nothing in C++ related to heap and stack memory. C++ uses automatic storage duration and dynamic storage duration. Where those are actually stored, C++ doesn't care. – NathanOliver Oct 22 '20 at 19:30
  • 1
    `s` on the stack and `*s` is on the heap (which is leaked since it is never deallocated). `studentCreator()` return a copy of `*s` which is assigned to `myStudent` which is on the stack frame for `main`. – wcochran Oct 22 '20 at 19:31
  • 1
    @NathanOliver "heap" is the usual name of the memory area used for dynamic data, "stack" is the name of the memory area used for automatic data. Any relationship with the OS segments with the same names is purely coincidental. – Barmar Oct 22 '20 at 19:32
  • @NathanOliver C++ indeed uses stack and/or registers for automatic variables, will almost always use the heap for explicit allocated memory (unless a special allocator is used), and will use static data/bss memory for the rest. – wcochran Oct 22 '20 at 19:34
  • P.s. `Student() : id(0) {}`, or even just declare `int id=0;` and remove the constructor. – JHBonarius Oct 22 '20 at 19:35
  • 1
    Also noteworthy that this code leaks memory. The necessary `delete` can't be done, since the address of the allocated object is lost. – πάντα ῥεῖ Oct 22 '20 at 19:35
  • @Barmar What would you call the memory area, which is the backup of a placement new then? It's used for _dynamic data_ as well, no? – πάντα ῥεῖ Oct 22 '20 at 19:37
  • @πάνταῥεῖ Placement new doesn't allocate any memory at all, it uses whatever memory area the address points to. – Barmar Oct 22 '20 at 19:39
  • @Barmar Sure, I well know that. Though, if I am right in my interpretation of the standard terminology, it's still a _dynamic allocation_, no matter if the memory backing was allocated at the _"stack"_, no? – πάντα ῥεῖ Oct 22 '20 at 19:44
  • @πάνταῥεῖ I simply find these pedantic complaints about common terminology pointless and more likely to confuse people than help. – Barmar Oct 22 '20 at 19:47
  • @πάνταῥεῖ placement new is not considered to perform any allocation. The resulting object's storage duration is unclear. I'm not sure you can accurately assign storage duration to a placement new'ed object. – François Andrieux Oct 22 '20 at 19:49
  • @Barmar I can agree with that. But there must be a reasoning why it isn't mentioned in the c++ standard. I suspect it's because it's an OS thing, rather than a definition essential for the language. – πάντα ῥεῖ Oct 22 '20 at 19:50
  • @πάνταῥεῖ Standard go out of their way to describe things in an abstract way, to avoid the appearance of specifying implementation details. – Barmar Oct 22 '20 at 19:51
  • @FrançoisAndrieux That's exactly my point. You cannot, while for _automatic storage_ you can tell. – πάντα ῥεῖ Oct 22 '20 at 19:51
  • @πάνταῥεῖ I'm not sure what you mean. If you mean that placement new over storage that has automatic storage results in an object with automatic storage, that is not accurate. The new'ed object will not be destroyed (though its storage will). Edit : New expression specifies that it always results in an object with dynamic storage duration, even placement new. – François Andrieux Oct 22 '20 at 19:53
  • @FrançoisAndrieux No, you misunderstood. I meant that placement new still is dynamic allocaiton, no matter where the backing memory comes from. But well, as _@barmar_ mentioned it's a bit of useless quibbling if it comes to practical implementations of the c++ language. _"New expression specifies that it always results in an object with dynamic storage duration, even placement new."_ that's all I wanted to tell. – πάντα ῥεῖ Oct 22 '20 at 19:55
  • @FrançoisAndrieux Ah yes, _allocation_ and _duration_ make a difference. – πάντα ῥεῖ Oct 22 '20 at 20:00
  • @πάνταῥεῖ I removed my last comment because it is a matter of interpretation. Placement new *does* call an allocation function but the placement allocation functions that can called do not allocate. – François Andrieux Oct 22 '20 at 20:01
  • @FrançoisAndrieux Which makes me wonder now: Does a constuctor function _allocate_ the class members (in terms of correct memory layout)? – πάντα ῥεῖ Oct 22 '20 at 20:06
  • No matter whatever. This question is the umpteenth gazillionth dupe of already asked and answered questions here (and most of them were asked in better ways). – πάντα ῥεῖ Oct 22 '20 at 20:09

2 Answers2

2

myStudent is on the stack. During the function call you are creating something in the heap and losing its reference.

vmp
  • 2,370
  • 1
  • 13
  • 17
1

Here you have myStudent on the stack because that function creates a student on the heap but return it dereferencing it, then you have a memory leak. The main function is stored on the stack by the operating system.

F.Guerinoni
  • 277
  • 2
  • 12
  • "The main function is stored on the stack" -- It is unclear what you mean with these words. Do you mean the actual code for the function `main` is stored on the stack (which would be wrong)? – Andreas Wenzel Oct 22 '20 at 19:51