Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 821023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:34:26+00:00 2026-05-15T02:34:26+00:00

I’m writing a simple nD-vector class, but am encountering a strange bug. I’ve stripped

  • 0

I’m writing a simple nD-vector class, but am encountering a strange bug. I’ve stripped out the class to the bare minimum that still reproduces the bug:

#include <iostream>

using namespace std;

template<unsigned int size> class nvector
{
public:
  nvector() {data_ = new double[size];}
  ~nvector() {delete[] data_;}

  template<unsigned int size2> 
  nvector(const nvector<size2> &other)
  {
    data_ = new double[size];
    int i=0;
    for(; i<size && i < size2; i++)
      data_[i] = other[i];

    for(; i<size; i++)
      data_[i] = 0;
  }

  double &operator[](int i) {return data_[i];}
  const double&operator[](int i) const {return data_[i];}

private:
  const nvector<size> &operator=(const nvector<size> &other); //Intentionally unimplemented for now

  double *data_;
};

int main()
{
  nvector<2> vector2d;
  vector2d[0] = 1;
  vector2d[1] = 2;

  nvector<3> vector3d(vector2d);
  for(int i=0; i<3; i++)
    cout << vector3d[i] << " ";
  cout << endl; //Prints 1 2 0

  nvector<3> other3d(vector3d);
  for(int i=0; i<3; i++)
    cout << other3d[i] << " ";
  cout << endl; //Prints 1 2 0
} //Segfault???

On the surface this seems to work fine, and both tests print out the correct values. However, at the end of main the program crashes with a segfault, which I’ve traced to nvector’s destructor.

At first I thought the (incorrect) default assignment operator was somehow being called, which is why I added the (currently) unimplemented explicit assignment operator to rule this possibility out.

So my copy constructor must be buggy, but I’m having one of those days where I’m staring at extremely simple code and just can’t see it. Do you guys have any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:34:26+00:00Added an answer on May 15, 2026 at 2:34 am

    Templated implementation of conversion-constructor is never considered as a candidate function for a copy-constructor. Your templated copy-constructor is never called. Instead, the compiler uses an implicitly generated “default” copy-constructor implementation, which performs shallow copying with obvious consequences.

    In other words, the templated constructor you have implemented above will never be used as a copy-constructor, only as a conversion-constructor. You have to implement your copy-constructor explicitly as a non-template function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.