尝试创建指针数组时EXC_BAD_ACCESS

时间:2016-09-08 22:02:01

标签: c++ arrays sorting pointers

对于任何格式错误道歉这是我的第一篇文章。我不确定为什么这个程序在Xcode中不起作用。每当我尝试运行它时,都会收到EXC_BAD_ACCESS错误。

#include <iostream>

using namespace std;

int main ()
{
int MAX = 6;
int  Arr[6] = {10, 8, 14, 16, 18, 37};
int *Arrptr[6];
//Here i have created two arrays, the original and one for pointers
int i;
int g;
int k;

//Here i increment through the arrays to find the highest value in the original array
//once the highest value is found it assigns the value's address to a pointer in the second array.
for ( i = 0; i < MAX ; i++)
{
*Arrptr[i] = 0;
    for (g = 0; g < MAX; g++)
    {
        if (*Arrptr[i] > Arr[g])
        {
            Arrptr[i] = &Arr[g];
        }
    }
}
// Here i display the values in descending order by dereferincing the pointers in the pointer array.
cout << "Today's sales is descending order are!" << endl;
for (k = 0; k < MAX; k++ )
{
    cout << *Arrptr[k] <<endl;
}

    return 0;


}

这是原始作业: 有一个商店出售笔记本电脑。从周一到周六,这里是他们每天销售的笔记本电脑的数量: 10 8 14 16 18 37 商店经理希望您编写一个程序,按降序显示日销售,以及原始订单。

要求: 请在程序中定义两个数组 Arr:按原始顺序保存日销售的一系列整数; PtrArr:一个指针数组,可用于按降序显示Arr的内容,而不进行排序Arr

提前感谢您的帮助。任何反馈都表示赞赏。

2 个答案:

答案 0 :(得分:1)

基本上,当抛出EXC_BAD_ACCESS异常时,它意味着您正在向已释放的对象发送消息。您在C ++编译器中的代码会产生分段错误。我们会更详细地解释here

  

当您向对象发送消息时,指向您要发送消息的对象的指针需要被取消引用。这意味着您获取指针指向的内存地址并访问该内存块的值。

     

如果不再为您的应用程序映射该内存块,或者换句话说,该内存块不会用于您认为已使用的内存,则不再可能访问那块内存。发生这种情况时,内核会发送一个异常(EXC),表明您的应用程序无法访问该内存块(BAD ACCESS)。

当你在第一个循环中写这个时:

*Arrptr[i]=0;

您正在为不存在的内存块分配值,您将获得异常。 Here你可以进一步了解。

以下代码适用于我。如果您需要更好的性能,我将留下您优化它,并删除我将整数分配给Arrptr的第一个循环。为了清楚起见,我把它留在了那里。 希望这会对你有所帮助:

#include <iostream>
using namespace std;

int main ()
{
int MAX = 6;
int Arr[] = {10, 8, 14, 16, 18, 37};
int * Arrptr[MAX];

//Here i have created two arrays, the original and one for pointers


//For the sake of better distinction, first we assign the memory addresses of Arr to the array of pointers (note the &). These are not integers but memory locations. 
for (int i=0;i<MAX;i++){
   Arrptr[i]=&Arr[i];
}

//And now we can proceed to sort the pointer without modifying the original array. Obviously, this can be optimized and remove the previous loop for better performance.
for (int i=0;i<MAX;i++){
   for (int j=i+1;j<MAX;j++){
      if (*Arrptr[i]>*Arrptr[j]){
         int * temp=Arrptr[i];
         Arrptr[i]=Arrptr[j];
         Arrptr[j]=temp;
      }
   }
}

// Here i display the values in descending order by dereferincing the pointers in the pointer array. Note the inverse loop here, not in the above.
cout << "Today's sales is descending order are!" << endl;

for (int k = MAX-1; k > -1; k-- )
{
    cout << *Arrptr[k] <<endl;

}

//For debugging purposes, we can also show the original array
cout << "Today's sales is original order are!" << endl;

for (int k = 0; k < MAX; k++ )
{
    cout << Arr[k] <<endl;

}
    return 0;
}

答案 1 :(得分:-2)

#include <iostream>
#include <algorithm>

int main()
{
    constexpr int max{6};
    int  arr[max]{10, 8, 14, 16, 18, 37};
    int* arrptr[max];

    // set array of pointers to point to array members
    std::transform(std::begin(arr), std::end(arr), arrptr, [](auto& i) {return &i; });

    // sort pointers according to values pointed to
    std::sort(std::begin(arrptr), std::end(arrptr), [](auto i, auto j) {return *i > *j; });

    std::cout << "Today's sales in descending order are!\n";
    for(auto i : arrptr) {
        std::cout << *i << '\n';
    }

    std::cout << "Today's sales in original order are!\n";
    for(auto i : arr) {
        std::cout << i << '\n';
    }

    return 0;
}