Skip to content

Instantly share code, notes, and snippets.

@santiagosilas
Created April 7, 2019 20:51
Show Gist options
  • Select an option

  • Save santiagosilas/11aba808342752cae9391c2dd79ade25 to your computer and use it in GitHub Desktop.

Select an option

Save santiagosilas/11aba808342752cae9391c2dd79ade25 to your computer and use it in GitHub Desktop.
ANSI C - malloc
/**
Exemplifica o uso de sizeof em C */
#include <stdio.h>
#include<stdlib.h>
int main(void) {
// Uma variável declarada dinamicamente
int *x = malloc(1 * sizeof(int));
// Se ocorrer erro ao alocar
if(!x)
printf("Memory not allocated!");
*x = 42;
printf("x = %d\n", *x);
// Quando não precisamos mais desta variável, é só liberar a memória
free(x);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment