Fix Dynamic Array Bug: Find the First Item Less than Preceding

Click For Summary

Discussion Overview

The discussion revolves around a bug in a C++ function designed to find the first item in an array that is less than the preceding element. Participants explore issues related to pointer manipulation and function parameter passing, focusing on debugging and potential fixes. The scope includes technical explanations and programming challenges.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes the intended functionality of the function and notes that it fails, suggesting there may be multiple bugs.
  • Another participant suggests using a debugger to identify issues within the function.
  • A participant points out that the program encounters a runtime error due to an uninitialized pointer, indicating that the problem lies within the function rather than the main routine.
  • One participant explains that C++ uses call-by-value, which means changes to the pointer inside the function do not affect the original pointer passed in.
  • Several participants propose two solutions: returning the address of the disordered value or changing the pointer parameter to a pointer to a pointer.
  • A participant expresses confusion about the suggestion to return the address of the disordered value, questioning if it means using a different assignment.
  • Another participant clarifies the two proposed solutions for modifying the function's parameters.
  • A participant attempts to implement the second solution but reports continued runtime errors, indicating that changes in the function do not resolve the issue.
  • One participant suggests initializing the pointer in the main function to avoid runtime errors, but acknowledges that this may not align with the original constraints of the task.
  • A participant shares their complete code and notes compilation errors related to type conversion between pointers.
  • Another participant advises passing the address of the pointer to resolve the type conversion issue but warns that other runtime errors may persist.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the issues related to pointer handling and function parameters, but there is no consensus on the best approach to fix the function without altering the main routine. Multiple competing views on how to resolve the pointer issue remain present.

Contextual Notes

Participants note that the original function's design may not be compatible with the intended use of pointers, leading to confusion and runtime errors. The discussion highlights the limitations of pointer manipulation in C++ and the implications of call-by-value versus call-by-reference.

magnifik
Messages
350
Reaction score
0
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:
  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;
	}
    }
 
Physics news on Phys.org
magnifik said:
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:
  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;
	}
    }

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.
 
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
 
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
 
Mark44 said:
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

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?
 
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:
int * findDisorder(int arr[], int n, int* p)
2. Keep the function a void function, but change the third parameter to int ** p.
 
i tried the second method you suggested and made int ** p and changed nothing else so my code for the void function was

Code:
 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??
 
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.)
 
this is my code in its entirety (without having changed the main routine)

Code:
#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 **
 
  • #10
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.
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 38 ·
2
Replies
38
Views
5K
  • · Replies 8 ·
Replies
8
Views
7K