简体   繁体   English

指向整数数组的指针的 C++ 程序行为

[英]C++ program behavior on pointers to integer array

Can anyone please explain why the loop only execute once!!谁能解释一下为什么循环只执行一次!!

The for loop executes once and never reaches end of the programm and if you've got some time to review my mistakes then please point out mistakes because i know this is not how it is done!! for 循环执行一次,永远不会到达程序的结尾,如果您有时间检查我的错误,请指出错误,因为我知道这不是这样做的!!

using namespace std;
#include<iostream>

int* rotate(int* ar,int d,int n)
{
    int tmp[n];
    d = d%n;

    for (int i = 0; i < n; ++i)
    {
        tmp[i] = ar[(i+d)%n];
    }
    free(ar);
    return tmp;
}


int main(int argc, char const *argv[])
{
    int arr[] = {1,2,3,4,5};
    int *a;
    int n = sizeof(arr)/sizeof(arr[0]);
    a = rotate(arr,4,n);
    cout<<endl;

    for (int i = 0; i < n; ++i)
    {
         cout<<i<<endl;
         cout<<a[i]<<endl;
    }
    cout<<"End";
    return 0;
}

Lets take it apart...让我们把它拆开...

int* rotate(int* ar,int d,int n)
{
    int tmp[n];                     // 1
    d = d%n;
    for (int i = 0; i < n; ++i)
    {
        tmp[i] = ar[(i+d)%n];
    }
    free(ar);                       // 2
    return tmp;                     // 3
}

int tmp[n]; is not standard C++ . 不是标准的 C++ If you do not want to use std::vector the proper replacement would be a dynamically allocated array.如果您不想使用std::vector则正确的替换将是动态分配的数组。

You call free with a pointer that was not allocated via malloc , which invokes undefined behavior.您使用未通过malloc分配的指针调用free ,这会调用未定义的行为。 In C++ you shouldn't be using free and malloc at all, but rather new and delete .在 C++ 中,您根本不应该使用freemalloc ,而应该使用newdelete And also new and delete only for such exercise.还有newdelete仅用于此类练习。 Otherwise use smart pointers.否则使用智能指针。

You return a pointer to a local variable .返回一个指向局部变量的指针 The pointer is dangling and using it in main invokes undefined behavior.指针悬空,在 main 中使用它会调用未定义的行为。

You can't simply replace the static array arr in main with something else in the function.您不能简单地将main的静态数组arr替换为函数中的其他内容。 A function called rotate is not expected to create a new array or delete the one that was passed (also because like in your case it is just not possible).不应期望名为rotate的函数创建新数组或删除传递的数组(也因为就像您的情况一样,这是不可能的)。

There are different ways to fix your code.有多种方法可以修复您的代码。 I choose to make rotate rotate the array "in-place".我选择使rotate “就地”旋转阵列。 However, as you can see, the implementation actually uses an additional array of same size.但是,正如您所看到的,该实现实际上使用了一个相同大小的附加数组。 I leave it to you to figure out how to change it to use less additional memory:我把它留给你来弄清楚如何改变它以使用更少的额外内存:

#include <iostream>
using std::cout;
using std::endl;

void rotate(int* ar,int d,int n)
{
    int* tmp = new int[n];
    d = d%n;
    for (int i = 0; i < n; ++i)
    {
        tmp[i] = ar[(i+d)%n];
    }
    for (int i = 0; i < n; ++i)
    {
        ar[i] = tmp[i];
    }                       
}


int main(int argc, char const *argv[])
{
    int arr[] = {1,2,3,4,5};
    int n = sizeof(arr)/sizeof(arr[0]);
    rotate(arr,4,n);
    cout<<endl;

    for (int i = 0; i < n; ++i)
    {
         cout<<i<<endl;
         cout<<arr[i]<<endl;
    }
    cout<<"End";
    return 0;
}

This is how you can do the same using std::rotate :这是使用std::rotate执行相同操作的方法:

#include <array>
#include <iostream>
#include <algorithm>

int main(int argc, char const *argv[])
{
    int arr[] = {1,2,3,4,5};
    int n = sizeof(arr)/sizeof(arr[0]);
    std::rotate(std::begin(arr),std::begin(arr)+4,std::end(arr));

    for (int i = 0; i < n; ++i)
    {
         std::cout << i << std::endl;
         std::cout << arr[i] << std::endl;
    }
    std::cout<<"End";
}

... it even uses pointers ;) ...它甚至使用指针;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM