Segmentation fault (core dumped) C error for structures

In summary, the conversation discusses a C program that uses a structure to store information about players. The program has a problem with a segmentation fault, which is found to be related to the printf() line. The program also uses the drand48() function to generate random numbers, which can range from 0 to 9999. The issue is solved by changing the way the program assigns values to the player array.
  • #1
Fb.Researcher
9
0
I have written a C program that produce a structure, give it's elements their initial values and change these values when neccessary.I can compile my program but when I try to run it,error:"segmentation fault(core dumped)" appears.I commented different parts of my program and I underentood the problem is related to my structur.The code is:
Code:
#define PN 5000 //number of players I chose
#define RN 10 //number of array's rows
#define N number of array's columns 
enum logical {W,R};

main()
{
   int j=0,i=0,logic=R,r[100000]={0};
   struct plrrecord
   {
   int n;//identity number of the player
   short int lfstat;//life status: dead or alive
   short int ch[RN][N];
   };

   struct plrrecord plr[PN];


    while(   i<PN  ) 
   {
       r[i]=(int)(drand48() * 10000);
       for( j=0 ; j<i ; j++)		 
       {    if( r[j]==r[i] )logic=W;}  
       if( logic==R )
       {
            cplr[r[i]].n=r[i];
            i++;
       }
       else { logic = R; }
   }
   for(i=0;i<PN;i++)
        printf("%i\t",cplr[i].n);/****/
return 0;
}
In my program there are many players,I give to 5000 of them a number netween 0 to 10000
which should not be repeated.I check if their number is repeated in a "while-loop".The "logic" varible helps me here.If it's value is "R" then the number is not repeated.If it's vlue is "w" then I should chose another number.The array "r" saves the random numbers chsen before.
Now I think the error is related to the line with printf,I put a /****/ in that line,because when I comment this line thre is no error anymore.
What do you thik yhe problem is?
How do you think we can solve this problem?
 
Technology news on Phys.org
  • #2
Plenty of problems, I doubt what you posted compiles.

Why do you use drand48() instead of a simple rand()? What is the maximum possible value of (int)(drand48()*10000)?
 
  • #3
100% agree with Borek: there are just way too many problems with that code snippet.
 
  • #4
I've forgotten more than I ever knew about C, but I do note that you define the player variable as 'plr' but then assign values to 'cplr'. Is that the kind of thing that might lead to pointers gleefully messing with memory?
 
  • #5
The problem with calling plr instead of cplr was not serous,it happened when I was omitting extra rows of my program to make it simpler( I did it so that parts that are not related to the problem do not botter us)
The code is:
Code:
#include <stdio.h>
#include <stdlib.h>

#define PN 10 //number of players I chose
#define RN 10 //number of array's rows
#define N 2 //number of array's columns 
enum logical {W,R};

main()
{printf("Hi");
   int j=0,i=0,logic=R,r[100000]={0};
   struct plrrecord
   {
   int n;//identity number of the player
   short int lfstat;//life status: dead or alive
   short int ch[RN][N];
   };

   printf("environmental sourc:");
   struct plrrecord cplr[PN];


    while(   i<10  ) 
   {
       r[i]=(int)(drand48() * 10000);
       for( j=0 ; j<i ; j++)		 
       {    if( r[j]==r[i] )logic=W;}  
       if( logic==R )
       {
            cplr[i].n=r[i];
            i++;
       }
       else { logic = R; }
   }printf("%i\t",i);
   for(i=0;i<PN;i++)
        printf("%i\n",cplr[i].n);
return (0);
}

Now this works.When the program chose a player, the number of that player is kept in r[].At first it was :
Code:
cplr[r[i]].n=r[i]
I do not know why chnging it this way solved the problem because the length of the array,I mean r[],is 100000 which is more than the number of players.You see for chosing a player a number between 0 and 10000 is chosen. by saying cplr[r].n==r I change an element of array which is between 0 and 10000,And drand48() chose a random number between 0 and 1.Using drand48() instead of rand() is just one way of chosing a random varible.It may not be the easiest way, but it works.
 
  • #6
What is the number of the last element in table[10000]? What is the maximum value of (int)(drand48() * 10000)?
 
  • #7
Before changing the first program,a numer like r1 between 0 and 10000-1 was saved in the 9999th element of array so the array was chosen in a way so that always the program have an empty element for it.
dran48() gives a random number beween 0 and 1(not including 1 or a number in scale [0 1) ) so drand48()*10000 gives a random number between 0 and 10000 and (int)(drand48() * 10000 ) ranges from 0 to 9999 (including 9999 or an integer number in scale [0 9999])
 
Last edited:

1. What is a segmentation fault in C?

A segmentation fault (or segfault) is a runtime error in the C programming language that occurs when a program tries to access memory that it is not allowed to access. This can happen when a program tries to access a null pointer, access memory that has already been freed, or access memory outside of its allocated bounds.

2. How do structures relate to segmentation faults in C?

In C, structures are used to group related data together. Segmentation faults can occur when trying to access elements within a structure if the structure itself or its elements are not properly allocated or initialized. This can happen if the structure is not declared or if it is declared but not initialized before trying to access its elements.

3. How can I debug a segmentation fault in my C program?

The best way to debug a segmentation fault in C is to use a debugger such as gdb. This tool allows you to step through your program line by line and see exactly where the error occurs. You can also use print statements to track the values of variables and see when and where they change unexpectedly.

4. What are some common causes of segmentation faults in C?

Some common causes of segmentation faults in C include dereferencing null pointers, accessing memory outside of an array's bounds, and using uninitialized variables. It is also possible for segmentation faults to occur due to issues with memory allocation or freeing.

5. How can I prevent segmentation faults in my C program?

To prevent segmentation faults in C, it is important to carefully manage memory allocation and deallocation. This includes properly declaring and initializing structures, using arrays and pointers correctly, and avoiding accessing memory outside of allocated bounds. It is also helpful to use debugging tools and write tests to catch potential errors before they occur.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
601
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
940
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
Replies
10
Views
955
Back
Top