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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:08:14+00:00 2026-05-13T07:08:14+00:00

I wanted to test the following code (which works fine for a non-null list)

  • 0

I wanted to test the following code (which works fine for a non-null list) to see what would happen in the case of an empty list (in which case the head would be null).

hence the code which applies to filling the list is commented out..

But for some strange reason, the test for NULL in print_nodes() just doesnt seem to work. ive added some debug cout calls to see (and also checked using gdb) but whilst the value does indeed appear to be NULL, any if statements dont seem to test the equivalence properly..

any idea why?

many thanks!

   #include <iostream>
using namespace std;

struct node {
  char dat;
  node *nextPtr;
};

//inserts new node and returns pointer
node* new_node(char data, node* prevNode);

//adds a new node at the head ofthe list
void new_head (node *head_, char dat_);

//inserts new node after *before
void insert_node (node *before, char dat_);

//runs through and prints the list - requires first node (head)
void print_nodes (node *head);


int main() {
  cout <<endl << endl;
  cout << endl << "*******************RUN******************" <<endl <<endl;

  node* head = NULL;

  if (head == NULL) { 
    cout << "head null"; //this works here
  }


  //head non-standard
  //  node* head = new node;
  //  head->dat ='a';

  /*
  node* b = new_node('b', head);
  node* c = new_node('c', b);
  node* d = new_node('d', c);
  node* e = new_node('e', d);
  node* f = new_node('f', e);

  */
  print_nodes(head);

  insert_node(head,'N');

  print_nodes(head);

  cout << endl << "*******************END RUN******************" <<endl;
  return 0;
}

node* new_node(char data, node* prevNode) {
  node* tempPtr = new node;
  tempPtr->dat = data;
  tempPtr->nextPtr = NULL; //standard
  prevNode->nextPtr = tempPtr;
  return tempPtr;
}

void new_head (node *head_, char dat_) {

}

void insert_node (node *before, char dat_) {
  node* tempPtr = new node;
  tempPtr->dat = dat_;

  tempPtr->nextPtr = before->nextPtr;
  before->nextPtr = tempPtr;

}


void print_nodes (node *head) {

  node* tempPtr = head;

  cout << "\nPrinting nodes..." <<endl;

  if (tempPtr == NULL) { //this test is not working.. why?
    cout << "tempPtr is NULL";
    return; 
  } else { //only run in the non null case
    for (tempPtr; tempPtr != NULL; tempPtr = tempPtr->nextPtr) {
      cout << "Current node content: " << tempPtr->dat <<endl;
    }  
  }
}
  • 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-13T07:08:14+00:00Added an answer on May 13, 2026 at 7:08 am

    You have a problem: head was not allocated, but insert accesses its “next element”:

    before->nextPtr = tempPtr;
    

    head is passed in as before, and you didn’t allocate memory for head. Hence you dereference a NULL pointer here.

    Could it be that your application crashes as a result, and the printout to cout isn’t done because cout is buffered?

    Try to:

    1. Remove the call to insert
    2. Change cout to cerr (unbuffered)

    Report the results of these changes.

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

Sidebar

Related Questions

I have the following test case in eclipse, using JUnit 4 which is refusing
I have written a simple WPF application in which I wanted to test the
Hi I wanted know the reasons of the following code void main() { class
I'm doing a small test. The following code runs 3 random queries 1000 times
I wanted to test an app which I developed against API level 7 on
I have the following code: <tr id=test runat=server> <td align=<%=myalignment%>> I set myalignment =
I have the following HttpModule that I wanted to unit test. Problem is I
I wanted to test how C++ behaves when the return value of a function
Out of curiosity I wanted to test the number of ticks to compare a
I set up a new webapp Maven project and wanted to test it with

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.