申请的内存管理是可以被返回的
// ConsoleApplication9.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include #include #include #include using namespace std; struct Student { int number; //xuehao char name[1024]; double yuwen; double shuxue; double yingyu; }; class MyClass { public: MyClass(); ~MyClass(); private: }; MyClass::MyClass() { } MyClass::~MyClass() { } int *description; int *cinmyArray(int array[]) { cout << "请输入10个数字" << endl; description = (int *)malloc(10 * sizeof(int)); if (description == NULL) { fprintf(stderr, "Error - unable to allocate required memoryn"); } else { for (int i = 0; i < 10; i++) { cin >> description[i]; } } cout << "输入完毕" << endl; return description; } int *sortMyArray(int *description) { for (int i = 0; i<10; i++) { for (int j = i + 1; j<10; j++) { if (description[i]>description[j]) {//若前一个元素比后一个元素大,则借助临时变量temp交换(此排序为升序排序,若为降序排序则为arr[i] int temp = description[i]; description[i] = description[j]; description[j] = temp; } } } return description; } void adjust(int a[], int n) { for (int i = 0; i //找最大值 if (i % 2 == 0) { int j = i + 1; //记录最大值的位置 int loc = i; for (; j if (a[j]>a[loc])loc = j; } //发生交换 if (loc != i) { int temp = a[loc]; a[loc] = a[i]; a[i] = temp; } } //找最小值 else { int j = i + 1; //记录最小值的位置 int loc = i; for (; j if (a[j] int temp = a[loc]; a[loc] = a[i]; a[i] = temp; } } } } int *coutMyArray(int *description) { for (int i = 0; i < 10; i++) { cout << description[i] << endl; } return description; } int _tmain(int argc, _TCHAR* argv[]) { int myArray[10] = { 0 }; FILE*fp; cinmyArray(myArray); sortMyArray(description); coutMyArray(description); cout << "*************" << endl; adjust(description, 10); coutMyArray(description); if ((fp = fopen("stud", "w")) == NULL) { printf("error :cannot open file!n"); exit(0); } for (int i = 0; i<10; i++) fprintf(fp, "%dn", description[i]); fclose(fp); if (description != nullptr) { free(description); cout << "内存清理完毕" << endl; } return 0; }
上一篇 git的使用、ssh生成、github、Git分支操作
下一篇 微软发布 Win11 累积更新(KB5014019),版本号升至 22000.708
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号