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

  • Home
  • SEARCH
  • 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 1053901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:19:25+00:00 2026-05-16T17:19:25+00:00

I wrote my first ever C++ template code on expandable array and I am

  • 0

I wrote my first ever C++ template code on expandable array and I am getting a segmentation fault! After an hour of debugging I have realized that I need help. Something is wrong with the constructor or the destructor I think but not sure.

The code is on pastie ready to be compiled.
http://pastie.org/1150617

/* Expandable array in C++ */

#include <iostream>
using namespace std;

template <class T>
class EArray{
private:
    T* arr;
    int size;
public:
    EArray(int l);
    ~EArray();

    void setElement(int i, const T& newval);
    void eraseElement(int i);
    void addElement(int i, const T& newval);
    void push(const T& newval);
    void display();
};

template <class T>
EArray<T>::EArray(int l){
    size = l;
}

template <class T>
EArray<T>::~EArray(){
    delete [] arr;
    arr = NULL;
}

template <class T>
void EArray<T>::setElement(int i, const T& newval){
    if(i < size && i >= 0){
        arr[i] = newval;
    }
}

template <class T>
void EArray<T>::eraseElement(int index){
    size -= 1;
    T* newarr = new T[size];
    for (int i = 0; i < size+1; i++){
        if (i < index){
            newarr[i] = arr[i];
        }
        else if(i > index){
            newarr[i-1] = arr[i];
        }
    }
    delete [] arr;
    arr = newarr;
}

template <class T>
void EArray<T>::addElement(int index, const T& newval){
    size += 1;
    T* newarr = new T[size];
    for(int i = 0; i < size; i++){
        if(i<index){
            newarr[i] = arr[i];
        }
        else if (i == index){
            newarr[i] = newval;
        }
        else{
            newarr[i] = arr[i-1];
        }
    }
    delete [] arr;
    arr = newarr;
}

template <class T>

void EArray<T>::push(const T& newval){
    size += 1;
    T * newarr = new T[size];
    for (int i = 0; i < size-1; i++){
        newarr[i] = arr[i];
    }
    newarr[size-1]=newval;
    delete [] arr;
    arr = newarr;
}

template <class T>
void EArray<T>::display(){
    for(int i = 0; i < size; i++){
        cout << arr[i] << endl;
    }
}

int main(){
    EArray<int> A(6);
    A.setElement(0,34);
    A.setElement(1,544);
    A.setElement(2,32);
    A.setElement(3,324);
    A.setElement(4,24);
    A.display();
    A.addElement(3,12);
    A.display();
    A.eraseElement(4);
    A.display();
    A.push(32456);
    A.display();
}
  • 1 1 Answer
  • 1 View
  • 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-16T17:19:25+00:00Added an answer on May 16, 2026 at 5:19 pm

    It has nothing to do with templates. It’s just a problem of memory management. In the constructor of EArray, you have never initialized arr, so by default it contains some invalid pointer.

    But then in setElement, you used this invalid pointer arr[i] = newval;, which should cause a SegFault.

    It should be fixable by adding

    arr = new T[size];
    

    in the constructor (result: before, with segfault — after, running fine).

    (BTW, in practice, please use a std::vector.)

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

Sidebar

Related Questions

I am writing my first ever code for AWS. I have downloaded the AWS
Just trying to write my first ever WP7 app. I have several hundreds of
I wrote code first without using functions to prototype, and of course, it worked
I am using Entity Frameworks Code First. I have one entity that I need
I'm new to OpenCl, and I'm having trouble running my first ever code. I
I wrote my first program in Haskell today. It compiles and runs successfully .
I wrote my first program almost fifty years ago (yes, coding is still a
First of all, I wrote a simple php page, that picks up some variables
I'm using Dynatree for the first time and wrote a PHP script that returns
I'm trying wxpython for the first time. I've wrote a GUI for a python

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.