#include <stdio.h>
#include <stdlib.h>
int main()
{
int* pa = NULL; //pa는 int형 데이터를 가리키는 포인터
double* pb = NULL; //pb는 double형 데이터를 가리키는 포인터
pa = (int*)malloc(sizeof(int)); //malloc():지정된 바이트 수만큼 힙 메모리를 할당
pb = (double*)malloc(sizeof(double)); //반환값: void*
*pa = 10; //pa가 가리키는 메모리 주소에 10 저장
*pb = 3.14; //pb가 가리키는 메모리 주소에 3.14 저장
printf("int : %d\n", *pa); // %d:정수 출력
printf("double : %g\n", *pb); //%g: 실수 출력(소수점 자릿수, 지수 표기 자동 조절)
free(pa); // 메모리 해제-> 메모리 누적 방지를 위해서 해야함
free(pb);
}
- malloc()은 지정된 바이트 수만큼 힙 메모리를 할당함
- free(): 메모리 해제, 메모리의 누적을 방지하기 위해서 해야함
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* pa = NULL; // 정수형 메모리를 가리킬 포인터
double* pb = NULL;// 실수형 메모리를 가리킬 포인터
// CRUD(Create, Read, Update, Delete)
// 동적 메모리 할당(create)
pa = (int*)malloc(sizeof(int)); //4바이트 정수형 메모리 확보
pb = (double*)malloc(sizeof(double));// 8바이트 실수형 메모리 확보
// 값 저장 및 출력(Update & Read)
*pa = 10; //할당된 정수형 메모리 공간에 10 저장
*pb = 3.14; // 실수형 메모리 공간에 3.14 저장
printf("int : %d\n", *pa); // 결과: int: 10
printf("double : %g\n", *pb); // 결과: double: 3.14
free(pa);
free(pb); //메모리 해제=> pa=NULL; pb=NULL;로 초기화도 가능, 결론: pa, pb가 가리키는 메모리만 해제, 포인터 변수 자체는 남음
}
#include <stdio.h>
#include <stdlib.h>
// 메모리 할당 함수들(Create)
int* AllocInt()
{
return (int*)malloc(sizeof(int)); => int와 double 하나를 저장할 수 있는 크기의 힙 메모리를 할당하고 포인터 반환 , malloc()실패시, NULL 반환됨->사용시 주의 필요
}
double* AllocDouble()
{
return (double*)malloc(sizeof(double));
}
// 값 설정 함수들(Update)==> 전달받은 포인터(p)가 가리키는 위치에 값 저장
void SetInt(int* p, int data)
{
*p = data; // 해당 메모리 공간에 값을 대입
}
void SetDouble(double* p, double data)
{
*p = data;
}
// 값 출력 함수들(Read)==> 값만 받아서 출력(포인터가 아닌 복사된 값을 받음)
void PrintInt(int data)
{
printf("int : %d\n", data);
}
void PrintDouble(double data)
{
printf("double : %g\n", data);
}
// 메모리 해제 함수들(Delete)==>free()을 통해 메모리 해제, 이후에 해당 포인터 다시 사용시, '정의되지 않은 동작'이라고 뜸
void FreeInt(int* p)
{
free(p);
}
void FreeDouble(double* p)
{
free(p);
}
int main() //main 함수 동작의 흐름
{
int* pa = NULL;
double* pb = NULL;
pa = AllocInt(); //int* 메모리 할당
pb = AllocDouble();//double* 메모리 할당
SetInt(pa, 10);//SetInt(&*pa, 10); // 할당된 메모리에 10 저장
SetDouble(pb, 3.14); //3.14 저장
PrintInt(*pa); //10 출력
PrintDouble(*pb); //3.14 출력
FreeInt(pa); //메모리 해제
FreeDouble(pb);
}
#include <stdio.h>
#include <stdlib.h>
int* AllocInt()
{
return (int*)malloc(sizeof(int));
}
double* AllocDouble()
{
return (double*)malloc(sizeof(double));
}
void SetInt(int* p, int data)
{
*p = data;
}
void SetDouble(double* p, double data)
{
*p = data;
}
void PrintInt(int data)
{
printf("int : %d\n", data);
}
void PrintDouble(double data)
{
printf("double : %g\n", data);
}
void FreeInt(int* p)
{
free(p);
}
void FreeDouble(double* p)
{
free(p);
}
int main()
{
int* pa = NULL;
double* pb = NULL;
pa = AllocInt();
pb = AllocDouble();
SetInt(pa, 10);//SetInt(&*pa, 10);
SetDouble(pb, 3.14);
PrintInt(*pa);
PrintDouble(*pb);
FreeInt(pa);
FreeDouble(pb);
}
- 전체 구조 개요
- C: 동적 메모리 할당(Create)
- R: 값을 읽어 출력(Read)
- U: 메모리 값 저장(Update)
- D: 메모리 해제(Delete)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* pa = (int*)malloc(sizeof(int) * 10); //int형 크기만큼 10개의 메모리 할당, 4X10(int가 4바이트인 경우=> 40바이트), pa = 이 메모리의 블록의 시작 주소를 가리킴
for (int i = 0; i < 10; ++i) // 결과적으로 pa[0]~pa[9]: 값 1~10이 됨
pa[i] = i + 1; // pa[i]=*(pa + i)와 동일
for (int i = 0; i < 10; ++i) // 배열에 저장된 값을 한 줄씩 출력
printf("data : %d\n", pa[i]);
free(pa); // 동적으로 할당된 메모리 해제
}
#include <stdio.h>
#include <stdlib.h>
void AllocIntArray(int** pp, int capacity) //이중 포인터(int **pp)를 사용-> 함수 외부의 포인터 변수에 해당
{
*pp = (int*)malloc(sizeof(int) * capacity); // main()안에서는 AllocIntArray(&pa, 10);형태로 호출
}
void InitIntArray(int* pa, int size)
{
for (int i = 0; i < size; ++i)
pa[i] = i + 1;
}
void PrintIntArray(int* pa, int size)
{
for (int i = 0; i < size; ++i)
printf("data : %d\n", pa[i]);
}
void FreeIntArray(int* pa)
{
free(pa);
}
int main()
{
int* pa = 0;
AllocIntArray(&pa, 10);
InitIntArray(pa, 10);
PrintIntArray(pa, 10);
FreeIntArray(pa);
}
#include <stdio.h>
#include <stdlib.h>
int* AllocIntArray(int capacity) // 반환값으로 포인터 전달
{
return (int*)malloc(sizeof(int) * capacity);
}
void InitIntArray(int* pa, int size)
{
for (int i = 0; i < size; ++i) //main에서는 int* pa=AllocIntArray(10);처럼 간단히 사용
pa[i] = i + 1;
}
void PrintIntArray(int* pa, int size)
{
for (int i = 0; i < size; ++i)
printf("data : %d\n", pa[i]);
}
void FreeIntArray(int* pa)
{
free(pa);
}
int main()
{
int* pa = AllocIntArray(10);
InitIntArray(pa, 10);
PrintIntArray(pa, 10);
FreeIntArray(pa);
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 10;
int b = 20;
int* p = NULL;
//p = &a; //활성화하면 출력값은 10
p = &b; // 포인터 p가 b를 가리키므로 *p==20
printf("data : %d\n", *p);
}
#include <stdio.h>
#include <stdlib.h>
void AssignAddress(int** pp,int* pdata)
{
*pp = pdata;
}
int main()
{
int a = 10;
int b = 20;
int* p = NULL;
//p = &a;
//p = &b;
AssignAddress(&p, &b);
printf("data : %d\n", *p);
}
#include <stdio.h>
#include <stdlib.h>
typedef int I;
typedef int* PI;
void AssignAddress(PI* pp, I* pdata)
{
*pp = pdata;
}
int main()
{
I a = 10;
I b = 20;
PI p = NULL;
//p = &a;
//p = &b;
AssignAddress(&p, &b);
printf("data : %d\n", *p);
}
#include <stdio.h>
struct Point
int x;
int y;
};
int main()
{
int n = 10;
Point pt = { 2,3 };
printf("%d\n", n);
printf("(%d, %d)\n", pt.x, pt.y);
}
#include <stdio.h>
struct Point
{
int x;
int y;
};
int main()
{
Point pt = { 2,3 };
printf("(%d, %d)\n", pt.x, pt.y);
printf("%p %p\n", &pt, &pt+1);
printf("%p %p\n", &pt.x, &pt.x+1);
Point* p1 = &pt;
int* p2 = &pt.x;
}
#include <stdio.h>
struct Point
{
int x;
int y;
};
int main()
{
Point pt = { 2,3 };
Point* p = &pt;
printf("(%d, %d)\n", pt.x, pt.y);
printf("(%d, %d)\n", p->x, p->y);
}
#include <stdio.h>
struct Point
{
int x;
int y;
};
int main()
{
Point pt = { 2,3 };
Point* p = &pt;
printf("(%d, %d)\n", pt.x, (&pt)->y);
printf("(%d, %d)\n", (*p).x, p->y);
}
#include <stdio.h>
struct Point
{
int x;
int y;
};
void PrintPoint(Point pt)
{
printf("(%d, %d)\n", pt.x, pt.y);
}
int main()
{
Point pt = { 2,3 };
PrintPoint(pt);
}
#include <stdio.h>
struct Point
{
int x;
int y;
};
void PrintPoint(Point pt)
{
printf("(%d, %d)\n", pt.x, pt.y);
}
int main()
{
Point pt = { 2,3 };
Point pt2 = { -1, 3 };
PrintPoint(pt);
PrintPoint(pt2);
}
#include <stdio.h>
struct Point
{
int x;
int y;
};
void PrintPoint(Point* p)
{
printf("(%d, %d)\n", p->x, p->y);
}
int main()
{
Point pt = { 2,3 };
Point pt2 = { -1, 3 };
PrintPoint(&pt);
PrintPoint(&pt2);
}