Another calloc() and malloc() C function usage - the dynamic memory allocation

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Allocate and reallocate memory storage

To show: Another calloc() and malloc() C function usage - the C dynamic memory allocation

 

/* Another calloc() and malloc() example */

#include <stdlib.h>

#include <stdio.h>

#define n 10

 

/* a struct */

typedef struct book_type

{

int id;

char name[20];

float price;

}book;

 

int main(void)

{

int *aPtr = NULL, *bPtr = NULL, m = 0;

char *str = NULL;

book *bookPtr = NULL;

 

/* create an int array of size 10 */

aPtr = (int *)calloc(n, sizeof(int));

 

/* do some verification */

if(aPtr == NULL)

{

printf("calloc() for integer fails lol!\n");

exit (0);

}

else

{

printf("Memory allocation for int through calloc() is OK\n");

printf("The allocated memory is n times sizeof(int): %d bytes\n", n * sizeof(int));

}

 

/* create a char array of size 10 */

str = (char *)calloc(n, sizeof(char));

if(str == NULL)

{

printf("calloc() for char fails lol!\n");

exit (0);

}

else

{

printf("\nMemory allocation for char through calloc() is OK\n");

printf("The allocated memory is n times sizeof(char): %d bytes\n", n * sizeof(char));

}

 

/* create a structure of book */

bookPtr = (book *)malloc(sizeof(book));

if(bookPtr == NULL)

{

printf("malloc() for struct fails lol!\n");

exit (0);

}

else

{

printf("\nMemory allocation for struct through malloc() is OK\n");

printf("The allocated memory is: %d bytes\n", sizeof(book));

}

 

/* clean up or return to the system of the allocated memory */

free(aPtr);

free(str);

free(bookPtr);

 

/* other way, get the number of elements from the user and then allocate */

printf("\nEnter the size of integer array (bytes): ");

 

/* scanf("%d", &m); */

scanf_s("%d", &m, sizeof(m));

bPtr = (int *)calloc(m, sizeof(int));

if(bPtr == NULL)

{

printf("calloc() for int fails lol!\n");

exit (0);

}

else

{

printf("\nMemory allocation for int through calloc() is OK\n");

printf("The allocated memory is m times sizeof(int): %d bytes\n", m * sizeof(int));

}

free(bPtr);

return 0;

}

 

Output example:

 

Memory allocation for int through calloc() is OK

The allocated memory is n times sizeof(int): 40 bytes

Memory allocation for char through calloc() is OK

The allocated memory is n times sizeof(char): 10 bytes

Memory allocation for struct through malloc() is OK

The allocated memory is: 28 bytes

Enter the size of integer array (bytes): 40

Memory allocation for int through calloc() is OK

The allocated memory is m times sizeof(int): 160 bytes

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index