0

When I ran the below code in dev C++ the output is empty, Even though online compilers are doing well. Is there a specific error in my code or do I have change dev C++ settings

#include<iostream>
#include<vector>
using namespace std;

class node //node definition
{
    public:
        int data;
        node* next;
        node(int value=0)
        {
            data=value;
            
        }
};

node* insert(node* head,int data)  //node insertion 
{
    node* ins=new node(data);
    if(head==NULL)
    {
        return ins;
    }
    else
    {
        node* ptr=head;
        while(head->next!=NULL)
        head=head->next;
        
        head->next=ins;
        ins->next=NULL;
        return ptr;
    }
    
}

void print(node* head)  //printing the values of linked list
{
    while(head!=NULL)
    {
        cout<<head->data<<" ";
        head=head->next;
    }
}

int main()
{
    vector <int> a{1,2,3,6,8};
    node* list=NULL;
    for(int x:a)
    {

        list=insert(list,x);
    }
    print(list);
}

Output:

Can anyone resolve the issue?

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • 1
    Related question: [`endl` and flushing the buffer](https://stackoverflow.com/questions/4751972/endl-and-flushing-the-buffer). Also please consider reading [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Stef Jul 05 '21 at 10:23
  • Long story short: I recommend adding `std::cout << std::endl;` at the end of the `print` function. – Stef Jul 05 '21 at 10:25
  • 2
    Short story: You don't default-initialize the `next` member of your structure. So, when you call `insert` the first time, the `next` member of your `new` structure could be anything at all. The online compiler(s) may accidentally give that a `nullptr` (zero) value, but you can't rely on it. – Adrian Mole Jul 05 '21 at 10:27
  • Additionally I recommend to check, if the exe-file that you are calling, is the correct one. Maybe it will be in some debug or other sub directory. Please check your compiler for the correct output path settings. Only maybe . . . – A M Jul 05 '21 at 10:28
  • No head no print, you don't assign to head when head is null. – Surt Jul 05 '21 at 15:04

1 Answers1

0

You forgot to initialize the next member of a newly allocated node, hence your list has an undefined (and potentially illegal) pointer value at the end after the first node insertion.

From that point on the behavior of the program is undefined.

A fix is completeing the constructor:

        node(int value=0)
        {
            data = value;
            next = nullptr;
        }
CiaPan
  • 9,381
  • 2
  • 21
  • 35
  • Thanks for you help. But I am still wondering why online compilers executed properly even when the next is not initialized with nullptr? – manas chinta Jul 06 '21 at 11:19
  • @manaschinta An _Undefined_ Behavior is a behavior that is Undefined. Neither the language standard, nor specific compiler, nor the runtime library specification or the execution environment _defines_ what should or may happen. The program may crash immediately or it may work pretty well. It may also pretend to work, but silently destroy its vital data somewhere else and crash at some apparently unrelated operation. The behavior needn't be predictable or even repeatable. – CiaPan Jul 07 '21 at 10:06
  • @manaschinta _Possibly_ online compilers use a clearing allocator, which initializes memory with zeros, thus causing pointer members be `nullptr` (which, AFAIK, is itself implementation-dependent). But that is not guaranteed and, as you experienced it, needn't work same in different environments or on different machines. It may also change without prior notice, because it is not even defined the particular behavior will remain constant. – CiaPan Jul 07 '21 at 10:07