I'm receiving this error when I'm trying to run my program: Program received signal: “EXC_BAD_ACCESS”.
Does anyone know what this means?

Dani AI

Generated

EXC_BAD_ACCESS means the program tried to read or write memory it wasn’t allowed to. Common causes are out‑of‑bounds indexing, use of uninitialized values, or stack corruption. In this thread the crash happens after entering coefficients and when control moves to the polynomial routine, so the fault is inside that function.

As already noted, two important issues exist: an automatic array sized at run time (not portable C++) and use of an uninitialized x. There is one more bug that explains the immediate crash: the loop inside the evaluator decrements an index that was started at zero while testing a “<=” condition, so the index becomes negative and c[...] is read out of bounds. That out‑of‑bounds access is the most likely trigger for EXC_BAD_ACCESS, which matches ’s observation that the failure appears when calling the polynomial routine.

Recommended fixes and a safer evaluation:

  • Allocate coefficients with a dynamic container (for example std::vector<double> sized n+1) so bounds are explicit.
  • Make the polynomial evaluator take x as a parameter (don’t use an uninitialized local).
  • Avoid repeated pow() for integer exponents; Horner’s method is simpler and faster. Example evaluator:
double eval_poly(const std::vector<double>& coeff, double x) {
    double result = 0.0;
    for (int i = (int)coeff.size() - 1; i >= 0; --i)
        result = result * x + coeff[i];
    return result;
}

Compile with warnings and the address sanitizer to find memory errors early:

g++ -std=c++17 -Wall -Wextra -pedantic -fsanitize=address -g main.cpp -o main

Quick checklist: validate n >= 0, allocate n+1 slots, initialize or pass x, fix loop directions, and run with sanitizers. After those fixes the derivative logic can be added safely.

Recommended Answers

All 7 Replies

Can you show the line of code that throws the exception?

This is the code I have written so far. Right now the code only gets the polynomial, later it will create the derivative. The error is not in the code itself. It runs until all the coefficients are input and then I get the bad access thing.

#include <iostream>
#include <cmath>

using namespace std;

double poly(double[], int);
double drv(double[], int, double);

int main ()
{
	int n;
	cout << "Enter the degrees of the polynomial: ";
	cin >> n;
	
	double c[n];
	
	for (int i=n; i>=0; i--) 
	{
		cout << "Enter the coefficient for the variable of degree " << i << ": ";
		cin >> c[i];
	}
    
	cout << "The polynomial entered is: " << poly(c,n) << endl; //I think this is where it's messing up
	
	return 0;
}

double poly(double c[], int n)
{
	double sum=0.0;
	double x;
	
	for (int i=0; i<=n; i--) 
	{
		sum+=c[i]*pow(x, i);
	}
	
	return sum;
}

Does your compiler compile this?
You are allocating an array with a non-constant (line 15)

Also x is never initialized and is used on line 35.

What does this mean?

You are allocating an array with a non-constant (line 15)

The compiler compiles it until it reaches the point where it's supposed to pick up from the function, so something is wrong with the function. The x is because I want it to say that it's the coefficient (c) times x^i, because later on in the program I need it to take the derivative.

x does not have a value.
You will get random results at best or a crash.

How could I do it differently?

If your intention is to use the pow() function, both parameters need to have actual values.
...meaning: The variable x needs a value before you use it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.