Skip to content

Instantly share code, notes, and snippets.

@dj-amadeous
Created October 30, 2018 08:29
Show Gist options
  • Save dj-amadeous/7dc27bc4e52ec08d3cfb7d8096f31d7e to your computer and use it in GitHub Desktop.
Save dj-amadeous/7dc27bc4e52ec08d3cfb7d8096f31d7e to your computer and use it in GitHub Desktop.
an example of calloc from the cpp reference documentation
int calloc_example(){
int* p1 = calloc(4, sizeof(int)); // an array of 4 integers
int* p2 = calloc(1, sizeof(int[4])); // same thing, but mixed up where the size and space goes. (it doesn't matter)
int* p3 = calloc(4, sizeof *p3); // same thing but without using int, instead using the dereferenced version of p3
if(p2) {
for(int n = 0; n < 4; n++) // printing array
printf("p2[%d] == %d\n", n, p2[n]);
}
free(p1);
free(p2);
free(p3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment