Динамическое распределение памяти — страница 4
- Категория Компьютеры и устройства
- Раздел Курсовые
- Просмотров 3760
- Скачиваний 197
- Размер файла 17 Кб
*)malloc(sizeof(char)) )) { printf ("Не хватает памяти\n"); exit(1); } } mas[m][n]=c; //Занесение цифры на нужную позицию n++; //в число if (n) //Выделение памяти под следующую { //позицию в числе if (!(mas[m]=(char *)realloc(mas[m],sizeof(char)*(n+1)))) { printf ("Не хватает памяти\n"); exit(1); } } putch (c); //Вывод цифры на экран } printf ("Output\n"); for (int i=0;i<m;i++) printf ("%s\n",mas[i]); //Вывод всех чисел на экран for (i=0;i<m;i++) if (mas[i]) free(mas[i]); //Освобождение памяти if (mas) free(mas); return 0; } Задание №12 #include <stdio.h> #include <conio.h> #include <alloc.h> #include <process.h> struct Matr{ int m,n; double **ptr; void SetRazm(int mm,int nn) { m=mm; n=nn; } }; int DinMatr (Matr *matr); //функция выделения памяти для Matr void FreeMatr(Matr *matr); //функция освобождения памяти из под Matr void Setelem(Matr *matr,double M[3][3]); //функция заполнения матрицы элементами double OctNorm(Matr *matr); //функция вычисления нормы матрицы main() { clrscr(); double M_[3][3]={{1,2,3},{4,5,6},{7,8,9}}; Matr M; M.SetRazm(3,3); if (!DinMatr(&M)) { printf ("Не хватает памяти для матрицы\n"); exit(1); } Setelem(&M,M_); printf ("%f\n",OctNorm(&M)); FreeMatr(&M); return 0; } int DinMatr (Matr *matr) { if (!(matr->ptr=(double **)malloc(sizeof(double *)*(matr->m)))) return 0; for (int i=0;i<matr->m;i++) { if (!(matr->ptr[i]=(double *)malloc(sizeof(double)*(matr->n)))) return 0; } return 1; } void FreeMatr(Matr *matr) { for (int i=0;i<matr->m;i++) { if (matr->ptr[i]) free(matr->ptr[i]); } if (matr->ptr) free(matr->ptr); } void Setelem(Matr *matr,double M[3][3]) { for (int i=0;i<matr->m;i++) { for (int j=0;j<matr->n;j++) (matr->ptr[i][j])=M[i][j]; } } double OctNorm(Matr *matr) { double max=0; double a=0; for (int i=0;i<matr->m;i++) { max+=matr->ptr[i][0]; } for (int j=0;j<matr->n;j++) { for (i=0;i<matr->m;i++) { a+=matr->ptr[i][j]; } if (a>max) max=a; a=0; } return max; } Задание №14 #include <stdio.h> #include <alloc.h> #include <conio.h> #include <process.h> void main(void) { long N=1; char *A; A=(char *)calloc(N,1024); //Выделение в куче места do { free(A); //Освобождение массива A=(char *)calloc(N,1024); //Выделение памяти под больший массив N++; //Увеличение счетчика } while(A!=NULL); //Продолжать пока память выделяется printf("\nMaximum size of heap N=%iKb",N);//Вывод результатов } Задание №16 #include <stdio.h> #include <conio.h> #include <alloc.h> #include <process.h> #include <stdlib.h> struct MATR { int n,m; double **ptr; int read_(char name[80]) { FILE *pf; int i=0,j=0; char c; char num[10]; int pos=0,flag=1; m=0; n=0; if (!(pf=fopen(name,"rt"))) return 0; ptr=(double **)malloc(sizeof(double *)); ptr[0]=(double *)malloc(sizeof(double)); while ((c=fgetc(pf))!=EOF) { if (((c>='0')&&(c<='9'))||(c=='.')) { num[pos]=c; pos++; flag=1; } if ((c==' ')&&(flag)) { flag=0; num[pos]=0; ptr[i][j]=atof(num); j++; ptr[i]=(double *)realloc(ptr[i],sizeof(double)*(j+1)); pos=0; } if ((c=='\n')&&(flag)) { flag=0; num[pos]=0; ptr[i][j]=atof(num); i++; ptr=(double **)realloc(ptr,sizeof(double *)*(i+1)); ptr[i]=(double *)malloc(sizeof(double)); j=0; pos=0; } if (i>n) n=i; if (j>m) m=j; } n--; fclose (pf); return 1; } void free_() { for(int i=0;i<=n;i++) free(ptr[i]); free (ptr); } void print_() { for (int i=0;i<=n;i++) { for (int j=0;j<=m;j++) { printf ("%8.3f ",ptr[i][j]); } printf ("\n"); } } int write_(char name[80]) { FILE *pf; if (!(pf=fopen(name,"wt"))) return 0; for (int i=0;i<=n;i++) { for (int j=0;j<=m;j++) { fprintf (pf,"%f ",ptr[i][j]); } fprintf (pf,"\n"); } fclose (pf); } }; void main() { clrscr(); MATR A; A.read_("C:\\mas.txt"); A.print_(); A.write_("C:\\out.txt"); A.free_(); } Библиографический список 1. Трофимов С.П. Программирование в Си. Динамическое распределение памяти:
Похожие работы
- Доклады
- Рефераты
- Рефераты
- Рефераты
- Контрольные