When to use new in declaration of pointers and when this

-1

When declaring pointers I have always used the following form:

int *edad = №

But I only see in many places how, when declaring a pointer, they use something similar to:

int *edad = new int;

What is the difference between the two forms?

And the second question is: When is this used and why / for what?

Edit:

I have found a possible utility, and it is to ask the user the amount of data to be stored, in order to reserve the memory based on the data to be entered. Something like this:

cout << "Cuantos nombres quieres introducir?: ";
int t;
cin >> t; 
cin.ignore();
string *nombre = new string[t];

for(int i=0; i!=t; i++){
    cout << "Escribe un nombre: ";
    getline(cin, nombre[i]);
    cout << endl;
}

cout << endl << endl;
for(int i=0; i!=t; i++){
    cout << i+1 << ": " << nombre[i] << endl;
}
    
asked by Jogofus 14.09.2017 в 03:29
source

2 answers

3
  

What is the difference between the two forms?

A pointer is a special variable that, instead of storing values, stores memory locations ... that is, points to other elements.

Thus, the following code:

int variable;
int* ptr = &variable;

what it does is that the pointer points to a specific variable ... if you modify the variable, the value pointed to by the pointer will also change and vice versa.

Instead, the following code:

int* ptr = new int;

What it does is, on the one hand, make a dynamic memory reservation and then modify the pointer so that it points to that memory.

What are the implications of one use and which of the other?

  • Variables have a specific scope. Outside of that scope, the variable simply does not exist and any pointer that points to the variable will become invalid (it will read garbage and write memory that it should not):

    int* func()
    {
      int var;
      int* ptr = &var;
    
      *ptr = 5; // ok
      return ptr;
    }
    
    int main()
    {
      int* ptr = func();
    
      *ptr = 10; // peligro!!! ptr apunta a var que ya no existe
    }
    
  • Dynamic memory does not have a defined life cycle but rather it is the algorithm itself that must free memory when it is no longer needed. In addition, the dynamic memory is usually much more abundant than the memory of the stack:

    int* func()
    {
      int* ptr = new int;
      *ptr = 5; // ok
      return ptr;
    }
    
    int main()
    {
      int* ptr = func();
      *ptr = 10; // ok
      delete ptr;
    }
    
  

When is this used and why / for what?

this is an implicit pointer that only exists within classes and structures. When you write a code like this:

struct Test
{
  int var;

  void func()
  { var = 5; }
};

The compiler interprets it like this:

struct Test
{
  int var;

  void func()
  { this->var = 5; }
};

But when should we use this ?

It is necessary to use this in those situations in which overlapping names occur. For example, in the following code:

struct Test
{
  int var;

  void func(int var)
  {
    var = 5;
  }
};

What variable are we modifying? The variable member of the structure? The local variable of the function?

The answer is: the local variable of the function.

And how could the member variable be accessed? In this case it is when we have to use this mandatorily:

struct Test
{
  int var;

  void func(int var)
  {
    this->var = var;
  }
};

In the rest of the situations, its use is not obligatory. So it is relegated to the programmer's tastes, coding rules, ...

    
answered by 14.09.2017 в 08:45
2

In both cases you are creating an object or variable and a reference to it.

Object creation:

In the first case ( int variable ) the object is created statically and stored on the stack and in the second ( new int ) is created dynamically and stored in the heap. Static creation implies that the object (the memory it occupies) is released when the scope where it was created ends. On the other hand, if the object is created dynamically it will occupy memory until it is explicitly deleted by the delete operator.

Object reference:

In the first case ( int * ptr = & variable ) a variable is created statically ( ptr ) that will save the address where the created object is located.

In the second, the same variable is created statically to which the result of the new operator is assigned, which is nothing more than the address of the created object.

Therefore, in both cases the variable ptr contains a reference to the created object (the address of the memory it occupies) and the fundamental difference between both forms is the scope from which it can be accessed. A dynamically created object can be accessed from other areas than the one created.

This behavior implies that the ptr variables that contain the reference to the object and that have been created in both cases statically, will cease to exist when the scope in which they were declared ends, leaving both objects variable without any reference that makes it accessible. This is not a problem in the first case since the object will also be destroyed because it was created statically but in the second case the object will occupy inaccessible memory until the process ends and all associated resources are released.

    
answered by 15.09.2017 в 14:37