Language/C
[따배씨] 14.4 구조체의 메모리 할당
migrationArc
2021. 6. 22. 23:00
따배씨 - 따라하며 배우는 C언어
14강 구조체_1
14.4 구조체의 메모리 할당 Memory Allocation
#include <stdio.h>
#include <stdlib.h>
int main()
{
/*
Well aligned structure
*/
struct Aligned
{
int a;
float b;
double c;
};
// structure 를 선언한는 것으로 메모리를 차지 하지 않음
/*
0 1 2 3 4 5 6 7|8 9 10 11 12 13 14 15|
|int a |float b|double c
4 + 4 + 8 = 16
*/
struct Aligned a1, a2;
// 구조체 변수들이 만들어 질 때, 메모리가 할당 됨
printf("struct Aligned a1\n");
// struct Aligned a1
printf("Sizeof %zd\n", sizeof(struct Aligned));
// Sizeof 16
printf("%lld\n", (long long)&a1);
// 140732920755328
printf("%lld\n", (long long)&a1.a);
// 140732920755328
printf("%lld\n", (long long)&a1.b);
// 140732920755332
printf("%lld\n", (long long)&a1.c);
// 140732920755336
printf("struct Aligned a2\n");
// struct Aligned a2
printf("Sizeof %zd\n", sizeof(a2));
// Sizeof 16
printf("%lld\n", (long long)&a2);
// 140732920755312
printf("%lld\n", (long long)&a2.a);
// 140732920755312
printf("%lld\n", (long long)&a2.b);
// 140732920755316
printf("%lld\n", (long long)&a2.c);
// 140732920755320
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
/*
padding (struct member alignment)
- 1 word: 4 bytes in x86, 8 byte in x64
메모리와 cpu가 데이터를 주고 받는 최소 단위
*/
struct Padded1
{
char a;
float b;
double c;
};
/*
Without padding
0 1 2 3 4 5 6 7|8 9 10 11 12 13 14 15|16
|a|float b|double c | ? ? ?
1 + 4 + 8 = 13
*/
/*
With padding
0 1 2 3 4 5 6 7|8 9 10 11 12 13 14 15|16
|char a |float b|double c |
4(char?) + 4 + 8 = 16
1 word 단위에 맞추어, compiler 가 3byte padding 을 넣어줌
*/
struct Padded1 p1;
printf("struct Padded p1\n");
// struct Padded p1
printf("Sizeof %zd\n", sizeof(p1));
// Sizeof 16
printf("%lld\n", (long long)&p1);
// 140732920755048
printf("%lld\n", (long long)&p1.a);
// // 140732920755048
printf("%lld\n", (long long)&p1.b);
// 140732920755052
printf("%lld\n", (long long)&p1.c);
// 140732920755056
struct Padded2
{
float a;
double b;
char c;
};
/*
|0 1 2 3 4 5 6 7|8 9 10 11 12 13 14 15|16 17 18 19 20 21 22 23|24
|float a |double b |char c |
8(float ?) + 8 + 8(char ?) = 24
compiler 가 double 8byte 자르지 않기 위하여 float 과 char 에 padding 을 넣어 줌
*/
struct Padded2 p2;
printf("struct Padded p2\n");
// struct Padded p2
printf("Sizeof %zd\n", sizeof(p2));
// Sizeof 24
printf("%lld\n", (long long)&p2);
// 140732920755024
printf("%lld\n", (long long)&p2.a);
// // 140732920755024
printf("%lld\n", (long long)&p2.b);
// 140732920755032
printf("%lld\n", (long long)&p2.c);
// 140732920755040
struct Padded3
{
char a;
double b;
double c;
};
/*
|0 1 2 3 4 5 6 7|8 9 10 11 12 13 14 15|16 17 18 19 20 21 22 23|24
| char a | double b | double c |
8(char ?) + 8 + 8 = 24
*/
struct Padded3 p3;
printf("struct Padded p3\n");
// struct Padded p3
printf("Sizeof %zd\n", sizeof(p3));
// Sizeof 24
printf("%lld\n", (long long)&p3);
// 140732920755000
printf("%lld\n", (long long)&p3.a);
// 140732920755000
printf("%lld\n", (long long)&p3.b);
// 140732920755008
printf("%lld\n", (long long)&p3.c);
// 140732920755016
struct Person
{
char name[41]; //member
int age; //member
float height; //member
};
struct Person mommy;
printf("struct Person\n");
// struct Person
printf("%lld\n", (long long)&mommy.name[0]);
// 140732920755280
printf("%lld\n", (long long)&mommy.age);
// 140732920755324
printf("%lld\n", (long long)&mommy.height);
// 140732920755328
printf("Sizeof %zd\n", sizeof(mommy)); // 41 + 4 + 4 = 49 ?
// Sizeof 52
struct Person f[4];
printf("Sizeof a structure array %zd\n", sizeof(f));
// Sizeof a structure array 208
/*
|f[0].name |f[0].age | f[0].height | ... |f[3].name |f[3].age |f[3].height |
*/
return 0;
}
이 글의 모든 사진과 내용의 출처는 홍정모 교수님께 있음을 알려드립니다.
http://blog.naver.com/atelierjpro
실리콘 밸리의 프로그래머 : 네이버 블로그
안녕하세요! 홍정모 블로그에 오신 것을 환영합니다. 주로 프로그래밍 관련 메모 용도로 사용합니다. 강의 수강하시는 분들은 홍정모 연구소 카페로 오세요.
blog.naver.com
http://www.inflearn.com/course/following-c
홍정모의 따라하며 배우는 C언어 - 인프런 | 강의
'따배씨++'의 성원에 힘입어 새롭게 개발된 C 언어로 시작하는 프로그래밍 입문 강의입니다. '따배씨'와 함께 프로그래밍 인생을 업그레이드 해보세요., 따라하며 배우는 C언어 '따배씨++'의 성원
www.inflearn.com