General protection fault/stack fault

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rusty uts
    New Member
    • Jun 2011
    • 7

    General protection fault/stack fault

    Well I had an assignment to sort an array of structures so i went about trying but always some strange fault come about. Now not only does the main program fail but also another program. What i want to know is that is it a problem in my code, or something else is happening?? Below are the two programs!!

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    main()
    {
    	struct boy
    	{
    		char name[100];
    		int marks;
    	};
    	struct boy a[100];
    	int no,i,l,m,k,t;
    	char nam[100];
    	printf("Enter no of students");
    	scanf("%d", &no);
    	for(i=0;i<no;i++)
    	{
    		printf("Enter Name");
    		gets(a[i].name);
    		printf("Enter Marks");
    		scanf("%d", a[i].marks);
    	}
    	for(l=1;i<=no-1;l++)
    	{
    		for(m=0;m<no-l;m++)
    		{
    			if(a[m].marks>a[m+1].marks)
    			{
    				strcpy(nam,a[m].name);
    				strcpy(a[m].name,a[m+1].name);
    				strcpy(a[m+1].name,nam);
    				t=a[m].marks;
    				a[m].marks=a[m+1].marks;
    				a[m+1].marks=t;
    			}
    		}
    	}
    	for(k=0;k<no;k++)
    	{
    		puts(a[k].name);
    		printf("%d", a[k].marks);
    	}
    	getch();
    	return 0;
    }
    on running it it gives that "stack fault in module toolhelp.dll at 0001:2239"

    Again the simple program to retrieve something from an array of structure fails saying "general protection exception 0x2557:0x37EB processor fault"
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	struct book
    	{
    		int pages;
    		int no;
    	};
    	struct book b[100];
    	int i,n,j;
    	scanf("%d", &n);
    	for(i=0;i<n;i++)
    	{
    		printf("Enter pages");
    		scanf("%d", b[i].pages);
    		printf("Enter Book Number");
    		scanf("%d", b[i].no);
    	}
    	for(j=0;j<n;j++)
    	{
    		printf("Pages are %d", b[j].pages);
    		printf("Number is %d", b[j].no);
    	}
    	getch();
    	return 0;
    }
    Last edited by Meetee; Jun 20 '11, 04:40 AM. Reason: Please add code tags while posting a code i.e. [CODE][/CODE]
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    In general, your programs should protect themselves from somebody entering sizes (no,n) that are bigger than your preallocated arrays; or from somebody entering a string via gets that overflows your preallocated string variables.

    Presumably you wouldn't have used test cases that overflow these limits. The errors you're getting sound pretty ominous. I suggest you contact your compiler's technical support folks to find out what these error messages mean.

    Comment

    • rusty uts
      New Member
      • Jun 2011
      • 7

      #3
      Well I am using Turbo C++ 4.5. However I havent used cases where sizes of variables are greater than their preallocated arrays. I am at a total confusion, is this the operating system's fault? If u have that compiler can u just run this program and see whether it works or not, as I am begining to think that something is wrong with my Windows XP and not the code(Dont know why I feel that way probably as my machine hasnt been formatted for like 3 yrs)

      Comment

      • rusty uts
        New Member
        • Jun 2011
        • 7

        #4
        The first program is to sort a number of arrays of structures on the basis of the marks. The guy with lowest marks gets their names and marks printed first, then the one with higher marks and so on!! The second is just a simple program to input data in a structure and reprint them on the screen, BTW if u feel there is something wrong with the code then please do let me know!! Thanks in advance!!

        Comment

        • puneetsardana88
          New Member
          • Aug 2009
          • 57

          #5
          For the second question you are forgetting to add & while inputting

          Code:
                      printf("Enter pages");
                      scanf("%d", &b[i].pages);
                      printf("Enter Book Number");
                      scanf("%d", &b[i].no);
          This works fine....

          Comment

          • rusty uts
            New Member
            • Jun 2011
            • 7

            #6
            @Puneet Thanks man, it worked!! I really was being stupid overlooking the obvious mistake!!

            Comment

            • rusty uts
              New Member
              • Jun 2011
              • 7

              #7
              However the first one still doesnt respond, even when the "&" case is fixed. It causes problems in toolhelp.dll or something, anybody knows why this is happening??

              Comment

              • puneetsardana88
                New Member
                • Aug 2009
                • 57

                #8
                Code:
                    #include<stdio.h>
                    #include<conio.h>
                    #include<string.h>
                    main()
                    {
                        struct boy
                        {
                            char name[100];
                            int marks;
                        };
                        struct boy a[100];
                        int no,i,l,m,k,t;
                        char nam[100];
                        printf("Enter no of students");
                        scanf("%d", &no);
                        for(i=0;i<no;i++)
                        {
                            printf("Enter Name  ");
                            scanf("%s",(a[i].name));
                            printf("Enter Marks  ");
                            scanf("%d", &a[i].marks);
                        }
                        for(l=1;i<=no-1;l++)
                        {
                            for(m=0;m<no-l;m++)
                            {
                                if(a[m].marks>a[m+1].marks)
                                {
                                    strcpy(nam,a[m].name);
                                    strcpy(a[m].name,a[m+1].name);
                                    strcpy(a[m+1].name,nam);
                                    t=a[m].marks;
                                    a[m].marks=a[m+1].marks;
                                    a[m+1].marks=t;
                                }
                            }
                        }
                        for(k=0;k<no;k++)
                        {
                            puts(a[k].name);
                            printf("%d", a[k].marks);
                        }
                        getch();
                        return 0;
                    }
                Works fine...Check out the changes made

                Comment

                • rusty uts
                  New Member
                  • Jun 2011
                  • 7

                  #9
                  @Puneet Thanks for ur effort, but it doesnt seem to run. This toolhelp.dll stack fault is still happening. U ran it in ur PC right? If it did run then my Operating System is probably at fault. BTW, the only differences are the "&", and the scanf("%s" instead of the gets() part right? The rest seems to be the same. Anyway if it worked on ur PC, can u tell me why we cant use gets() here? I mean scanf("%s" and gets() would do the same thing right? Only difference that gets() will automatically put the '\0' character at the end while scanf will not, Right?

                  Comment

                  • puneetsardana88
                    New Member
                    • Aug 2009
                    • 57

                    #10
                    @ rusty uts

                    The code that I posted works fine for me. I am running in code block mingw compiler. Please check

                    Comment

                    • rusty uts
                      New Member
                      • Jun 2011
                      • 7

                      #11
                      @Puneet Thanks man, turns out that my turbo C++ 4.5 was corrupted or something, started using code::Blocks, works great, And its better as well, so thanks again!!

                      Comment

                      Working...