Created
April 7, 2019 20:51
-
-
Save santiagosilas/11aba808342752cae9391c2dd79ade25 to your computer and use it in GitHub Desktop.
ANSI C - malloc
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 = 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