Like Tree9Likes
  • 3 Post By stahta01
  • 3 Post By Salem
  • 1 Post By SirPrattlepod
  • 2 Post By SirPrattlepod

Compiler errors.

This is a discussion on Compiler errors. within the C Programming forums, part of the General Programming Boards category; I have a c file which can be compiled in Linux via GCC , but when I compile it in ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35

    Compiler errors.

    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.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,543
    Fix the bad code.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    14
    Try looking into gdb (debugger) and use it to step through your code.
    Maybe get traces back to see the behavior of your code.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    GDB doesn't produce any errors as it can be compiled in GCC ..

    I try to see if there's any errors lurking somewhere....

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    post the code and we'll try to tell you what you did wrong.
    Code:
    namespace life
    {
        const bool change = true;
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    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.

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    141
    Quote Originally Posted by Krabiki View Post
    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.
    Why not make it easier for someone to assist you by actually posting the code?
    When you compile and run the code, does the output actually do what you want it to do?

  9. #9
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    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.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    Where did you allocate memory for that pointer?

    Jim

  11. #11
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    Quote Originally Posted by jimblumberg View Post
    Where did you allocate memory for that pointer?

    Jim
    I don't think I did any memory allocation for the pointer =/

    The ptr*[1000] actually points to the address of another array which is storing the struct.

    Updated the code in the previous post.
    Last edited by Krabiki; 08-16-2013 at 10:23 PM. Reason: Typo.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    A proper test case may help

    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;
    }
    No segfaults here, so I am guessing your counter or x are going out of bounds.
    Salem likes this.

  14. #14
    Registered User
    Join Date
    Apr 2013
    Location
    Still looking..
    Posts
    35
    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;
    }

  15. #15
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    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.
    Salem and stahta01 like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler errors
    By Beowolf in forum C++ Programming
    Replies: 12
    Last Post: 10-31-2007, 09:25 AM
  2. Compiler errors
    By ICool in forum C Programming
    Replies: 9
    Last Post: 10-18-2007, 08:01 AM
  3. compiler errors
    By Draco in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2003, 11:08 AM
  4. Compiler errors
    By JaWiB in forum Windows Programming
    Replies: 3
    Last Post: 03-02-2003, 12:28 AM
  5. Compiler errors
    By BellosX in forum C Programming
    Replies: 2
    Last Post: 09-21-2001, 03:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21