general protection error

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2005
Posts: 61
Reputation: johnray31 is an unknown quantity at this point 
Solved Threads: 0
johnray31 johnray31 is offline Offline
Junior Poster in Training

general protection error

 
0
  #1
Nov 16th, 2005
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;//problem is here
          node1* temp2 = (node*)malloc(sizeof(node1*));
			 temp2->value=val;
			 temp2->next=head->next;
			 head->next=temp2;
		}
<< moderator edit: fixed [code][/code] tags >>
Last edited by Dave Sinkula; Nov 16th, 2005 at 11:25 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: general protection error

 
0
  #2
Nov 16th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 61
Reputation: johnray31 is an unknown quantity at this point 
Solved Threads: 0
johnray31 johnray31 is offline Offline
Junior Poster in Training

Re: general protection error

 
0
  #3
Nov 16th, 2005
thankx for first question

and what about general protection error
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: general protection error

 
0
  #4
Nov 16th, 2005
Originally Posted by johnray31
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 61
Reputation: johnray31 is an unknown quantity at this point 
Solved Threads: 0
johnray31 johnray31 is offline Offline
Junior Poster in Training

Re: general protection error

 
0
  #5
Nov 16th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,686
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: general protection error

 
0
  #6
Nov 16th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: general protection error

 
0
  #7
Nov 25th, 2005
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++).
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: vibhu1 is an unknown quantity at this point 
Solved Threads: 0
vibhu1 vibhu1 is offline Offline
Newbie Poster
 
-1
  #8
27 Days Ago
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<<
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC