The code should result in segmentation fault due to shallow copy but instead it allows printing the address of the the head node for list2 when printAll() is called for list2 in main().What should be done to get the expected behaviour ?Also please explain why is it happening ? I am a beginner.Thanks in advance.
>>>> when tried with #pragma pack(1) it resuted in the first output
>>>> Without #pragma pack(1) it resulted in the second output
Here is the code i wrote.
The code is for creating an object (list1) of SLL class(singly linked list) and copying it to another object(list2) of SLL class.
#include <iostream>
using namespace std;
//#pragma pack(1)
class Node{
public:
char letter;
Node *next;
};
class SLL{
private:
Node *head, *tail;
public:
SLL(){
head = NULL;
tail = NULL;
}
void printAll();// for printing the list
void insertNewNode(char item);// for insertion at head
//function for deletion at head
void deleteAtHead(){
Node *tmp;
tmp = this->head;
this->head = this->head->next;
free(tmp);
}
};
//it is for printing a singly linked list
void SLL::printAll(){
Node *p;
p = head;
cout<<"VALUE "<<" "<<" ADDRESS"<<endl;
while(p!=NULL){
cout <<p->letter << "--------------- "<<p<<endl;
p = p->next;
}
}
void SLL::insertNewNode(char item){
Node* temp;
temp = (Node *)malloc(sizeof(Node));
temp->letter = item;
temp->next = head;
head = temp;
}
int main(){
SLL list1;
list1.insertNewNode('D');
list1.insertNewNode('C');
list1.insertNewNode('B');
list1.insertNewNode('A');
cout<<"PRINTING LIST1"<<endl;
list1.printAll();
cout<<""<<endl;
cout<<"SHALLOW COPY INVOKED"<<endl;
SLL list2 = list1;
cout<<""<<endl;
cout<<"PRINTING LIST2"<<endl;
list2.printAll();
list1.deleteAtHead();
cout<<""<<endl;
cout<<" LIST1 AFTER ITS HEAD DELETION"<<endl;
list1.printAll();
cout<<""<<endl;
cout<<" LIST2"<<endl;
list2.printAll(); // as soon as this is executed it should result in runtime error
return 0;
}
>>>>>>> Output1:
PRINTING LIST1
VALUE ADDRESS
A ------------- 0x5578d6f872e0
B ------------- 0x5578d6f872c0
C ------------- 0x5578d6f872a0
D ------------- 0x5578d6f87280
SHALLOW COPY INVOKED
PRINTING LIST2
VALUE ADDRESS
A ------------- 0x5578d6f872e0
B ------------- 0x5578d6f872c0
C ------------- 0x5578d6f872a0
D ------------- 0x5578d6f87280
LIST1 AFTER ITS HEAD DELETION
VALUE ADDRESS
B ------------- 0x5578d6f872c0
C ------------- 0x5578d6f872a0
D ------------- 0x5578d6f87280
LIST2
VALUE ADDRESS
--------------- 0x5578d6f872e0
>>>>>> Output2:
PRINTING LIST1
VALUE ADDRESS
A ------------- 0x55baac1032e0
B ------------- 0x55baac1032c0
C ------------- 0x55baac1032a0
D ------------- 0x55baac103280
SHALLOW COPY INVOKED
PRINTING LIST2
VALUE ADDRESS
A ------------- 0x55baac1032e0
B ------------- 0x55baac1032c0
C ------------- 0x55baac1032a0
D ------------- 0x55baac103280
LIST1 AFTER ITS HEAD DELETION
VALUE ADDRESS
B ------------- 0x55baac1032c0
C ------------- 0x55baac1032a0
D ------------- 0x55baac103280
LIST2
VALUE ADDRESS
--------------- 0x55baac1032e0
B--------------- 0x55baac1032c0
C--------------- 0x55baac1032a0
D--------------- 0x55baac103280