473,388 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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!!

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. main()
  5. {
  6.     struct boy
  7.     {
  8.         char name[100];
  9.         int marks;
  10.     };
  11.     struct boy a[100];
  12.     int no,i,l,m,k,t;
  13.     char nam[100];
  14.     printf("Enter no of students");
  15.     scanf("%d", &no);
  16.     for(i=0;i<no;i++)
  17.     {
  18.         printf("Enter Name");
  19.         gets(a[i].name);
  20.         printf("Enter Marks");
  21.         scanf("%d", a[i].marks);
  22.     }
  23.     for(l=1;i<=no-1;l++)
  24.     {
  25.         for(m=0;m<no-l;m++)
  26.         {
  27.             if(a[m].marks>a[m+1].marks)
  28.             {
  29.                 strcpy(nam,a[m].name);
  30.                 strcpy(a[m].name,a[m+1].name);
  31.                 strcpy(a[m+1].name,nam);
  32.                 t=a[m].marks;
  33.                 a[m].marks=a[m+1].marks;
  34.                 a[m+1].marks=t;
  35.             }
  36.         }
  37.     }
  38.     for(k=0;k<no;k++)
  39.     {
  40.         puts(a[k].name);
  41.         printf("%d", a[k].marks);
  42.     }
  43.     getch();
  44.     return 0;
  45. }
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"
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. main()
  4. {
  5.     struct book
  6.     {
  7.         int pages;
  8.         int no;
  9.     };
  10.     struct book b[100];
  11.     int i,n,j;
  12.     scanf("%d", &n);
  13.     for(i=0;i<n;i++)
  14.     {
  15.         printf("Enter pages");
  16.         scanf("%d", b[i].pages);
  17.         printf("Enter Book Number");
  18.         scanf("%d", b[i].no);
  19.     }
  20.     for(j=0;j<n;j++)
  21.     {
  22.         printf("Pages are %d", b[j].pages);
  23.         printf("Number is %d", b[j].no);
  24.     }
  25.     getch();
  26.     return 0;
  27. }
Jun 20 '11 #1

✓ answered by puneetsardana88

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

Expand|Select|Wrap|Line Numbers
  1.             printf("Enter pages");
  2.             scanf("%d", &b[i].pages);
  3.             printf("Enter Book Number");
  4.             scanf("%d", &b[i].no);
  5.  
This works fine....

10 9415
donbock
2,426 Expert 2GB
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.
Jun 20 '11 #2
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)
Jun 21 '11 #3
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!!
Jun 21 '11 #4
For the second question you are forgetting to add & while inputting

Expand|Select|Wrap|Line Numbers
  1.             printf("Enter pages");
  2.             scanf("%d", &b[i].pages);
  3.             printf("Enter Book Number");
  4.             scanf("%d", &b[i].no);
  5.  
This works fine....
Jun 22 '11 #5
@Puneet Thanks man, it worked!! I really was being stupid overlooking the obvious mistake!!
Jun 22 '11 #6
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??
Jun 22 '11 #7
Expand|Select|Wrap|Line Numbers
  1.     #include<stdio.h>
  2.     #include<conio.h>
  3.     #include<string.h>
  4.     main()
  5.     {
  6.         struct boy
  7.         {
  8.             char name[100];
  9.             int marks;
  10.         };
  11.         struct boy a[100];
  12.         int no,i,l,m,k,t;
  13.         char nam[100];
  14.         printf("Enter no of students");
  15.         scanf("%d", &no);
  16.         for(i=0;i<no;i++)
  17.         {
  18.             printf("Enter Name  ");
  19.             scanf("%s",(a[i].name));
  20.             printf("Enter Marks  ");
  21.             scanf("%d", &a[i].marks);
  22.         }
  23.         for(l=1;i<=no-1;l++)
  24.         {
  25.             for(m=0;m<no-l;m++)
  26.             {
  27.                 if(a[m].marks>a[m+1].marks)
  28.                 {
  29.                     strcpy(nam,a[m].name);
  30.                     strcpy(a[m].name,a[m+1].name);
  31.                     strcpy(a[m+1].name,nam);
  32.                     t=a[m].marks;
  33.                     a[m].marks=a[m+1].marks;
  34.                     a[m+1].marks=t;
  35.                 }
  36.             }
  37.         }
  38.         for(k=0;k<no;k++)
  39.         {
  40.             puts(a[k].name);
  41.             printf("%d", a[k].marks);
  42.         }
  43.         getch();
  44.         return 0;
  45.     }
  46.  
  47.  
Works fine...Check out the changes made
Jun 22 '11 #8
@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?
Jun 23 '11 #9
@ rusty uts

The code that I posted works fine for me. I am running in code block mingw compiler. Please check
Jun 26 '11 #10
@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!!
Jun 27 '11 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Paiman Allage | last post by:
Hello all, I hope you can help me. I have developed an ".NET" application and started to deploy the application. However, I am haveing a problem, where when I compile the program on my laptop,...
0
by: Mullai | last post by:
0Hi, My exe comes out with two types of errors like : 1.PG1609VV caused an invalid page fault in module KERNEL32.DLL at 017f:bff9dfff. Registers: EAX=07fbfe38 CS=017f EIP=bff9dfff...
37
by: dmoran21 | last post by:
I am a mathematician trying to write a program in C to do some curve fitting. When I get to the point where I attempt to enter data in my arrays, I get a General Protection Exception error message....
2
by: akmkat | last post by:
When I try to compile the following code with N = 1024, "General Protection Exception" occurs, but wehn N < 20, there is no problem. I need N = 1024, what'll I do? Plz help anyone #include...
1
by: manhquynh8x | last post by:
I am programming a data compession program. It is used to compress a text file. After run program, i see this message: Exception 13: General protection fault at o1A7.. The compiler is Turbo C++...
2
by: subhasree | last post by:
Hi, I am a student of mathematics and quite new to programming in C. I am trying to write a code to construct an array which at every step has to compare the value of the current element with all...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.