-1

This code is calling a class Rectangle where there is an area function defined, it is calculating the area and returning it. but I have a doubt where is the object p created for this class in the memory.

int main() {

    Rectangle r;
    Rectangle *p;
    p = &r;

    r.length = 10;
    r.breadth = 10;
    cout<<p-> area();
    return 0;
}
rawrex
  • 3,929
  • 2
  • 7
  • 22
  • Note, there's no *dynamic memory* allocation here. – rawrex May 17 '22 at 05:54
  • 4
    `r` is an object on the stack, while `p` is a value on the stack containing the address of `r`. A reasonable compiler might optimize `p` away completely. Not only can it be potentially stored in a register, but it is kinda redundant. – paddy May 17 '22 at 05:55
  • Stack memory is also dynamic. These values could theoretically be also placed in the static memory also existing compilers will use stack. – Vladimir F Героям слава May 17 '22 at 05:58
  • @VladimirFГероямслава Agree! Yet my input was based on the conventional use of the *"dynamic memory"* term. As a memory allocated on the heap, by explicitly using `new`/`delete`. – rawrex May 17 '22 at 06:02
  • 2
    `where is the object p`. Note, p is not an object, p is the pointer to r, r is the object. – Zhang May 17 '22 at 06:23
  • 2
    @zhang A pointer is also an object in the C++ terminology. It's an object of a pointer type. On the contrary, a reference is not an object by itself. See, e.g., [C++ difference between reference, objects and pointers](https://stackoverflow.com/q/3224155/580083). – Daniel Langr May 17 '22 at 06:30
  • The pointer `p` points to the address of `r` variable, which is in the Stack, the portion of memory where the program "lives": https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – 440 May 17 '22 at 09:03
  • @VladimirFГероямслава local variables are objects with *automatic* storage duration. There are no *dynamic* storage duration objects in this snippet – Caleth May 17 '22 at 09:44
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 17 '22 at 11:04
  • @Caleth That is really just word playing. Automatic allocation is one od the kinds of dynamic memory allocations (as opposed to static). – Vladimir F Героям слава May 17 '22 at 11:16
  • @VladimirFГероямслава no, there are 4 [storage durations](https://en.cppreference.com/w/cpp/language/storage_duration#Storage_duration) in C++: *automatic*, *dynamic*, *static* and *thread* – Caleth May 17 '22 at 11:21
  • @Caleth However, I did not refer to *storage durations* in any of my comments you seem to be replying to. In particular, my first comment replies to another comment that is about *memory allocations*. – Vladimir F Героям слава May 17 '22 at 11:44
  • @VladimirFГероямслава storage durations is what C++ talks about, and they all describe *when* memory is allocated and deallocated – Caleth May 17 '22 at 11:45
  • @Caleth If you want to do this nitpicking, do it with someone else please. I was speaking about how the memory is actually used under the hood by the machine code or assembly, without worrying about the actual words used in the particular C, C++, Fortran, Algol or whatever standard, because these terms are often specific to that very standard. Stack memory and automatic storage is one of the possible ways how to use dynamic memory. Old programming languages that only had static memory didn't have any stack at all. – Vladimir F Героям слава May 17 '22 at 11:48
  • @Caleth Or simply put, I was replying to one specific comment, namely the very first one. My words cannot be taken out of context for some strawman language lawyer argumentation. They were only used as a reaction to that very specific comment. – Vladimir F Героям слава May 17 '22 at 11:50

0 Answers0