1 / 6

11/2 , 11/4 실습 보충

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 ( &quot;%d <br>&quot; , sizeof (a)); //5 packed! printf ( &quot;%d <br>&quot; , sizeof (b)); //8 = int * m;

galia
Télécharger la présentation

11/2 , 11/4 실습 보충

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 11/2 , 11/4 실습 보충 Structure alignment malloc과 free()

  2. 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;

  3. 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의 변화는? }

  4. 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

  5. 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?

  6. 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)); } }

More Related