Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming: review on C: Pointer and malloc

Similar presentations


Presentation on theme: "C++ Programming: review on C: Pointer and malloc"— Presentation transcript:

1 C++ Programming: review on C: Pointer and malloc
2018, Spring Pusan National University Ki-Joune Li

2 Pointer 와 Address Address 또는 Pointer Pointer Variable
Main Memory의 위치를 지정 각 Variable의 값을 저장하는 위치 &n : m이 저장된 위치 (0x20AC) Pointer Variable Address를 값으로 하는 변수 Main memory int n,m; n=20; m=30; 0x20AC n = 20 pn = 20AC int n,*pn; pn=&n; *pn=30;

3 Pointer와 Function Parameter
Call-By Value를 통한 Function내의 값 변경 int m,n; . . . m=20; m=50; swap(&m,&n); void swap(int *a, int *b) { int m; m=*a; *a=*b; *b=m; } int m,n; . . . m=20; m=50; swap(m,n); void swap(int a, int b) { int m; m=a; a=b; b=m; }

4 Pointer와 Array Pointer 와 Array의 유사성과 차이점 Main memory int values[100];
values: values array의 처음 주소 int *pvalues; pvalues=values;  pvalues[0]와 values[0]은 서로 호환 가능 차이점: values는 상수 0x20AC=values=&values[0] values[0] values[99]

5 Pointer와 Array Pointer와 Array Index
int *pvalues; pvalues=values; /* pvalues[0]==values[0] */ ++pvalues; /* *pvalues==values[1] */ Example int sum=0; int sum=0; int i, values[10]; int i,values[10],*pvalues; /* ... value[0], ... value[9] */ pvalues=values; for(i=0;i<10;i++) for(i=0;i<10;i++) sum=sum+values[i]; sum=sum+*values++; */ int sum=0; int sum=0; int i, values[10]; int i,values[10],*pvalues; pvalues=values; for(i=0;i<10;i++) sum=sum+pvalues[i];

6 Array Arithmetic Increment and Decrement
pvalues가 0x1000일 때 pvalues++ 한 후 pvalues != 0x1000 pvalues == 0x sizeof(type of pvalues, int의 경우 4) Multiplication과 Division은 Pointer에 대하여 사용 불가능 int *pvalue; int value; pvalue=&value; pvalue=pvalue*2; /* Error */ pvalue=pvalue+2; /* OK */

7 Character Pointer Character Pointer Exchangeable with Character Array
Example char *string1=“Hello World\n”; char string2[]=“Hello World\n”; void strcpy(char *s, char *d) { while((*s++=*d++)!=‘\0’); } void strcpy(char s[], char d[]) { int i=0; while((s[i]=d[i++])!=‘\0’); }

8 Character Pointer Pointer to String and Double Pointer
Example char *stringArray[2]; char **pstr; stringArray[0]="Hello first\n"; stringArray[1]="Hello second\n"; pstr=stringArray; printf("%s",*pstr++); printf("%s",*pstr); Parameters of main function void main(int argc, char *argv[]) pstr stringArray[0] “Hello first” stringArray[1] “Hello second”

9 Pointer Array and Multidimensional Array
Pointer Array: Array of Pointer Example int values[10][20]; int *pvalues[10]; pvalues[0]=values[0]; pvalues[0]++; /* ? */ pvalues[0][1]; /* ? */ Main memory values =0x1000 values[0] = 0x1000 values[0][0] values[0][0] values[0][1] values[1] =0x1050 values[1][0] values[9][19]

10 Double Pointer Example If single pointer would be used?
#include <stdlib.h> void main() { char *string = "Hello, world!"; char *copystr; if(allocstr(strlen(string), &copystr))strcpy(copystr, string); else fprintf(stderr, "out of memory\n"); } int allocstr(int len, char **retptr) { char *p = malloc(len + 1); /* +1 for \0 */ if(p == NULL) return 0; *retptr = p; return 1; } If single pointer would be used?

11 malloc Dynamic memory allocation (in run time) typedef struct {
int year; char name[20]; float score; } Student; int main(int argc,char **argv){ Student Astudents[100]; Student *Bstudents; Bstudents=(Student *)malloc(sizeof(Student)*100); . . . }

12 malloc and function parameter passing
int main(int argc,char **argv){ Student *students; ... InitializeStudents(student, 100); ... } void InitializeStudents(Student *ptr, int n){ int i; ptr=(Student *)malloc(sizeof(Student)*100); for(i=0;i<n;i++) ptr++.year=0; }

13 malloc and function parameter passing
Call-by value students=cccc ptr=cccc  8000 ? 8000 *student Memory allocated by malloc *(student+1)

14 malloc and function parameter passing - solution
int main(int argc,char **argv){ Student *students; ... InitializeStudents(&student, 100); ... } void InitializeStudents(Student **ptr, int n){ int i; *ptr=(Student *)malloc(sizeof(Student)*100); for(i=0;i<n;i++) ptr++.year=0; }

15 malloc and function parameter passing - solution
Call-by value 4000 students=cccc ptr=4000 ? *ptr=malloc(…) 8000 *student *(student+1)


Download ppt "C++ Programming: review on C: Pointer and malloc"

Similar presentations


Ads by Google