尝试重新排列数组时 C++ 程序崩溃
C++ Program crashing when trying to rearrange array
我对 C++ 还是比较陌生。我正在尝试编写一个程序,该程序接受一个数字数组并使用一个函数反转数组中这些数字的顺序。程序如下:
#include <iostream>
using namespace std;
void reverse(int *array, int size);
int main() {
int Array[] = { 1, 2, 3, 4, 5 };
int size = sizeof(Array) / 4;
reverse(Array, size);
return 0;
}
void reverse(int *array, int size) {
int Array2[5];
for (int i = 0; i < size; i++) {
Array2[i + size] = array[i];
array[i + size] = Array2[i + size];
};
}
当我 运行 这个程序时,它崩溃了,我不确定为什么。如果有人可以帮助我弄清楚为什么会非常感激。谢谢你。
Array2[i + size]
无论 i
的值如何,您都在越界访问。
您的意思可能是 Array2[size - 1 - i]
向后迭代数组。 (size - 1
是最后一个元素的索引。)
当您说 int size = sizeof(Array) / 4;
时,size
现在是 (5 * sizeof(int)) / 4
。这就是 sizeof
运算符的工作方式(至少在应用于数组时)。
所以 size
可能是 5,假设一个 4 字节 int
。现在,你到达 reverse
并且它的参数 size
也是 5.
您进入 for
循环,甚至在第一次迭代时,您有 Array2[5] = /* something */
和 array[5] = /* something */
,两者都是缓冲区溢出。
此外,您的 reverse
函数实际上并没有进行任何反转。试试这个:
void reverse(int *arr, int size);
int main()
{
int Array[] = { 1, 2, 3, 4, 5 };
int size = sizeof(Array) / sizeof(int);
reverse(Array, size);
return 0;
}
void reverse(int *arr, int size)
{
int temp[5];
for (int i = 0; i < size; i++)
temp[size - 1 - i] = arr[i];
for (int i = 0; i < size; i++)
arr[i] = temp[i];
}
通过使用 swap,您将获得更好的解决方案,也更高效
void reverse(int *array, int size) {
for (int i = 0; i < size/2; i++) {
std::swap(array[i],array[size-1-i]);
};
}
Zenith 有,但有几点值得注意和快速技巧可以帮助您。
#include <iostream>
//using namespace std; don't need this, and using namespace std is overkill and often
// causes problems. It pulls in a lot of stuff that may conflict, case in point
// std::reverse now becomes reverse. Which reverse will you get? Your reverse or the standard
// library's reverse? Only pull in what you need, for example
using std::cout; // still not used, but makes a good example.
void reverse(int *array, int size)
{
// no need for the other array and another loop. You can swap each element for
//it's counterpart in the upper half of the array.
for (int i = 0; i < size /2 ; i++) // only need to go half way. Other half was
// already swapped doing the first half.
{
int temp = array[i]; // store a temporary copy of element i
array[i] = array[size-1-i]; // replace element i with it's counterpart
// from the second half of the array
array[size-1-i] = temp; // replace the counterpart of i with the copy of i
// or call std::swap(array[i], array[size-1-i]);
};
}
int main() {
int Array[] = { 1, 2, 3, 4, 5 };
// int size = sizeof(Array) / 4; using 4 here can trip you up on a computer with
// a different sized int
int size = sizeof(Array) / sizeof(Array[0]);
// dividing the size of the array by the size of an element in the array will always
// get you the correct size
reverse(Array, size);
return 0;
}
我对 C++ 还是比较陌生。我正在尝试编写一个程序,该程序接受一个数字数组并使用一个函数反转数组中这些数字的顺序。程序如下:
#include <iostream>
using namespace std;
void reverse(int *array, int size);
int main() {
int Array[] = { 1, 2, 3, 4, 5 };
int size = sizeof(Array) / 4;
reverse(Array, size);
return 0;
}
void reverse(int *array, int size) {
int Array2[5];
for (int i = 0; i < size; i++) {
Array2[i + size] = array[i];
array[i + size] = Array2[i + size];
};
}
当我 运行 这个程序时,它崩溃了,我不确定为什么。如果有人可以帮助我弄清楚为什么会非常感激。谢谢你。
Array2[i + size]
无论 i
的值如何,您都在越界访问。
您的意思可能是 Array2[size - 1 - i]
向后迭代数组。 (size - 1
是最后一个元素的索引。)
当您说 int size = sizeof(Array) / 4;
时,size
现在是 (5 * sizeof(int)) / 4
。这就是 sizeof
运算符的工作方式(至少在应用于数组时)。
所以 size
可能是 5,假设一个 4 字节 int
。现在,你到达 reverse
并且它的参数 size
也是 5.
您进入 for
循环,甚至在第一次迭代时,您有 Array2[5] = /* something */
和 array[5] = /* something */
,两者都是缓冲区溢出。
此外,您的 reverse
函数实际上并没有进行任何反转。试试这个:
void reverse(int *arr, int size);
int main()
{
int Array[] = { 1, 2, 3, 4, 5 };
int size = sizeof(Array) / sizeof(int);
reverse(Array, size);
return 0;
}
void reverse(int *arr, int size)
{
int temp[5];
for (int i = 0; i < size; i++)
temp[size - 1 - i] = arr[i];
for (int i = 0; i < size; i++)
arr[i] = temp[i];
}
通过使用 swap,您将获得更好的解决方案,也更高效
void reverse(int *array, int size) {
for (int i = 0; i < size/2; i++) {
std::swap(array[i],array[size-1-i]);
};
}
Zenith 有,但有几点值得注意和快速技巧可以帮助您。
#include <iostream>
//using namespace std; don't need this, and using namespace std is overkill and often
// causes problems. It pulls in a lot of stuff that may conflict, case in point
// std::reverse now becomes reverse. Which reverse will you get? Your reverse or the standard
// library's reverse? Only pull in what you need, for example
using std::cout; // still not used, but makes a good example.
void reverse(int *array, int size)
{
// no need for the other array and another loop. You can swap each element for
//it's counterpart in the upper half of the array.
for (int i = 0; i < size /2 ; i++) // only need to go half way. Other half was
// already swapped doing the first half.
{
int temp = array[i]; // store a temporary copy of element i
array[i] = array[size-1-i]; // replace element i with it's counterpart
// from the second half of the array
array[size-1-i] = temp; // replace the counterpart of i with the copy of i
// or call std::swap(array[i], array[size-1-i]);
};
}
int main() {
int Array[] = { 1, 2, 3, 4, 5 };
// int size = sizeof(Array) / 4; using 4 here can trip you up on a computer with
// a different sized int
int size = sizeof(Array) / sizeof(Array[0]);
// dividing the size of the array by the size of an element in the array will always
// get you the correct size
reverse(Array, size);
return 0;
}