Segmentation fault while structure variable declaration

This is a discussion on Segmentation fault while structure variable declaration within the C Programming forums, part of the General Programming Boards category; I'm facing a segmentation fault(core) at the step where a structure variable is declared. PFB the code snippet: insert Code: ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    12

    Segmentation fault while structure variable declaration

    I'm facing a segmentation fault(core) at the step where a structure variable is declared.

    PFB the code snippet:
    insert
    Code:
    struct LOCAL_UTILS_struct_
    {
        tbaAmtType          bigLimitAmt;
        longDescType        longDesc;
        tba800ErrorMsgType  mlStr01;
    };
    typedef struct LOCAL_UTILS_struct_ LOCAL_UTILS_STRUCT_TYPE;
    
    
    Function ()
    {
                  LOCAL_UTILS_STRUCT_TYPE locUtils;
    Above line is giving segmentation fault; but not always. When structure eliminated, and structure variables declared separately inside function, core didn’t occur.
    Also, I declared a dummy string above this structure declaration of size 30000. Then the core didn’t occur.

    Please suggest probable reason for this behaviour.

    Latest info:
    While analysis, it was found that the structure name LOCAL_UTILS_STRUCT_TYPE was also present in one of the header files. But that header file is not present in my current source. Can this be the probable reason?
    Last edited by rk211083; 06-22-2010 at 03:48 AM. Reason: Got a clue.......

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Are you sure that it's the line that gives segfault? What's the sizeof your_struct?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    Quote Originally Posted by rk211083 View Post
    I'm facing a segmentation fault(core) at the step where a structure variable is declared.

    PFB the code snippet:
    insert
    Code:
    struct LOCAL_UTILS_struct_
    {
        tbaAmtType          bigLimitAmt;
        longDescType        longDesc;
        tba800ErrorMsgType  mlStr01;
    };
    typedef struct LOCAL_UTILS_struct_ LOCAL_UTILS_STRUCT_TYPE;
    
    
    Function ()
    {
                  LOCAL_UTILS_STRUCT_TYPE locUtils;
    Above line is giving segmentation fault; but not always. When structure eliminated, and structure variables declared separately inside function, core didn’t occur.
    Also, I declared a dummy string above this structure declaration of size 30000. Then the core didn’t occur.

    Please suggest probable reason for this behaviour.
    You have not provided a type, just a definition, the correct declaration is:

    Code:
    struct
    {
        tbaAmtType          bigLimitAmt;
        longDescType        longDesc;
        tba800ErrorMsgType  mlStr01;
    }LOCAL_UTILS_struct_;
    If you want to typedef it, and make at tag and pointer type at the same time, the statement is:

    Code:
    typedef struct LOCAL_UTILS_struct_tag
    {
        tbaAmtType          bigLimitAmt;
        longDescType        longDesc;
        tba800ErrorMsgType  mlStr01;
    }LOCAL_UTILS_struct_t, *pLOCAL_UTILS_struct_t;
    The tag can be used to declare this type of struct inside the struct, the type is the LOCAL_UTILS_struct_t (the _t is just my decoration of typedef types), and the pointer type is pLOCAL_UTILS_struct_t (here both the p and _t are my decoration).
    Last edited by glennik; 06-22-2010 at 03:37 AM.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    12

    Got one clue......

    Quote Originally Posted by Bayint Naung View Post
    Are you sure that it's the line that gives segfault? What's the sizeof your_struct?
    Yes, I used dbx to find out the exact line.

    While analysis, it was found that the structure name LOCAL_UTILS_STRUCT_TYPE was also present in one of the header files. But that header file is not present in my current source. Can this be the probable reason?

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    12

    Got one clue.....

    Quote Originally Posted by glennik View Post
    You have not provided a type, just a definition, the correct declaration is:

    Code:
    struct
    {
        tbaAmtType          bigLimitAmt;
        longDescType        longDesc;
        tba800ErrorMsgType  mlStr01;
    }LOCAL_UTILS_struct_;
    If you want to typedef it, and make at tag and pointer type at the same time, the statement is:

    Code:
    typedef struct LOCAL_UTILS_struct_tag
    {
        tbaAmtType          bigLimitAmt;
        longDescType        longDesc;
        tba800ErrorMsgType  mlStr01;
    }LOCAL_UTILS_struct_t, *pLOCAL_UTILS_struct_t;
    The tag can be used to declare this type of struct inside the struct, the type is the LOCAL_UTILS_struct_t (the _t is just my decoration of typedef types), and the pointer type is pLOCAL_UTILS_struct_t (here both the p and _t are my decoration).

    Thanks for ur response.
    Above declaration in my code is also correct, it works correctly.

    While analysis, it was found that the structure name LOCAL_UTILS_STRUCT_TYPE was also present in one of the header files. But that header file is not present in my current source. Can this be the probable reason?

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You need to show more code. The only way that code you've posted is going to cause a segfault is if the function is called recursively and the stack overflows. It's also possible that the stack is already full when you're calling the function which could explain the segfault.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    If a type of that name is already defined, you should get a compiler warning / error (use the pedantic flag). If the header where its defined is not used in your project, it should not be a problem, but you should always take some precautions to make sure that you never get type and function names mixed up or redefined. For instance, be careful about the scope of the declaration and use somewhat unique names (eg. not LOCAL_UTILS, its to general).

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    12

    Clarification

    Quote Originally Posted by glennik View Post
    If a type of that name is already defined, you should get a compiler warning / error (use the pedantic flag). If the header where its defined is not used in your project, it should not be a problem, but you should always take some precautions to make sure that you never get type and function names mixed up or redefined. For instance, be careful about the scope of the declaration and use somewhat unique names (eg. not LOCAL_UTILS, its to general).

    The header file where same structure name is present is included in some other C sources under same project under which my particular C source is present. But not sure whether any of those sources come during flow before my C source.

    Just few queries regarding this structure definition and scope:
    i. If 2 C sources have structure definitions and usage with same name, and both are linked, can this cause an issue?
    ii. If a header file contains a structure and its included in some C sources; now some structure is defined locally in some C source with same name, but that header is not included in this C file, but this source is in the same project/exe as those C sources which have header included, then can this create problem?

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    27
    Generally, segmentation fault error is the cause of like these:
    the variable is out of control with forever addings.
    the pointer points to the wrong memery area.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    When the objects a linked, a global symbol table is created in the executable. In this symbol table, the exported symbols have capital letters, while non exported symbols have small letters. Look at your symbol table and see if you got several entries. However, I wonder how you manage to link this, if thats the real problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault?
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-10-2008, 07:26 AM
  2. Segmentation Fault :(
    By DarkDot in forum C++ Programming
    Replies: 39
    Last Post: 04-07-2007, 04:16 AM
  3. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 04:50 PM
  4. Segmentation fault
    By Buckshot in forum C++ Programming
    Replies: 14
    Last Post: 06-23-2005, 08:20 AM
  5. segmentation fault - pointers/functions
    By p1c1078 in forum C Programming
    Replies: 15
    Last Post: 04-22-2003, 05:46 PM

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