60 likes | 200 Views
11/2 , 11/4 실습 보충. Structure alignment malloc 과 free (). Code example #6 : structure alignment. int main( int argc , char * args []) { A a ; B b ; C c ; D d ; printf ( "%d <br>" , sizeof (a)); //5 packed! printf ( "%d <br>" , sizeof (b)); //8 = int * m;
E N D
11/2 , 11/4 실습 보충 Structure alignment malloc과 free()
Codeexample #6 : structure alignment int main(intargc, char *args[]) { A a; B b; C c; D d; printf("%d \n", sizeof(a)); //5 packed! printf("%d \n", sizeof(b)); //8 = int * m; printf("%d \n", sizeof(c)); //4 = short * m; printf("%d \n", sizeof(d)); //16 = double * m // total size는 multiple of k, 즉, k * n으로 경계를 잡아 맞춥니다. // n는구조체member 개수가아니에요.! // k= 1, 2, 4, 8 .. 구조체 멤버 중 가장 큰 멤버기준. // 결국 worst case로 k=1일 때, total size는 2(n-1) // 대단위 프로젝트 시특히 라이브러리의 linking과정에서memory performance를 위해 // n-bytes alignment 을 맞추는 것이 좋습니다. } • typedefstruct • { • char c1; • char c2; • char c3; • } E; typedefstruct__attribute__((packed)) { inti; charc; } A; typedefstruct { int i; charc; } B; typedefstruct { shorti; charc; } C; typedefstruct { doubled; charc; }D;
Codeexample #7 : malloc(), realloc(), free(); #define size 10 intmain() { char*ps = (char *)malloc (sizeof(char)* size); printf("할당된 size : %d", _msize(ps)); memset(ps, 0, sizeof(char)* size); //초기화 scanf("%s", ps); // 10자 이내로 입력 for(i=0; i<_msize(ps); i++) printf("ps[%d] : %c \n",i, ps[i] ); //입력된데이터 출력해보고. ps=(char *)realloc(ps, sizeof(char)* size*2); // 할당된 size 변경 printf("변경된 size : %d/", _msize(ps)); for(i=0; i<_msize(ps); i++) printf("ps[%d] : %c \n",i, ps[i] ); //사이즈 변경 후, 데이터 출력해보기 free(ps); //free 이후의 ps와 *ps의 변화는? }
void * 포인터 • “a” 또는 • ”abc”.. str ? pstr • “a” 또는 • ”abc”.. 또는 • 123 • …. • char pstr 10 • char • char *str vs. void *pstr • Code 예) • str= (char *)malloc(sizeof(char) * 10); • 반면, • pstr = (char **)malloc(sizeof(char *) * 10); 도 가능 • 위 상태에서 • str과pstr을 다시 그림으로 나타내면? … • char
Codeexample #8 : void* • vp=(int *)a; • printf("%d \n", *(int *)(vp)); • printf("%d \n", *(int *)(vp+sizeof(int))); • printf("%d \n", *(int *)(vp+sizeof(int)*2)); vp =(char *)str[0]; printf("%s \n", (char *)(vp)); return 0; } #include<stdio.h> int main(void) { int a[] = { 10, 20, 30, 40, 50 }; char *str[] = {“abcd”,”efghhi!!!”}; void *vp; vp = a; // 가능 vp = &a[2]; // 가능 //출력문을 넣어보세요. *vp = 35;// 오류, why? vp++;// 오류, why? *(int *)vp = 35; // 가능, why?
Codeexample #9 : void* int main() { void *pp; char *fruit[]={"apple","orange","berry","watermelon","banana"}; inti=0; //그림으로 그려본 후코드를 보면 굉장히 수월할 겁니다. printf(" %p - %s \n", *(fruit+3), *(fruit+3)); pp = (char **)malloc(sizeof(char *) * sizeof(fruit)/sizeof(char*)); printf("&pp : %p pp : %p \n", &pp, pp); //&pp는 stack, pp의 값은 heap주소 for (i=0; i<sizeof(fruit)/sizeof(char *); i++){ *((char **)pp+i) = *(fruit + i); printf(" pp+[%d] %p %p \n", i, ((char **)pp+i), *((char **)pp+i)); } printf(" %p - %s \n", *(fruit+3), *(fruit+3)); for (i=0; i<sizeof(fruit)/sizeof(char *); i++){ *((char **)pp+i) = *(fruit + i); printf(" fruits [%d]: %s \n", i, *((char **)pp+i)); } }