Last active
April 7, 2019 20:54
-
-
Save santiagosilas/b5a8d0d35d426ba8419f460ffae5c4fb to your computer and use it in GitHub Desktop.
ANSI C - CALLOC
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
| /** | |
| 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