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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:45:16+00:00 2026-05-26T05:45:16+00:00

I have a technical problem and it’s really confusing me. I apologise in advance

  • 0

I have a technical problem and it’s really confusing me. I apologise in advance because I may not be giving the relevant details; I don’t yet why it’s going wrong and it would be excessive to include all the code I’m working with.

I’m working with a large program that uses the C++ STL. I’m moving this code to a very sensitive environment without a standard clib nor STL implementaton; it will redefine malloc/free/new/delete etc… For that, I need to replace the std:: parts with my own simplified implementations. I’ve started with std::vector. Right now it’s running in the standard ecosystem so it’s the GNU libc and STL. The only thing that’s changed is this vector class.

When I execute the program with the replaced class, it segfaults. I’ve put this through GDB and found that the program will request an object from the vector using the subscript operator. When the object reference is returned, a method is invoked and the program segfaults. It seems it can’t find this method and ends up in main_arena() in GDB. The type of the object is an inherited class.

I’m really not sure at all what the problem is here. I would love to provide additional details, but I’m not sure what more I can give. I can only assume something is wrong with my vector implementation because nothing else in the program has been changed. Maybe there’s something obvious that I’m doing wrong here that I’m not seeing at all.

I’m using: g++ (GCC) 4.4.5 20110214 (Red Hat 4.4.5-6)

I’d really appreciate any feedback/advice!

#ifndef _MYSTL_VECTOR_H_
#define _MYSTL_VECTOR_H_

#include <stdlib.h>
#include <assert.h>

typedef unsigned int uint;

namespace mystl
{
    /******************
      VECTOR
    ********************/


    template <typename T>
    class vector 
    {

        private:

            uint _size;
            uint _reserved;
            T *storage;

            void init_vector(uint reserve)
            {
                if (reserve == 0)
                {
                    _reserved = 0;
                    return;
                }

                storage = (T*)malloc(sizeof(T)*reserve);
                assert(storage);

                _reserved = reserve;
            }

        public:
            vector()
            {
                    // std::cerr << "default constructor " << this << std::endl;
                    storage = NULL;
                    _size = 0;
                    _reserved = 0;
            }

            vector(const vector<T> &other)
            {
                // std::cerr << "copy constructor " << this << std::endl;

                storage = NULL;
                _size = 0;
                _reserved = 0;


                init_vector(other.size());
                _size = other.size();

                for (uint i=0; i<other.size(); i++)
                {
                    storage[i] = T(other[i]);
                }
            }

            vector(uint init_num, const T& init_value)
            {
                    // std::cerr << "special constructor1 " << this << std::endl;

                        storage = NULL;
                        _size = 0;
                        _reserved = 0;

                      init_vector(init_num);

                      for (size_t i=0; i<init_num; i++)
                      {
                          push_back(init_value);
                      }
            }

            vector(uint init_num)
            {
                    // std::cerr << "special constructor2 " << this << std::endl;

                        storage = NULL;
                        _size = 0;
                        _reserved = 0;

                      init_vector(init_num);
            }

            void reserve(uint new_size) 
            {   
                if (new_size > _reserved) 
                {

                    storage = (T*)realloc(storage, sizeof(T)*new_size);
                    assert(storage);

                    _reserved = new_size;
                }
            }

            void push_back(const T &item) 
            {
                if (_size >= _reserved) 
                {
                    if (_reserved == 0) _reserved=1;
                    reserve(_reserved*2);
                }

                storage[_size] = T(item);
                _size++;
            }

            uint size() const
            {
                return _size;
            }

            ~vector()
            {
                if (_reserved)
                {
                    free(storage);
                    storage = NULL;
                    _reserved = 0;
                    _size = 0;
                }
            }

            // this is for read only
            const T& operator[] (unsigned i) const
            {
                // do bounds check...
                if (i >= _size || i < 0)
                {
                    assert(false);
                }
                return storage[i];
            }


            T& operator[] (unsigned i)
            {
                // do bounds check...
                if (i >= _size || i < 0)
                {
                    assert(false);
                }
                return storage[i];
            }

            // overload = operator
            const vector<T>& operator= (const vector<T>& x)
            {
                // check for self
                if (this != &x)
                {   
                    _reserved = 0;
                    _size = 0;
                    storage = NULL;

                    init_vector( x.size() );

                    for(uint i=0; i<x.size(); i++)
                    {
                        storage[i] = T(x[i]);
                    }

                    _size = x.size();
                }

                return *this;
            }

            uint begin() const
            {
                return 0;
            }

            void insert(uint pos, const T& value)
            {
                push_back(value);
                if (size() == 1)
                {
                          return;
                }
                for (size_t i=size()-2; i>=pos&& i>=0 ; i--)
                {
                    storage[i+1] = storage[i];
                }
                storage[pos] = value;
            }

            void erase(uint erase_index)
            {
                if (erase_index >= _size) 
                {
                    return;
                }
                //scoot everyone down by one
                for (uint i=erase_index; i<_size; i++)
                {
                    storage[i] = storage[i+1];
                }
                _size--;
            }


            void erase(uint start, uint end)
            {

                if (start > end)
                {
                    assert(false);
                }

                if (end > _size)
                    end = _size;

                for (uint i=start; i<end; i++)
                {
                    erase(start);
                }

                assert(false);
            }

            void clear()
            {
                erase(0,_size);
            }

        bool empty() const
        {
            return _size == 0;
        }

    }; //class vector
}


#endif // _MYSTL_VECTOR_H_
  • 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-26T05:45:17+00:00Added an answer on May 26, 2026 at 5:45 am

    Wow!

    1. Your assignment operator also leaks memory.

    2. Becuause you are using malloc/release the constructor to your type T will will not be called and thus you can not use your vector for anything except the most trivial of objects.

    Edit:

    I am bit bored this morning: Try this

    #include <stdlib.h> // For NULL
    #include <new>      // Because you need placement new
    
    // Because you are avoiding std::
    // An implementation of swap
    template<typename T>
    void swap(T& lhs,T& rhs)
    {
        T   tmp = lhs;
        lhs = rhs;
        rhs = tmp;
    }
    
    
    template <typename T>
    class vector
    {
        private:
            unsigned int dataSize;
            unsigned int reserved;
            T*           data;
    
        public:
            ~vector()
            {
                for(unsigned int loop = 0; loop < dataSize; ++loop)
                {
                    // Because we use placement new we must explicitly destroy all members.
                    data[loop].~T();
                }
                free(data);
            }
            vector()
                : dataSize(0)
                , reserved(10)
                , data(NULL)
            {
                reserve(reserved);
            }
    
            vector(const vector<T> &other)
                : dataSize(0)
                , reserved(other.dataSize)
                , data(NULL)
            {
                reserve(reserved);
                dataSize = reserved;
                for(unsigned int loop;loop < dataSize;++loop)
                {
                    // Because we are using malloc/free
                    // We need to use placement new to add items to the data
                    // This way they are constructed in place
                    new (&data[loop]) T(other.data[loop]);
                }
            }
    
            vector(unsigned int init_num)
                : dataSize(0)
                , reserved(init_num)
                , data(NULL)
            {
                reserve(reserved);
                dataSize = reserved;
                for(unsigned int loop;loop < dataSize;++loop)
                {
                    // See above
                    new (&data[loop]) T();
                }
            }
    
            const vector<T>& operator= (vector<T> x)
            {
                // use copy and swap idiom.
                // Note the pass by value to initiate the copy
                swap(dataSize, x.dataSize);
                swap(reserved, x.rserved);
                swap(data,     x.data);
    
                return *this;
            }
    
            void reserve(unsigned int new_size)
            {
                if (new_size < reserved)
                {    return;
                }
    
                T*  newData = (T*)malloc(sizeof(T) * new_size);
                if (!newData)
                {    throw int(2);
                }
    
                for(unsigned int loop = 0; loop < dataSize; ++loop)
                {
                    // Use placement new to copy the data
                    new (&newData[loop]) T(data[loop]);
                }
                swap(data, newData);
                reserved    = new_size;
    
                for(unsigned int loop = 0; loop < dataSize; ++loop)
                {
                    // Call the destructor on old data before freeing the container.
                    // Remember we just did a swap.
                    newData[loop].~T();
                }
                free(newData);
            }
    
            void push_back(const T &item)
            {
                if (dataSize == reserved)
                {
                    reserve(reserved * 2);
                }
                // Place the item in the container
                new (&data[dataSize++]) T(item);
            }
    
            unsigned int  size() const  {return dataSize;}
            bool          empty() const {return dataSize == 0;}
    
            // Operator[] should NOT check the value of i
            // Add a method called at() that does check i
            const T& operator[] (unsigned i) const      {return data[i];}
            T&       operator[] (unsigned i)            {return data[i];}
    
            void insert(unsigned int pos, const T& value)
            {
                if (pos >= dataSize)         { throw int(1);}
    
                if (dataSize == reserved)
                {
                        reserve(reserved * 2);
                }
                // Move the last item (which needs to be constructed correctly)
                if (dataSize != 0)
                {
                    new (&data[dataSize])  T(data[dataSize-1]);
                }
                for(unsigned int loop = dataSize - 1; loop > pos; --loop)
                {
                    data[loop]  = data[loop-1];
                }
                ++dataSize;
    
                // All items have been moved up.
                // Put value in its place
                data[pos]   = value;
            }
    
            void clear()                                        { erase(0, dataSize);}
            void erase(unsigned int erase_index)                { erase(erase_index,erase_index+1);}
            void erase(unsigned int start, unsigned int end)    /* end NOT inclusive so => [start, end) */
            {
                if (end > dataSize)
                {   end     = dataSize;
                }
                if (start > end)
                {   start   = end;
                }
                unsigned int dst    = start;
                unsigned int src    = end;
                for(;(src < dataSize) && (dst < end);++dst, ++src)
                {
                    // Move Elements down;
                    data[dst] = data[src];
                }
                unsigned int count = start - end;
                for(;count != 0; --count)
                {
                    // Remove old Elements
                    --dataSize;
                    // Remember we need to manually call the destructor
                    data[dataSize].~T();
                }
            }
            unsigned int begin() const  {return 0;}
    
    
    }; //class vector
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Unfortunately I don't have a lot of technical information to give you but I
Not very technical, but... I have to implement a bad words filter in a
I have a similar technical problem I am trying to solve where I am
NOTE: I have changed the details provided due to comments provided. The new technical
I have problem with starting emulator with scale option between 0.4 and 1, not
I'm not sure where the problem is... I have an ajax request that checks
I have encountered a problem. My customer add me as Technical Role in his
I currently have a shared folder with 2 other (not very technical) people where
I have a technical problem about my architecture. I explain my goal with this
I have a technical interview on Monday and they were kind enough to give

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.