hello guys

here is a problem when i was written a simple demonstrative linklist program
i got the general protection error
(**one more question i need ur guide
i have the turbo c++ compiler but this does not support
when i write #include <iostream>
using namespace std;
plz tell me about latest version of compiler where i can write above statement
)

could u guide me what this error means and when it occur and
what is the solution ?

my code is as:

#include<iostream.h>
  #include<stdio.h>
  #include<conio.h>
  #include<alloc.h>
	typedef struct node
	 {
		int value;
		struct node *next;
	 }node1;
	node1* head=NULL;
	void buildlink(node*);
	void showlink();
	void addition();
  void main()
	{
		for (int x=0; x<4; x++)
		  buildlink(head);
		showlink();
		addition();
	  }
	void buildlink(node *linknode)
	 {  int temp;
		 cout<<"enter ur node value"<<endl;
		 cin>>temp;

		 if(linknode==NULL)
		  {
			 head = (node1*)malloc(sizeof(node1*));
			 head->value=temp;
			 head->next = NULL;
			}
		  else
			{
			 while(linknode->next)
			  linknode=linknode->next;

			  linknode->next=(node1*)malloc(sizeof(node1*));
			  linknode->next->value=temp;
			  linknode->next->next=NULL;
			}
		 }
	  void showlink()
		{
		  while(head)
			{
			  cout<<head->value<<endl;
			  head=head->next;
			}
		 }
	  void addition()
		{  int x,val;
			cout<<"enter position u want to make addition and value "<<endl;
			cin>>x>>val;
			for(int y=1; y<x; y++)
			 head=head->next;[B]//problem is here[/B]
          node1* temp2 = (node*)malloc(sizeof(node1*));
			 temp2->value=val;
			 temp2->next=head->next;
			 head->next=temp2;
		}

<< moderator edit: fixed [co[u][/u]de][/co[u][/u]de] tags >>

Recommended Answers

All 7 Replies

Turbo*** is 15 year old compiler and does not conforms to most of the standards..stop using it asap....you can download the DEV C++ IDE which uses GCC as it's compiler from here
http://www.bloodshed.net/devcpp.html

thankx for first question

and what about general protection error

and what about general protection error

First download that IDE...try running in it....i am sure it will give more errors and warning(though not tested ur code yet).......there are many issues with ur code....to start with....u r mixing stdio with iostream...u should not do that....using void main which is wrong....it should be int main.....make those changes and run it.....then see what error it gives

now i have installed that id and make some change as u said
it compile successfully and when i execute it
it gives some error and say ur file have to be closed due to a error
plz look it

It looks as though you are using the node * called head to be the first node in the list. Once there is an actual node associated with the pointer you don't want to change it, unless you want to make a new head node. The reason to point this out is the following:

for(int y=1; y<x; y++)
head=head->next;//problem is here

Here you change the value of head x times through the loop. I doubt that was your intention. Since you want to insert the new item at position x, I would recommend declaring a new node * to run the list. Start by assigning head to the new pointer. Then chang the value of the new pointer to next each time through the loop. When you get where you want to go, then insert the new node.

Again, don't use <iostream.h>. Plus main() returns int.

Don't cast malloc() (in C; in C++ you should use new ). And be sure to free() (for malloc()) or delete (for new) your memory.

#include<iostream.h>
  #include<stdio.h>
  #include<conio.h>
  #include<alloc.h>

In C++, <stdio.h> should become <cstdio>. And the last two headers you shouldn't use (at least not in Dev-C++).

i am gtng general protection error in this program while i run it
plz help!!!!

#include<iostream>
using namespace std;
struct node
{
       int seq;
       node *next;
       };
       int main()
       {
           node *fresh,*temp,*ptr,*save,*start=NULL,*temp1,*start1=NULL;;
           int i=0,c,s=1,se,sq;
           while(i<5)
           {
                     if(start==NULL)
                     {
                                    fresh=new node;
                                    cout<<
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.