C++;尝试重新排列数组时程序崩溃 我对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]; }; } #包括 使用名称空间std; 无效反转(整数*数组,整数大小); int main(){ int数组[]={1,2,3,4,5}; int size=sizeof(数组)/4; 反向(数组、大小); 返回0; } 无效反向(整数*数组,整数大小){ 国际阵列2[5]; 对于(int i=0;i

C++;尝试重新排列数组时程序崩溃 我对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]; }; } #包括 使用名称空间std; 无效反转(整数*数组,整数大小); int main(){ int数组[]={1,2,3,4,5}; int size=sizeof(数组)/4; 反向(数组、大小); 返回0; } 无效反向(整数*数组,整数大小){ 国际阵列2[5]; 对于(int i=0;i,c++,arrays,pointers,crash,C++,Arrays,Pointers,Crash,当我运行这个程序时,它崩溃了,我不知道为什么。如果有人能帮我找出原因,我将不胜感激。多谢各位 Array2[i+大小] 无论i的值是多少,您都在访问越界 您可能是想Array2[size-1-i]反向迭代数组。(size-1是最后一个元素的索引。)Array2[i+size] 无论i的值是多少,您都在访问越界 您可能是想Array2[size-1-i]反向迭代数组。(size-1是最后一个元素的索引。)当你说int size=sizeof(Array)/4,大小现在是(5*sizeof(int)

当我运行这个程序时,它崩溃了,我不知道为什么。如果有人能帮我找出原因,我将不胜感激。多谢各位

Array2[i+大小]

无论
i
的值是多少,您都在访问越界


您可能是想
Array2[size-1-i]
反向迭代数组。(
size-1
是最后一个元素的索引。)

Array2[i+size]

无论
i
的值是多少,您都在访问越界


您可能是想
Array2[size-1-i]
反向迭代数组。(
size-1
是最后一个元素的索引。)

当你说
int size=sizeof(Array)/4
大小现在是
(5*sizeof(int))/4
。这就是
sizeof
操作符的工作原理(至少在应用于数组时是如此)

所以
size
可能是5,假设是4字节
int
。现在,您可以使用
reverse
,它的参数
size
也是5

进入
for
循环,即使在第一次迭代中,也会出现
Array2[5]=/*something*/
array[5]=/*something*/
,这两种情况都是缓冲区溢出

此外,您的
反转功能实际上不执行任何反转操作。试试这个:

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];
}
void reverse(int*arr,int size);
int main()
{
int数组[]={1,2,3,4,5};
int size=sizeof(数组)/sizeof(int);
反向(数组、大小);
返回0;
}
无效反向(整数*arr,整数大小)
{
内部温度[5];
对于(int i=0;i
当你说
int size=sizeof(Array)/4
大小现在是
(5*sizeof(int))/4
。这就是
sizeof
操作符的工作原理(至少在应用于数组时是如此)

所以
size
可能是5,假设是4字节
int
。现在,您可以使用
reverse
,它的参数
size
也是5

进入
for
循环,即使在第一次迭代中,也会出现
Array2[5]=/*something*/
array[5]=/*something*/
,这两种情况都是缓冲区溢出

此外,您的
反转功能实际上不执行任何反转操作。试试这个:

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];
}
void reverse(int*arr,int size);
int main()
{
int数组[]={1,2,3,4,5};
int size=sizeof(数组)/sizeof(int);
反向(数组、大小);
返回0;
}
无效反向(整数*arr,整数大小)
{
内部温度[5];
对于(int i=0;i
通过使用swap,您将得到一个更好、更高效的解决方案

void reverse(int *array, int size) {
   for (int i = 0; i < size/2; i++) {
        std::swap(array[i],array[size-1-i]);
   };
}
void reverse(int*数组,int大小){
对于(int i=0;i
通过使用swap,您将得到一个更好、更高效的解决方案

void reverse(int *array, int size) {
   for (int i = 0; i < size/2; i++) {
        std::swap(array[i],array[size-1-i]);
   };
}
void reverse(int*数组,int大小){
对于(int i=0;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;
}
#包括
//使用名称空间std;不需要这样做,而且使用名称空间std是过分的,而且常常是错误的
//造成问题。它吸引了很多可能发生冲突的东西,这是一个很好的例子
//反转现在变成反转。你会选哪一种反面?你的反面还是标准
//图书馆的反面?例如,只拉你需要的东西
使用std::cout;//仍然没有使用,但这是一个很好的例子。
无效反向(整数*数组,整数大小)
{
//不需要另一个数组和另一个循环。您可以将每个元素交换为
//它是数组上半部分的对应项。
对于(inti=0;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;
}
#包括
//使用名称空间std;不需要这样做,而且使用名称空间std是过分的,而且常常是错误的
//造成问题。它吸引了很多可能发生冲突的东西,这是一个很好的例子
//反转现在变成反转。你会选哪一种反面?你的反面还是标准
//图书馆的反面?例如,只拉你需要的东西
使用std::cout;//仍然没有使用,但这是一个很好的例子。
无效反向(int*数组,int s