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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:39:35+00:00 2026-05-30T22:39:35+00:00

I have defined a vector like this in the header file class entry {

  • 0

I have defined a vector like this in the header file

class entry
{
   public: 
     int key;
     int next;
};
std::vector<entry *> TB;

in the cpp file, I wrote:

int s1, val;
s1 = 10; val = 2;
gh = (TB.size() % s1);

However when I want to write something to it, I get segmentation fault

TB[gh]->key = val;   

What is the problem with the assignment?

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

    The vector has no elements. Use push_back() or insert() to add elements to the vector:

    entry* e = new entry();
    e->key   = val;
    e->next  = 0;
    
    TB.push_back(e);          // Append to vector.
    TB.insert(TB.begin(), e); // Insert at beginning of the vector.
    

    When destroying the vector TB you must iterate over the elements and delete each individually (or use a smart pointer as the element type, such as boost::shared_ptr<entry> or std::unique_ptr<entry>).

    You could provide a constructor(s) for entry to make the addition of an entry to TB more concise:

    class entry
    {
    public: 
        entry(int a_key, int a_next = 0) : key(a_key), next(a_next) {}
        int key;
        int next;
    };
    
    TB.push_back(new entry(val));
    TB.insert(TB.begin(), new entry(val));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a map defined inside a class in header file like this:
I have two containers, let's say they're defined like this: std::vector<std::unique_ptr<int>> a; std::vector<std::unique_ptr<int>> b;
I have a 3D vector defined like this... std::vector<std::vector<std::list<Object*> > > m_objectTiles; I have
suppose I have an object like this: class Spline { public: Spline(std::size_t const dim);
I have the following in my Puzzle.h class Puzzle { private: vector<int> puzzle; public:
I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
I have a class defined like this: template< class TBoundingBox > class BoundingBoxPlaneCalculator {
I have a vector like this std::vector<Sprite*> mDrawings; std::vector<Sprite*>::iterator it = mDrawings.begin(); This gives
i have a priority queue and i have defined like this: priority_queue<Node*,vector<Node*>,greater<Node*>> myQueue; i
I have an object like below class MyClass{ int a; double b; }; and

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.