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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:59:28+00:00 2026-06-06T14:59:28+00:00

I am implementing a vector for an exercise. I want to do the following:

  • 0

I am implementing a vector for an exercise.

I want to do the following:

Initially allocate 100 elements without calling it’s constructors. Whenever an object is added to the vector, it call it’s constructor until the vector is too large so that can’t contain all objects. When the vector is full I allocate other 100 objects, and so on.

This is the code:

#include <iostream>
#include <memory>
#include <exception>
#include <cstdarg>

using namespace std;

class indexOutOfBounds:exception
{
    const virtual char* what()
    {
        return "Index out of bounds";
    }
};

template <class T>
class Vector
{
private:
    T* data;
    allocator<T> data_all;
    int length;
    int _size;
public:
    static const int block=100; // the size of a single allocation block
    Vector()
    {
        data=data_all.allocate(block,NULL);
        length=0;
        _size=block;
    }
    Vector(int n,...)
    {
        va_list vl;
        T temp;
        va_start(vl, n);
        for(int i=0; i<n;i++)
        {
            temp=va_arg(vl,T);
            push_back(temp);
        }
        va_end(vl);
    }
    int size() const
    {
        return length;
    }
    void push_back(T item)
    {
        length++;
        if(length==_size)
        {
            _size+=block;
            data=data_all.allocate(_size,data);
        }
        data_all.construct(&data[length-1],item);
    }
    T& operator[] (int i) throw()
    {
        if(i<0 || i>=length)
            throw indexOutOfBounds();
        return data[i];
    }
    ~Vector()
    {
        for(int i=0; i<length;i++)
        {
            data_all.destroy(&data[i]);
        }
        data_all.deallocate(data,_size);
    }
};

int main(int argc, char** argv)
{
    Vector<int> v(1,0);
    cout << v[0] << endl;
    return 0;
}

I get the exception when I try to print v[0]:

EXC_BAD_ACCESS(code=1, address= 0x0)

Maybe &v[0] is NULL, but I can’t figure the reason. If I don’t use the constructor with va_list and I just write a main like this:

int main(int argc, char** argv)
{
    Vector<int> v;
    v.push_back(1);
    cout << v[0] << endl;
    return 0;
}

I don’t get any exception. Can someone explain why?

  • 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-06-06T14:59:29+00:00Added an answer on June 6, 2026 at 2:59 pm

    The most probable reason is that in the constructor with variable arguments, you do not initialize length or _size. This means that those values might be anything when you call push_back.

    If you would have run your application in a debugger, and examined those variables, it would have been obvious. I recommend that next time you have a crash of some sort, run it in a debugger and try to figure it out first, before asking here.

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

Sidebar

Related Questions

Implementing a custom Dependency Property on a Framework Element object causes my Visual Studio
I am implementing a templated sparse_vector class. It's like a vector, but it only
I am currently working on implementing the constructor function for the vector class(my professor
I'm implementing a vector type. I'm not troubled by the algorithms or the data
Consider the following declarations of a std::vector (taken from cplusplus - EASTL has the
I have two algorithms that I'm implementing: AlgorithmA which works with Vector values and
Suppose I have a class similar to the following: #include <vector> class element {
I'm implementing a simple class to represent a 2D vector. Here's are the relevant
I am implementing a shopping website through JSP. I have a Java object called
I'm implementing vector class and I need to get an opposite of some vector.

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.