Simple memory allocation examples
This commit is contained in:
45
chp9/malloc.c
Normal file
45
chp9/malloc.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void *xmalloc(size_t size);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *str = xmalloc(1024);
|
||||
char *src = "HelloWorld";
|
||||
for (int i = 0; i < (int)strlen(src); i++)
|
||||
str[i] = src[i];
|
||||
printf("%s\n", str);
|
||||
|
||||
int *numbers = calloc(10, sizeof(int));
|
||||
for (int i = 0; i < 10; i++) {
|
||||
numbers[i] = i + 1;
|
||||
printf("%d ", numbers[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
int *p = realloc(numbers, 2 * sizeof(int));
|
||||
if (!p) {
|
||||
perror("realloc");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 20; i++) {
|
||||
printf("%d ", p[i]);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void *xmalloc(size_t size)
|
||||
{
|
||||
void *p;
|
||||
p = malloc(size);
|
||||
if (!p) {
|
||||
perror("xmalloc");
|
||||
exit(1);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
Reference in New Issue
Block a user