Skip to content

Instantly share code, notes, and snippets.

@Thiago4532
Created July 20, 2021 17:21
Show Gist options
  • Save Thiago4532/a0eceb41a6df14a4491ec5ec9ae7bd53 to your computer and use it in GitHub Desktop.
Save Thiago4532/a0eceb41a6df14a4491ec5ec9ae7bd53 to your computer and use it in GitHub Desktop.
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