What is the difference between creating an object with an asterisk and without, except for addressing not through a point, but through -> ?

When, for example, an object of the class SomeClass through Someclass obj , memory is allocated for it, and there will be some kind of own copy of the class fields. And if you create it this way: Someclass *obj , then memory will also be Someclass *obj for all its fields? It turns out that in both cases an object is created and the same amount of memory is allocated for it. What is the difference then?

    3 answers 3

    No, no, no, it's not like that.

    Text

     SomeClass obj1; 

    There is a declaration of a variable of type SomeClass . For it memory of size in sizeof(SomeClass) : if the declaration is inside a function, then on the stack, if in the global area, then the global object is allocated.

    Similarly, the text

     SomeClass* obj2; 

    There is a pointer declaration of type SomeClass* . For it, 4 or 8 bytes are allocated (or how much is in your architecture), an object of type SomeClass is not created . In order for the pointer to make sense, you must assign it the address of some object of type SomeClass . For example, you can create this object on the heap using new SomeClass (in this case, of course, memory will be obj2 ), and assign the resulting pointer to the variable obj2 .

    It became clearer?

      Wrong. All wrong. The fact is that when we do this

      Someclass obj;

      then we construct an instance of the object. At the same time, it is created on the stack or in global memory and the sizeof (Someclass) memory is allocated for it.

      If we do this:

      Someclass * obj;

      then we create a pointer. There is no fundamental difference from void* obj , except that the compiler will check how we will use this pointer and will not allow us to implement absolutely obviously delusional options. But still, he can be explained to him to shut up, if it is really necessary. The size of the pointer depends on the type of platform for which we build the program. But we can assume that it is 4 or 8 bytes. At the same time, a pointer is essentially an address in memory. No more and no less. As long as we have not tied this pointer to the object, we will not be able to apply to any fields, and we cannot call any f-tions either.

      Those.

        Someclass *obj; obj->somefunc(); // сразу вызовет падение программы 

      Usually it uses pointers like this:

        Someclass obj; Someclass *pointer; pointer = &obj; pointer->somefunc(); // OK Someclass *pointer2 = new Someclass; // объявили указатель и сразу создали объект, а затем записали его адрес в указатель. pointer2->somefunc(); // тоже OK 
      • 3
        +1, but Someclass *obj; obj->somefunc(); Someclass *obj; obj->somefunc(); causes in C ++ not a program crash, but undefined behavior (which is significantly worse). - VladD

      I’ll add to the answer above that when you declare the pointer Someclass *pointer; and then create an object using new SomeClass(... ) for example, the object itself is stored on the heap, and the pointer pointer to it on the stack is very important to understand. When the exit from the function occurs, the stack "spins up" and this pointer will be "destroyed" (in simple terms), and there will be a memory leak (memory leak) if you have not freed the memory through the delete pointer .