I have a c file which can be compiled in Linux via GCC , but when I compile it in NetBeans via Cygwin or MinGW , it doesn't work and keeps throwing a segmentation fault.
Any ideas on how to solve this ?
Much thanks.
I have a c file which can be compiled in Linux via GCC , but when I compile it in NetBeans via Cygwin or MinGW , it doesn't work and keeps throwing a segmentation fault.
Any ideas on how to solve this ?
Much thanks.
Fix the bad code.
Tim S.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
Try looking into gdb (debugger) and use it to step through your code.
Maybe get traces back to see the behavior of your code.
GDB doesn't produce any errors as it can be compiled in GCC ..
I try to see if there's any errors lurking somewhere....
post the code and we'll try to tell you what you did wrong.
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
> GDB doesn't produce any errors as it can be compiled in GCC ..
This makes no sense at all.
Compared to getting code to work correctly, getting something to "compile" is as easy as falling off a log.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Hmm maybe my phrasing of the sentence is wrong.. When I run the file in GDB, it simply runs without any errors.
What I Did
gcc -ggdb main.c
gdb ./a.out
<run>
will simply run the file with no errors.
Okay I found the source of the segmentation fault.
Hmm how do I properly check if the struct variable id is 0 ?
Non-global int variables are undefined in C from what I found out, so some compilers refuse to accept it.
Code:struct st1 { int id; }; typedef struct st1 ST ; ST da[1000], *ptr[1000]; // code... // da[] contains the struct variables // ptr[] contains the address of the da[] it points to ptr[counter] = &da[counter]; // code... // checking if id in the struct is 0 if(!(*ptr[x]).id) // seg fault in this line
Last edited by Krabiki; 08-16-2013 at 10:23 PM.
Where did you allocate memory for that pointer?
Jim
Stop guessing where the problem is, and post something that crashes!
It's easy for us to construct a working code from your snippets, but it's not telling us anything about how you're getting it wrong somewhere else.
Code:#include <stdio.h> struct st1 { int id; }; typedef struct st1 ST ; int main(void) { ST da[1000], *ptr[1000]; // code... // da[] contains the struct variables // ptr[] contains the address of the da[] it points to for(int counter=0;counter<1000;counter++) ptr[counter] = &da[counter]; // code... // checking if id in the struct is 0 for(int x=0;x<1000;x++) { if(!(*ptr[x]).id) { // seg fault in this line printf("success\n"); } } return 0; }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
A proper test case may help
No segfaults here, so I am guessing your counter or x are going out of bounds.Code:#include <stdio.h> struct st1 { int id; }; typedef struct st1 ST; int main(void) { ST da[1000], *ptr[1000]; int counter, x; // code... // da[] contains the struct variables // ptr[] contains the address of the da[] it points to const int len = sizeof da / sizeof da[0]; for (counter = 0; counter < len; counter++) { ptr[counter] = &da[counter]; // da[counter].id = 0; // checking if id in the struct is 0 x = counter; if(!(*ptr[x]).id) { // seg fault in this line puts("Not 0"); } } return 0; }
Hmm I think setting the struct int variable id to 0 will solve the issue, but I can't figure out how to do it.
Sample code, working code snippet showing the seg fault at the line in which it checks if struct in variable is 0.
Code:int x; int val_id = 1; int counter; struct st1 { int id; }; typedef struct st1 ST ; ST *ptr[1000], da[1000]; //ST da[1000]= { .id = 0 }; // code... // da[] contains the struct variables // ptr[] contains the address of the da[] it points to int main(){ for(counter=0;counter<1000;counter++){ ptr[counter] = &da[counter]; } for(x = 0; x<counter; x++) { if(!(*ptr[x]).id) { // Assign each ptr[x] with id (*ptr[x]).id=val_id; } } // code... // checking if id in the struct is 0 if(!(*ptr[x]).id) // seg fault in this line { printf("success\n"); } return 0; }
Line 22, what is the value of counter?
Edit: Line 33, what is the value of x?
Last edited by SirPrattlepod; 08-17-2013 at 01:33 AM.