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

The Archive Base Latest Questions

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

I have a simple doubly linked list example that I’m working on, but for

  • 0

I have a simple doubly linked list example that I’m working on, but for some reason I keep getting the error: pointer being freed was not allocated when I try to delete

Here is a code snipet of the insert at end of list method

template <class T>
void List<T>::insertAtEnd(T *o)
{
    node *newNode = new node;
    newNode->o = o;
    newNode->next = NULL;
    newNode->prev = last;
    if(last != NULL)
        last->next = newNode;
    last = newNode;
    if(first == NULL)
        first = newNode;
    delete(newNode);            // This delete call will usually be in another method
}                               // It is just here right now for testing

This class has two instance fields, a pointer to the beginning of the list and a pointer to the end, first and last respecitvely. Each node is a struct as follows:

struct node {
    node   *next;
    node   *prev;
    T      *o;
};

So far I have only tested with inserting 1 node, but I always get the same error. However, if I comment out the lines

if(first == NULL)
    first = newNode;

it works. Help please? Thanks

EDIT: Both first and last start as NULL.

  • 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-28T16:36:27+00:00Added an answer on May 28, 2026 at 4:36 pm

    You have, unfortunately, not provided the entirety of your code. You’ve omitted the section containing your error, so I must speculate as to the exact cause.

    The error you are seeing indicates that delete is being called on an address that was not produced via new. This probably means you are inadvertently doing a value assignment, instead of a pointer assignment, and then attempting to free the copied (stack-allocated) variable. One specific way this could happen is if you attempt to delete mynode->o, since o could be the address of a stack variable.

    Double-check that every type which should be a node* is a node*, not a node or a node**. Compiling your code with -Wall -Wextra may produce helpful additional warnings and should be a first debugging step if not a general practice.

    If your types and assignments are all correct, you may be using a C++ library or compiler that doesn’t give you particular double-free messages; this would expand the scope of the error you see to include freeing the same memory twice. Try printing an address just before you free it (or using a debugger to the same effect). When you see the same address come up twice, that’s it.

    This may be obvious, but you can’t have the delete call there and have your function work right. first and last will point to freed memory. Felt that needed to be said, just in case.

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

Sidebar

Related Questions

I have looked at most threads here about Doubly linked lists but still unclear
I have simple win service, that executes few tasks periodically. How should I pass
i have simple regular expression: ^123$ Matches are for example 123 1234 etc. How
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
I have simple SSIS package in which On Error event handler I have created
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have a strange error that has bothered me all day. I have a
I have a simple Excel file that queries a database when it opens and
I have a simple recursive function to write in VBA that does the following
I have a double linked list (queue) I have made on my own. I

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.