Created
July 20, 2021 17:21
-
-
Save Thiago4532/a0eceb41a6df14a4491ec5ec9ae7bd53 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() { | |
// Cria um novo inteiro dinamicamente e guarda a memória dele no ponteiro. | |
int *p = new int; | |
*p = 42; | |
cout << *p << '\n'; // 42 | |
// Também é possivel inicializar o new com algum valor | |
int *p2 = new int(27); | |
cout << *p2 << '\n'; // 27 | |
// Também é possível criar vetores com o new | |
int *vet = new int[10]; | |
// Como as variáveis não são apagadas quando a função termina, é necessário | |
// liberar a memória que foi criada pra elas usando o operador delete. | |
delete p; | |
delete p2; | |
// Se for um vetor é necessário colocar os colchetes no delete. | |
delete[] vet; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment