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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:32:43+00:00 2026-05-25T23:32:43+00:00

I am writing a generic linked list in C++ using templates, and am experiencing

  • 0

I am writing a generic linked list in C++ using templates, and am experiencing Segmentation Faults when accessing Node values.

To make the test case simpler, I have implemented a fixed size, two node, linked list.

I have two questions:

1a) Why isn’t aList.headNodePtr->prevNodePtr set to NULL?

1b) Why isn’t aList.tailNodePtr->nextNodePtr set to NULL?

I set both of these values to NULL in the LinkedList constructor, but the output in main shows that:

head prevAddress: 0x89485ed18949ed31
tail nextAddress: 0x7fffe8849679

2) Why does the following line in main() cause a Seg Fault?

aList.headNodePtr->nodeValue = 1;

The full code is below:

#include <iostream>
using namespace std;

template <class T>
    class Node {
    public:
    Node<T>* prevNodePtr;
    Node<T>* nextNodePtr;
    T nodeValue;
};

template <typename T>
    class LinkedList {
    public:
    Node<T>* headNodePtr;
    Node<T>* tailNodePtr;

    LinkedList() {
        Node<T>* headNodePtr = new Node<T>;
        Node<T>* tailNodePtr = new Node<T>;

        headNodePtr->prevNodePtr = NULL;
        headNodePtr->nextNodePtr = tailNodePtr;
        tailNodePtr->prevNodePtr = headNodePtr;
        tailNodePtr->nextNodePtr = NULL;
    }

    ~LinkedList() {
        headNodePtr = NULL;
        tailNodePtr = NULL;
        delete headNodePtr;
        delete tailNodePtr;
    }
};

int main()
{
    LinkedList<int> aList;
    cout << "head Value: " << aList.headNodePtr->nodeValue << endl;
    cout << "head prevAddress: " << aList.headNodePtr->prevNodePtr << endl;
    cout << "head nextAddress: " << aList.headNodePtr->nextNodePtr << endl;
    cout << "tail Value: " << aList.tailNodePtr->nodeValue << endl;
    cout << "tail prevAddress: " << aList.tailNodePtr->prevNodePtr << endl;
    cout << "tail nextAddress: " << aList.tailNodePtr->nextNodePtr << endl;

    aList.headNodePtr->nodeValue = 1;
}
  • 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-25T23:32:43+00:00Added an answer on May 25, 2026 at 11:32 pm

    You’re not actually setting the members, you’re setting the locals you declared in the ctor:

    Node<T>* headNodePtr;  // <-- MEMBERS
    Node<T>* tailNodePtr;
    
    LinkedList() {
        Node<T>* headNodePtr = new Node<T>;  // <-- LOCALS
        Node<T>* tailNodePtr = new Node<T>;
    

    Try this instead:

    Node<T>* headNodePtr;  // <-- MEMBERS
    Node<T>* tailNodePtr;
    
    LinkedList() {
        headNodePtr = new Node<T>;  // <-- MEMBER ACCESS
        tailNodePtr = new Node<T>;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a Python program I'm writing, I've built up a linked list using a
Hi I am writing a linked list data type. I have an inner class
I was just writing a generic object factory and using the boost preprocessor meta-library
For a CS class I am writing a linked list implementation of a linked
I am writing following code, using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace
I'm writing a semi-generic form plugin using jQuery in order to speed up the
Before writing a function that takes a generic bidirectional iterator I wanted to test
I am writing a generic extension method for IEnumerable for mapping a list of
I am writing a generic test function that will accept a function address (read
I was writing a generic class to read RSS feed from various source 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.