求助:C++程序中动态调整整型数组大小遇到问题
Hey there! Let's work through your dynamic array resizing issue. I've reviewed your code snippet, and the most likely culprit is missing or incorrect implementations of your reSIZE and sortFUNC functions—those are critical for making dynamic arrays work properly. Let's break this down step by step.
First, Let's Clarify Your Existing Code
Here's your code formatted with proper structure (I filled in the missing bits that were cut off):
#include <iostream> #include <string> #include <cmath> #include <iomanip> #include <cstdlib> using namespace std; int *reSIZE(int *&original, int &SIZE, const int &maxSIZE); //resize函数原型 void sortFUNC(int *&original, int &SIZE, const int &maxSIZE); //sortFUNC函数原型 int main() { int SIZE = 4; //当前数组的大小 int maxSIZE = 10; //最终数组的大小 int *original = new int[SIZE]; // Initialize original array // Example: Fill original array with random values for (int i = 0; i < SIZE; ++i) { original[i] = rand() % 100; } // Resize the array to maxSIZE original = reSIZE(original, SIZE, maxSIZE); // Sort the resized array sortFUNC(original, SIZE, maxSIZE); // Print the sorted array to verify cout << "Sorted resized array:" << endl; for (int i = 0; i < SIZE; ++i) { cout << original[i] << " "; } cout << endl; // Don't forget to free allocated memory! delete[] original; return 0; }
Common Issues with Dynamic Resizing (and Fixes)
The reSIZE function is where most bugs happen. Here's what a correct implementation looks like, with explanations:
int *reSIZE(int *&original, int &SIZE, const int &maxSIZE) { // Handle edge case: if new size is smaller or equal to current size if (maxSIZE <= SIZE) { SIZE = maxSIZE; // Update size if we're truncating return original; } // Allocate a new array with the desired max size int *newArray = new int[maxSIZE]; // Copy all elements from the original array to the new one for (int i = 0; i < SIZE; ++i) { newArray[i] = original[i]; } // Free the original array's memory to avoid leaks delete[] original; // Update the current size to the new max size SIZE = maxSIZE; // Return the pointer to the new array return newArray; }
Key Points to Note:
- Memory Leak Prevention: Always
delete[]the original array before replacing it—if you skip this, the old memory will never be freed, leading to memory leaks. - Element Preservation: The loop copies every element from the original array to the new one, so you don't lose data during resizing.
- Size Update: We set
SIZE = maxSIZEso subsequent operations (like sorting) use the correct array length. - Edge Case Handling: We added a check for when the new size is smaller than the current one, which lets you truncate the array safely if needed.
Fixing the Sort Function
Your sortFUNC should operate on the updated array size. Here's a simple bubble sort implementation that works with your resized array:
void sortFUNC(int *&original, int &SIZE, const int &maxSIZE) { // Sort only the valid elements (using the updated SIZE variable) for (int i = 0; i < SIZE - 1; ++i) { for (int j = 0; j < SIZE - i - 1; ++j) { if (original[j] > original[j + 1]) { // Swap elements int temp = original[j]; original[j] = original[j + 1]; original[j + 1] = temp; } } } }
Final Pro Tip
If you're using C++11 or later, consider using std::vector<int> instead of raw pointers—it handles dynamic resizing automatically and eliminates manual memory management errors entirely!
内容的提问来源于stack exchange,提问作者boypetey18

