-2

Apologies for any formatting errors this is my first post. I am not sure why this program will not work in Xcode. I receive an EXC_BAD_ACCESS error whenever i attempt to run it.

#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;


}

Here is there original assignment: There is a store to sale laptops. From Monday to Saturday, here are numbers of laptops they sold for each day: 10 8 14 16 18 37 The store manager wants you to write a program that display the day sales in descending order, as well as in their original order.

Requirements: Please define two arrays in your program Arr: an array of integers to hold the day sales in original order; PtrArr: an array of pointers that can be used to display the contents of Arr in descending order, without sorting Arr

Thank you in advance for any help. Any feedback is appreciated.

1
  • 2
    The right tool to solve such problems is your debugger. You should step through your code line-by-line before asking on Stack Overflow. For more help, please read How to debug small programs (by Eric Lippert). At a minimum, you should [edit] your question to include a Minimal, Complete, and Verifiable example that reproduces your problem, along with the observations you made in the debugger. Commented Sep 8, 2016 at 22:03

2 Answers 2

1

Basically, when a EXC_BAD_ACCESS exception is thrown it means that you are sending a message to an object that has already been released. Your code in a C++ compiler creates a segmentation fault. It is explained in more detail here:

When you send a message to an object, the pointer that points to the object you're sending the message to needs to be dereferenced. This means that you take the memory address the pointer is pointing to and access the value of that block of memory.

When that block of memory is no longer mapped for your application or, put differently, that block of memory isn't used for what you think it's used, it's no longer possible to access that chunk of memory. When this happens, the kernel sends an exception (EXC), indicating that your application cannot access that block of memory (BAD ACCESS).

When you are writing this in the first loop:

*Arrptr[i]=0;

you are assigning a value to a memory block that doesn't exist and you get the exception. Here you can understand further.

The following code works for me. I will leave up to you to optimize it if you need better performance and remove the first loop where I assign the integers to Arrptr. I have left it there for clarity. Hope this will help you:

#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;
}
Sign up to request clarification or add additional context in comments.

Comments

-1
#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;
}

1 Comment

Recommend explaining the how and why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.