Skip to content

Instantly share code, notes, and snippets.

@santiagosilas
Last active April 7, 2019 20:54
Show Gist options
  • Select an option

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

Select an option

Save santiagosilas/b5a8d0d35d426ba8419f460ffae5c4fb to your computer and use it in GitHub Desktop.
ANSI C - CALLOC
/**
Exemplifica o uso de sizeof em C */
#include <stdio.h>
#include<stdlib.h>
int main(void) {
// Uma variável declarada dinamicamente
int *x = calloc(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