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 793575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:10:29+00:00 2026-05-14T22:10:29+00:00

Hey there, I’m writing a template container class and for the past few hours

  • 0

Hey there, I’m writing a template container class and for the past few hours have been trying to allocate new memory for extra data that comes into the container (…hit a brick wall..:| )

template <typename T>
void Container<T>::insert(T item, int index){
    if ( index < 0){
        cout<<"Invalid location to insert " << index << endl;
        return;
    }


    if (index < sizeC){ 
    //copying original array so that when an item is 
    //placed in the middleeverything else is shifted forward
        T *arryCpy = 0;
        int tmpSize = 0;
        tmpSize = size();
        arryCpy = new T[tmpSize];
        int i = 0, j = 0;
        for ( i = 0; i < tmpSize; i++){
            for ( j = index; j < tmpSize; j++){
                arryCpy[i] = elements[j];
            }
        }
        //overwriting and placing item and location index
        elements[index] = item;
        //copying back everything else after the location at index
        int k = 0, l = 0;
        for ( k =(index+1), l=0; k < sizeC || l < (sizeC-index); k++,l++){
            elements[k] = arryCpy[l];
        }
        delete[] arryCpy;
        arryCpy = 0;
    }

    //seeing if the location is more than the current capacity
    //and hence allocating more memory
    if (index+1 > capacityC){
        int new_capacity = 0;
        int current_size = size();
        new_capacity = ((index+1)-capacityC)+capacityC;
        //variable for new capacity

        T *tmparry2 = 0;
        tmparry2 = new T[new_capacity];
        int n = 0;
        for (n = 0; n < current_size;n++){
            tmparry2[n] = elements[n];
        }
        delete[] elements;
        elements = 0;
        //copying back what we had before
        elements = new T[new_capacity];
        int m = 0;
        for (m = 0; m < current_size; m++){
            elements[m] = tmparry2[m];
        }
            //placing item
        elements[index] = item;
    }

    else{
        elements[index] = item;
    }
    //increasing the current count
    sizeC++;

my testing condition is
Container cnt4(3);
and as soon as i hit the fourth element (when I use for egsomething.insert("random",3);) it crashes and the above doesnt work. where have I gone wrong?

  • 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-14T22:10:30+00:00Added an answer on May 14, 2026 at 10:10 pm

    Several things don’t make too much sense to me:

    if (index+1 > capacityC){
    

    shouldn’t that be:

    if (index >= capacityC){
    

    Also, when you grow the array I don’t see why you are doing two lots of copying. shouldn’t:

    delete[] elements;
    elements = 0;
    

    be:

    delete[] elements;
    elements = tmparray2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey there, I have read the few posts here on when/how to use the
Hey there, I'm trying to make a site which have following: News, Products, About
Hey there, I am trying to get my signout button to work. This is
Hey there, I'm sort of new to Objective-C, and well, programming in general. I
Hey there, I'm a designer thats really new to programming with xcode or Objective-C
Hey there, I'm trying to validate a form with Zend_Validate and Zend_Form. My element:
hey there, i have a div that expands when the page is loaded, now
Hey there. I'm trying to write a small program that will read the four
Hey there, I'm at the moment trying to make a product management application. The
Hey there. been a busy debugger today. ill give ths short version. ive made

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.