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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:28:58+00:00 2026-06-05T23:28:58+00:00

I coded the following. Insert, delete at beginning, insert at beginning and end are

  • 0

I coded the following. Insert, delete at beginning, insert at beginning and end are all working fine. The memory marked in bold is not freed up. Should cout<<temp give an error? Please comment on the correctness of this code.

void del(node** head)
{
    node* temp = (*head)->next;
    node* prev = *head;
    while(temp ->next!= NULL)
    {
        prev = temp;
        temp = temp -> next;
    }
    cout<<endl<<temp;
    cout<<endl<<prev;

    //delete prev ->next;
    prev -> next = 0;

    delete temp;
    cout<<endl<<"temp after free"<<temp;
    cout<<endl<<prev;
}
void main()
{
    node* head = NULL;  
int x = 5;
head = insert(head,x);
insert(head,6);
insert(head,7);
insert(head,8);
print(head);
del(&head);
print(head);
getch();    }

Output:

Empty List
|5| at 00673BF0
-------------------
|6| at 00673C30
-------------------
|7| at 00673FB8
-------------------
|8| at 00673FF8
-------------------

00673FF8
00673FB8
temp after free00673FF8
00673FB8

|5| at 00673BF0
-------------------
|6| at 00673C30
-------------------
|7| at 00673FB8
-------------------
  • 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-06-05T23:29:00+00:00Added an answer on June 5, 2026 at 11:29 pm

    delete does not set the value of the pointer to NULL, but the memory pointed to is no longer valid (does not contain a live node)

    This means that cout << temp will print the value of the pointer it still has (and was the address of the node before the delete), but dereferencing temp (e.g. *temp or temp->next) is undefined behavior

    Note: nowehere in del do you modify the pointer pointed to by head, so you either don’t need the double indirection (node**) or you should assign the new head to *head.

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

Sidebar

Related Questions

Just run into a tricky NSFetchedResultsController problem. The following code works fine in all
In my project, I have cached foreign annotations and did all insert and delete
My goal is to achieve delete all recommendations and then insert new. (any online
I can insert/update/delete fine from tables like product and such but this code fails
I have the following code I use to insert form data into a single
How can the following code be modified to run my insert statements 100 at
I have the following code: <? $query =$db->prepare(INSERT INTO a_table (id, a_field) VALUES ('',
I'm using the following code: $query = $dbh->prepare(INSERT into crm_order_errors .SET (order_id, number_of_attempts, last_attempt)
I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT
I have the following Python code: cursor.execute("INSERT INTO table VALUES var1, var2, var3,") where

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.