Access voilation and Stack overflow error in visual studio

0

The code was working fine until suddenly it started throwing exceptions out of the blue. Before this, these exceptions never came up at all. The debugger stops at these two points and for some reason keeps showing me correct_math.h. I have three execptions in total and I'm not sure what to do.

Unhandled exception at 0x0042FDF9 in ray tracer.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00802F50).

vec3 origin() const { return o; }

Exception thrown at 0x0042D859 in ray tracer.exe: 0xC0000005: Access violation writing location 0x002B0FB8.

vec3(float a, float b, float c) : x{ a }, y{ b }, z{ c }{}

and

Exception thrown at 0x010AD0E9 in ray tracer.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x004E2F80).

--from correct_math.h

        _Check_return_ __inline float __CRTDECL sqrtf(_In_ float _X)
        {
            return (float)sqrt(_X);
        }

class files just in case

the ray class

#ifndef RAY_H_INCLUDED
#define RAY_H_INCLUDED

#include "vec3.h"

class ray
{
public:
    vec3 o, d;
    ray(const vec3& _o, const vec3& _d):o{_o}, d{_d}{}
    vec3 origin() const { return o; }//exception
    vec3 direction() const { return d; }
    vec3 get_point(float t) const { return o + t * d; }
};


#endif // RAY_H_INCLUDED

and the vec class

#pragma once

#ifndef VEC_H
#define VEC_H

#include<cmath>
#include<iostream>

constexpr double M_PI = 3.1415926535;

class vec3
{
public:
    float x, y, z;
    vec3() : x{ 0.0f }, y{ 0.0f }, z{ 0.0f }{}
    vec3(float a, float b, float c) : x{ a }, y{ b }, z{ c }{}//exception
    float r() const { return x; }
    float g() const { return y; }
    float b() const { return z; }
    vec3& operator+=(const vec3& v);
    float lenght();
    float lenght2();
};
vec3 unit_vector(vec3 v1);

vec3 operator + (const vec3& v1, const vec3& v2);//adding two vectors

vec3 operator - (const vec3& v1, const vec3& v2);//subtracting two vectors

vec3 operator - (const vec3& v1);//vector negation

vec3 operator * (float f, const vec3& v);//scaling up a vector

vec3 operator * (const vec3& v, float f);//scaling up a vector overide

vec3 operator / (const vec3& v, float f);//scaling down a vector

vec3 cross (const vec3& v1, const vec3& v2);

float dot(const vec3& v1, const vec3& v2);

double deg2rad(float degrees);

#endif // VEC_H

and the definitions

#include "vec3.h"

float vec3::lenght()
{
    return sqrt(lenght2());
}

float vec3::lenght2()
{
    return x * x + y * y + z * z;
}

vec3 unit_vector(vec3 v1)
{
    return v1 / v1.lenght();
}

vec3& vec3:: operator+=(const vec3& v) {
    x += v.x;
    y += v.y;
    z += v.z;
    return *this;
}

vec3 operator + (const vec3& v1, const vec3& v2)
{
    return vec3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}

vec3 operator - (const vec3& v1, const vec3& v2)
{
    return vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}

vec3 operator - (const vec3& v1)
{
    return vec3(-v1.x, -v1.y, -v1.z);
}

vec3 operator * (float f, const vec3& v)
{
    return vec3(v.x * f, v.y * f, v.z * f);
}

vec3 operator * (const vec3& v, float f)
{
    return vec3(v.x * f, v.y * f, v.z * f);
}

vec3 operator / (const vec3& v, float f)
{
    return vec3(v.x / f, v.y / f, v.z / f);
}


vec3 cross(const vec3& v1, const vec3& v2)
{
    return vec3((v1.y * v2.z - v1.z * v2.y), (v1.z * v2.x - v1.x * v2.z), (v1.x * v2.y - v1.y * v2.x));
}

float dot(const vec3& v1, const vec3& v2)
{
    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}

double deg2rad(float degrees)
{
    return (degrees * (M_PI / 180.0f));
}
c++
asked on Stack Overflow Apr 21, 2020 by JerSci

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0