{ char *leakage1; leakage1 = (char *) malloc(5*sizeof(char)); }
char *leakage2; leakage2 = (char *) malloc(5*sizeof(char)); ... leakage2 = "hello";
char *msg; printf("%s\n", msg);
char *buffer; strcpy(buffer, "some data"); scanf("%s",buffer);
char *buf; buf = (char *) malloc(5*sizeof(char)); strcpy(buf,"abcde");
int data[1000], i; for (i=0; i<=1000; i++) data[i] = i;
char *buf; buf = (char *) malloc(5*sizeof(char)); ... *(buf - 2) = '\0';
char buf[5]; ... *(buf - 2) = 'a'
char c; short int x; scanf("%d%d", &x, &c);
char *buf; buf = (char *) malloc(5*sizeof(char)); free(buf); ... free(buf);
char *buf, *ptr, array[100]; buf = (char *) malloc(5*sizeof(char)); ptr = buf + 1; free(ptr); ... ptr = array; free(ptr); ... ptr = 0; free(ptr);
char buf1[5], buf2[5]; buf1[0] = 'a'; buf1[1] = 'b'; strcpy(buf2, buf1); ... printf("%s\n",buf1);
char *buf; buf = (char *) malloc(5*sizeof(char)); ... free(buf); strcpy(buf, "memory bomb");
char *fun() { char *ptr, buf[10]; ... ptr = buf; return ptr; } char *dataPtr, buf[20]; dataPtr = func(); strcpy(buf, dataPtr); ... strcpy(dataPtr, buf);
... free(ptr); ptr = 0; ...
int (*ptr)[10], buf[20][10]; ptr = buf; *(int *)(ptr + 200*4) = 20; // should be ptr[19][9] = 20; // or *((int *)(ptr + 19) + 9) = 20; // or *((int *)ptr + 199) = 20;
void bizarrePrint(int n, int buf[]) { int localBuf[1000]; int i, pivot; if (n == 1) { printDigit(n, buf); return; } else { for (i=0; i<5; i++) { pivot = n*i/5; copyDigit(localbuf, n/5, &buf[pivot]); bizarrePrint(n-1, localbuf); } } } int i; int buf[2000]; for (i=0; i<2000; i++) buf[i] = i; bizarrePrint(2000,buf);
void func() { double image[2000][2000]; ... }
int i, *ptr; int n = 25000; ptr = (int *) malloc(n*sizeof(int)); for (i=0; i<n; i++) ptr[i] = i;
class Parent1 { int x; }; class Parent2 { int x; }; class Child : public Parent1, public Parent2 { int x; }; void main() { Child *objPtr = new Child; Parent1 *ptr1 = objPtr; ... Parent2 *ptr2 = objPtr; delete ptr2; // run time error }
int *ptr1 = new int; delete[] ptr1; int *ptr2 = new int[100]; delete ptr2;
回
C++ 程式設計課程
首頁
製作日期: 4/02/2002
by 丁培毅 (Pei-yih Ting)
E-mail: [email protected]
TEL: 02 24622192x6615
海洋大學
理工學院
資訊科學系