1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Dynamic array bug

  1. May 25, 2010 #1
    this function is supposed to find the first item in an array that is less than the element preceding it, and set the p parameter to point to that item, so the caller can know the location of that item. when i run it.. it fails. there are probably multiple bugs.. any suggestions on how to fix? thanks

    Code (Text):
      void findDisorder(int arr[], int n, int* p)
        {
            for (int k = 1; k < n; k++)
            {
                if (arr[k] < arr[k-1])
                {
                     p = arr + k;
                     return;
                }
            }
        p = NULL;
        }      
           
        int main()
        {
            int nums[6] = { 10, 20, 20, 40, 30, 50 };
            int* ptr;

            findDisorder(nums, 6, ptr);
        if (ptr == NULL)
            cout << "The array is ordered" << endl;
        else
        {
                cout << "The disorder is at address " << ptr << endl;
                cout << "It's at index " << ptr - nums << endl;
                cout << "The item's value is " << *ptr << endl;
        }
        }
     
  2. jcsd
  3. May 25, 2010 #2

    Mark44

    Staff: Mentor

    What do you mean when you say it fails? Have you learned how to use a debugger? That can show you what is happening at each step in your function, as well as what happens after your function returns to main.
     
  4. May 25, 2010 #3
    the program itself is able to open up, but immediately upon opening i get a runtime error that ptr is not initialized.. but the problems are in the void function, not in the main so i'm not sure what's wrong in the void part
     
  5. May 25, 2010 #4

    Mark44

    Staff: Mentor

    The first problem is easy to fix - just initialize ptr to something (nums would be appropriate).

    Your second problem is that C++ is strictly a call-by-value language. You can simulate call-by-reference by passing a pointer. This means that if a function has a pointer parameter, the address that is passed in as an argument to the function cannot change. What can change is what is at that address.

    What you're trying to do is pass in one address (the address of your nums array) and have p get changed inside the function. That does happen, but it has no effect on the actual address that was passed in.

    There are two solutions: pass in the pointer exactly as before but return the address where the disordered value was found (or NULL if the array was ordered); make the pointer parameter a pointer to a pointer (int ** p).

    If you want a pointer argument to The p parameter in
     
  6. May 25, 2010 #5
    i'm not sure what you mean by returning the address where the disordered value was found. does that mean p = &k and not arr + k?
     
  7. May 25, 2010 #6

    Mark44

    Staff: Mentor

    What I mean is that you can do either of the following:
    1. Keep the parameters to your function exactly the same but have the function return a pointer --
    Code (Text):
    int * findDisorder(int arr[], int n, int* p)
    2. Keep the function a void function, but change the third parameter to int ** p.
     
  8. May 25, 2010 #7
    i tried the second method you suggested and made int ** p and changed nothing else so my code for the void function was

    Code (Text):
     void findDisorder(int arr[], int n, int** p)
        {
            for (int k = 1; k < n; k++)
            {
                if (arr[k] < arr[k-1])
                {
                     p = arr + k;
                     return;
                }
            }
        p = NULL;
        }      
     
    but i am still getting a runtime error that ptr is not initialized...i'm not supposed to change anything in the main function so is there a way to fix it in the void??
     
  9. May 25, 2010 #8

    Mark44

    Staff: Mentor

    In main, I have
    int nums[6] = { 10, 20, 20, 40, 30, 50 };
    int * ptr = nums;

    As far as not changing anything in main, this is new information. I don't see any way around not making a change in main since you're getting a run-time error the way it currently is, and no change to findDisorder will cause that error to go away.

    The change I made by initializing ptr is pretty innocuous, so a reasonable prof shouldn't have any problem with a change like this. You should consult with him/her on that point.

    In the first version of findDisorder, the parameter p is a pointer to an int, and the two instances of p in the body of this function are as p.

    In the new version of this function, the parameter p is a pointer to a pointer to an int, so the two instances of p should be as .... ? Those are the only changes you need to make to findDisorder. (Be advised that I didn't thoroughly test your code, so you might have some edge conditions that could cause problems.)
     
  10. May 25, 2010 #9
    this is my code in its entirety (without having changed the main routine)

    Code (Text):
    #include <iostream>
    #include <cctype>
    #include <cstring>

    using namespace std;

    void findDisorder(int arr[], int n, int** p)
        {
            for (int k = 1; k < n; k++)
            {
                if (arr[k] < arr[k-1])
                {
                     p = arr + k;
                     return;
                }
            }
        p = NULL;
        }      
           
        int main()
        {
            int nums[6] = { 10, 20, 20, 40, 30, 50 };
            int* ptr;

            findDisorder(nums, 6, ptr);
        if (ptr == NULL)
            cout << "The array is ordered" << endl;
        else
        {
                cout << "The disorder is at address " << ptr << endl;
                cout << "It's at index " << ptr - nums << endl;
                cout << "The item's value is " << *ptr << endl;
        }
        }
    now it doesn't compile but instead gives me the error that it cannot convert int * to int **
     
  11. May 25, 2010 #10

    Mark44

    Staff: Mentor

    Pass in &ptr - that's the address of a pointer to an int; i.e. a pointer to a pointer to int. Once you fix this, though, you might still get the run-time error about a pointer not being initialized.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Dynamic array bug
  1. Dynamics ? (Replies: 2)

  2. C - array (Replies: 2)

  3. Function and array (Replies: 26)

Loading...