Access violation using a basic_point class template

seeking to create own point type wrote this:

so first of all using concept to constraint the type

//concept
template<typename T>
concept Numeric = requires(T param)
{
    requires std::is_integral_v<T> || std::is_floating_point_v<T>;
    requires !std::is_same_v<bool, T>;
    requires std::is_arithmetic_v<decltype(param +1)>;
    requires !std::is_pointer_v<T>;
};

the basic point template, just a wrapper arround an array

// Point<T,n>
template <Numeric T, size_t dim = 2 > 
struct basic_point {
    constexpr auto inline copy( basic_point<T,dim> const& p) {
        std::copy(p, p + dim * sizeof(p[0]), array.begin()); 
    }
    auto inline copy( T const (&p)[dim]) { 
        std::copy(p, p + dim * sizeof(p[0]), array.begin()); 
    }
    auto inline copy( T const& val) noexcept { 
        for (auto& a : array) 
            a = val; 
    }
    // new constructor
    constexpr basic_point( T const (&pt)[dim])           { copy(pt); }
    // copy constructor
    constexpr basic_point( basic_point<T,dim> const& p)  { copy(p);  }
    // default constructor
    constexpr basic_point( T dt = 0 ) noexcept           { copy(dt); }
    virtual  ~basic_point()         = default ;
 
    constexpr auto inline operator+=( basic_point<T,dim> const& o ) noexcept { 
        for(size_t i = 0; i < dim;i++) 
            array[i] += o.array[i]; 
        }
    constexpr auto inline operator-=( basic_point<T,dim> const& o ) noexcept { for(size_t i = 0; i < dim;i++) array[i] -= o.array[i]; }
    friend constexpr inline auto operator+( basic_point<T,dim> const& a, basic_point<T,dim> const& b ) noexcept { basic_point<T,dim> sum{a};  sum  += b; return sum; }
    friend constexpr inline auto operator-( basic_point<T,dim> const& a, basic_point<T,dim> const& b ) noexcept { basic_point<T,dim> rest{a}; rest -= b; return rest;}
    std::array<T,dim> array;
};

A special template then is explicitly managing a 2d point where x and y refer to elements 0 and 1 of the array, using composition, no inheritance.

//Point2D<T> 
template<Numeric T = size_t> struct Point2D {
        constexpr Point2D(T const (&p)[2])  {
            data = p;
            x = data.array[0];
            y = data.array[1];
         }
        Point2D(Point2D const& o) : x(o.x),y(o.y){}
       
        void operator+=( Point2D const& other ) {
            x += other.x;
            y += other.y;
        }
    
        void operator-=( Point2D const& other) {
            x -= other.x;
            y -= other.y;
        }
    
        friend Point2D operator+( Point2D const& a, Point2D const& b ) {
            Point2D sum( a );
            sum += b;
            return sum;
        }
    
        friend Point2D operator-( Point2D const&a, Point2D const& b ) {
            Point2D rest( a );
            rest -= b;
            return rest;
        }
        T& x = data.array[0];
        T& y = data.array[1];
        basic_point<T,2> data = basic_point<T,2>(0);  
};

to test it a trivial example can be ran like this

int main(int argc, char* argv[]) {
    const int a[2] = { 1 , 3 };
    const int b[2] = { 1 , 3 }; 
    auto point_a = Point2D(a); 
    auto point_b = Point2D(b);
    auto result = point_a + point_b;
    std::cout << "Result : ("<< result.x << " , " << result.y <<")n";
    return 0;
}

but it compiles and terminates due to access violation. Any idea why is this happening.

a test can be seen here

11

You got the pointer arithmetic wrong when you call std::copy in your basic_point::copy method which accepts an array (T const (&p)[dim]).

Instead of:

std::copy(p, p + dim * sizeof(p[0]), array.begin()); 

It should be:

std::copy(p, p + dim, array.begin());

Because when you use operator+ on a pointer (or an array) it automatically takes into account the sizeof of the object the pointer/array points to, therefore p + dim already advances the address by dim elements of size sizeof(p[0]).

The access-violation error you get is because you access the array out-of-bounds (due to the wrong end address).

This live demo is a fixed version of the one you posted: Live demo


Another issue which is not triggered by your test is the call the std::copy in your other basic_point::copy (the one that accepts a basic_point<T,dim> const& p). Your code attempts to use p as if it was a pointer but it is actually a reference (to a point) so it doesn’t really makes sense.
I am not sure why this method is even needed, but maybe you meant something like:

std::copy(p.array.begin(), p.array.end(), array.begin());

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật