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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:01:36+00:00 2026-05-26T04:01:36+00:00

template <typename T> class LinkedNode { public: T data; LinkedNode<T> *next; LinkedNode<T> *prev; LinkedNode<T>();

  • 0
template <typename T>
class LinkedNode
{
public:
    T data;
    LinkedNode<T> *next;
    LinkedNode<T> *prev;
    LinkedNode<T>();
    LinkedNode<T>(T);
};

// Default constructor
template <typename T>
LinkedNode<T>::LinkedNode()
{
    next = NULL;
    prev = NULL;
}



template <typename T>
class LinkedList
{
private:
    LinkedNode<T> *head;
public:
    LinkedList<T>();
    ~LinkedList<T>();
    void addFront(T);
    void addBack(T);
    void addAt(T, int);
    void removeFront();
    void removeBack();
    void removeAt(int);
    void printList();
};

// Constructor
template <typename T>
LinkedList<T>::LinkedList()
{
    head = NULL;
}

// Add new node to front
template <typename T>
void LinkedList<T>::addFront(T d)
{
    LinkedNode<T> temp;
    temp.data = d;

    if (head == NULL)
    {
        head = &temp;
    }
    else
    {
        temp.next = head;
        head->prev = &temp;
        head = &temp;
    }
}

// Add new node to back
template <typename T>
void LinkedList<T>::addBack(T d)
{
    // Find the back of this list
    LinkedNode<T> *ptr;
    ptr = head;

    while (ptr->next != NULL) // <------- DIES HERE, MEMORY ACCESS VIOLATION
    {
        ptr = ptr->next;
    }

    // Make a new node and join it to the back
    LinkedNode<T> temp;
    temp.data = d;
    temp.prev = ptr;
    ptr->next = &temp;
}

Here is a snippet from my linked list system. The issue is that it throws an error on the indicated line. The debugger says that the “head” pointer is pointing to a legitimate memory address of a LinkedNode with no “next” or “prev”, but the “ptr” pointer points to address 0xcccccc, not the address of head? I’m really confused, I thought I understood pointers!

  • 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-26T04:01:36+00:00Added an answer on May 26, 2026 at 4:01 am
    void LinkedList<T>::addFront(T d)
    {
        LinkedNode<T> temp;
        temp.data = d;
        ...
        head = &temp;
        ...
    }
    

    temp is a variable of automatic storage, its lifetime will end as soon as the addFront function returns and you will get a pointer to an invalid object. You have to allocate nodes in the heap, do this instead:

        LinkedNode<T>* temp = new LinkedNode<T>;
        temp->data = d;
        ...
        head = temp;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose a construct like this: class Interface { public: template <typename T> virtual void
Given class Foo template <typename T> class Foo { public: ...other methods.. void bar()
Given this code: class X { public: template< typename T > void func( const
template<typename Type> class List { public: List(void); ~List(void); ... } which inherited by template<typename
Consider a template class like: template<typename ReturnType, ReturnType Fn()> class Proxy { void run()
I have a class template <typename Iterator, typename Value> class Foo { public: Foo(const
This is my template matrix class: template<typename T> class Matrix { public: .... Matrix<T>
template<typename T> class vec3 { public: typename T type_t; T x; T y; T
template<typename T> class ClassVariantVisitor : public boost::static_visitor<T> { public: T operator()(int& i) const {
I have a fictional class: template<typename T> class demonstration { public: demonstration(){} ... T

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.