Operation of the new operator

Asked

Viewed 1,091 times

4

I wanted to understand basically what the logic behind the objects of the classes that use the operator new for example, I have the following program in D:

import std.stdio;

class Hello
{
 public this(){} //construtor

 public void print()
 {
  writeln("Hello World");
 }
}

void main()
{
  Hello h = new Hello();
  h.print();
}

In C++

#include <iostream>

class Hello
{
  public:
  Hello(){} //construtor

 void print()
 {
   std::cout<<"Hello World";
 }
}

void main()
{
  Hello *h = new Hello();
  h->print();
}

How does this object pointer allocation work in the language for the programmer to do h.print() and not h->print()?

The compiler generates more code behind and makes allocation or the language developer defines this way of working in the language itself?

You could simulate it in C++ like this?

Hello h = new Hello();
h.print();

instead of:

Hello *h = new Hello();
h->print();

2 answers

4


Firstly, I would point out that:

ponteiro->metodo();

Corresponds to

(*ponteiro).metodo();

And that’s what they call syntactic sugar, that is, a more convenient way for the programmer to do the same.

Analyzing the instruction, we see that we are obtaining the value pointed by the pointer with the * and on that value access to metodo.

It means that the example you presented could also be written like this:

Hello *h = new Hello();
(*h).print();

However it would be much less practical.

Operator new

The operator new in C++ always returns the memory address where the object was placed by the memory allocator. This is equivalent to malloc in C.

And that’s why you always see code that holds the result of new on a pointer, such as the one presented:

Hello *h = new Hello();
h->print();

But it is not required. I could only save value pointed by the received pointer, like this:

Hello h = *(new Hello()); //atente no * para obter o objeto que o ponteiro aponta
h.print();

But it will not simplify the use properly and will make it more difficult later when you need to pass the pointer to the object in other functions

  • Very interesting I have to take shame in the face and get the nose learns pointer, allocation, relocation and memory management..

  • But how so "will make it harder later when you need to pass the pointer to the object in other functions" if I understand correctly you mean that it would make it difficult to use this pointer to create a new object is this?

  • @dark777 I refer to situations where functions are waiting for pointers and you have objects come back to fetch the address to call the function. I’m basically talking at the syntax level. But in all the codes we see in C++ is always used the new and I keep the value in a pointer, is what is considered good practice

  • I understood I created this topic about doubt to understand if such a feat was possible and if it was how he would work back if it did only memory allocation or reallocation or if it gave return of *this within some kind of constructor for such but already elucidated me well the doubt... Thanks for the feedbacks ....

  • @dark777 No problem. It is always important to understand and deepen the pointer and memory part as it is vital in C/C++. Good studies.

3

There is a huge difference between C++ and D. D creates classes always by reference and allocates in heap managed. In C++ a class is just a structure (struct) whose members are private by default, but nothing. Whether to allocate to heap unmanaged must use the new to say this to the compiler. In D it is only to make readable that it is your intention even.

For another side in modern C++ it is rare to use this form, today it is used to use semi-managed memory with smart pointers.

In general compilers generate code "too much" all the time, it is one of its main functions.

The allocation is made by the operator new and so on. Internally the operator probably uses the function malloc(), but not necessarily. It is possible to define in the class its own operator new.

C++ likes to be explicit about how to access the object whereas D doesn’t care about this, so trying to use the point operator instead of the arrow is gambiarra and not idiomatic in C++.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.