Assigning an integer value to the heap

  #include<iostream>
  using namespace std;
  int main() 
  { 
    int i= new int;
    cout<<i;
    return 0;
  }

when I tried to assign an int value I for its heap Error: invalid conversion from 'int *' to 'int'

when I assign a value to an int pointer, then it assigns a value to it. Why can't we assign an int value to heap.Yes I know as much as possible without assigning a value to the heap. I looked at a lot of stackoverflow posts that all explained about int * p = new int [10]; Please someone explain this to me. Thank you for your help.

+4
source share
2 answers

new intreturns a pointer to int, that is int*. Assigning it intis an obvious type mismatch.

, new T, a.k.a., T. T .

:

int* ptr = new int;
*ptr = 1;
cout << *ptr;    // prints 1

delete


, , . . :

int i = 1;
+4
new int;

int, int . int, int, int*.

,

int i = 42;

.

:

,

int* i = new int;

, . , new, , delete; , , new delete.

+1

Source: https://habr.com/ru/post/1626671/


All Articles