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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:42:02+00:00 2026-06-06T12:42:02+00:00

I am experimenting with a linked list. My function null seems to modify my

  • 0

I am experimenting with a linked list. My function “null” seems to modify my list even though the list is not passed by reference. I’ve read that these problems can occur with objects that are passed as normal call-by-value parameter and that it is one the reasons that data inside a class is not declared as an public member in good OOP. I have tried the null function as an member function of the list and it works fine, but I still would like to understand why this way doesn’t work properly.
Thanks

#include <iostream>
#include <new>
#include <time.h>
#include <stdlib.h>
using namespace std;

class list{
    public:
    struct element {
    int data;
    element* next;
};

element * head;


list(){
    head=NULL;
}

~list(){
while (head!=NULL){
    element *e = head->next;
    delete head;
    head = e;
    }
    cout<<"Destructing..\n";
}

void add (int value){
    element *e = new element;
    e->data = value;
    e->next = head;
    head= e;
    }
};

void fill10 (class list & l){
    for (int i= 0; i<10 ;i++){
    l.add((rand()%10)+1);
    }
}

bool null (class list l){
    if (l.head!=NULL){ return false;}
    return true;
}


int main ()
{
    srand(time(NULL));
    class list l;
    fill10(l);

    cout<<l.head->data<<endl;
    cout<<l.head<<endl;

    cout<<endl<<null(l)<<endl;//when I comment this everything works out as expected

    cout<<l.head->data<<endl; //this data is not the same anymore after null is called
    cout<<l.head<<endl;

   return 0;
}
  • 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-06T12:42:03+00:00Added an answer on June 6, 2026 at 12:42 pm

    The problem is the way you pass the parameter to the null function

    bool null (class list l){
        if (l.head!=NULL){ return false;}
        return true;
    }
    

    When passing by value, you create a copy of your linked list. This copy will contain a copy of the same head pointer as the original.

    When the function returns, the parameter will be destroyed, and its destructor deletes all the nodes which is shared between the original and the copy.

    You either have to pass by reference, or define a copy constructor that creates new nodes for a copied list.

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

Sidebar

Related Questions

I have a linked list implementation and I'm experimenting with both MergeSort and QuickSort
In experimenting with this question I created an example that I utterly do not
I'm experimenting with hosting CLR (not trying to use mono for now, though I
after experimenting with js's prototypal inheritance i've found that i'm not wild about the
When experimenting with (embedded) Apache Derby DB, I noticed that a fresh database, with
Just experimenting and learning, and I know how to create a shared dictionary that
been experimenting with unordered list UL and i have seem varios examples of using
Experimenting with jQuery and trying to make a small slide show that rotates through
I'm just experimenting with mixing a native C++ (that uses MFC) static lib and
I am experimenting with Ruby and Rails. I like Ruby, but not Rails. 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.