8000 [임베디드 C언어] 메모리 동적 할당 calloc, realloc 사용 · hellonayeon/c-cpp@e96aa23 · GitHub
[go: up one dir, main page]

Skip to content < 8000 span data-view-component="true" class="progress-pjax-loader Progress position-fixed width-full">

Commit e96aa23

Browse files
committed
[임베디드 C언어] 메모리 동적 할당 calloc, realloc 사용
1 parent 2b6f602 commit e96aa23

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* calloc, realloc 사용해서 매모리 동적 할당 받기 */
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
7+
8+
int main(void)
9+
{
10+
struct arr {
11+
int size;
12+
int max_size;
13+
int *array;
14+
} *lpArray;
15+
16+
lpArray = (struct arr*)malloc(sizeof(struct arr));
17+
lpArray->size = 0;
18+
lpArray->max_size = 5;
19+
lpArray->array = (int *)calloc(lpArray->max_size, sizeof(int));
20+
21+
while (1)
22+
{
23+
if (lpArray->size >= lpArray->max_size)
24+
{
25+
printf("array is full!!\n");
26+
lpArray->max_size += 5;
27+
lpArray->array = (int *)realloc(lpArray->array, lpArray->max_size * sizeof(int));
28+
}
29+
30+
scanf("%d", &lpArray->array[lpArray->size]);
31+
if (lpArray->array[lpArray->size] == -1) break;
32+
lpArray->size++;
33+
}
34+
35+
int i;
36+
for (i=0; i<lpArray->size; i++)
37+
{
38+
printf("%d\t", lpArray->array[i]);
39+
}
40+
printf("\n");
41+
42+
free(lpArray->array);
43+
free(lpArray);
44+
45+
return 0;
46+
}

0 commit comments

Comments
 (0)
0