Thread: Segmentation Fault of death

  1. #31
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    components.ni = ∋
       components.xbpe = &xbpe;
    Setting pointers to point to local variables is generally a Bad Idea. I have no idea why you designed these as pointers (are they supposed to eventually turn into arrays?) but I think you'll be happier if you just malloc some space for these guys.

  2. #32
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, the first issue is that you are not declaring any components type variable like:

    Code:
    struct components var_components;
    From what I gather components is a DATA TYPE not a VARIABLE. Correct?

  3. #33
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    tabstop:
    I guess what I'm trying to do is pass the address of the components in pass1 to breakup so that breakup can tokenize the sourceline and assign the correct component parts into the components in pass1, does that make sense? The structs are provided and cannot be changed.

    claudiu:
    I'm creating components in pass1.c

    Code:
    struct toklist
    {
       char *lbl;
       char *opc;
       char *opr1;
       char *opr2;
       int *ni;
       int *xbpe;
    } components;

  4. #34
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I take it ni is then supposed to stand for "number of items"? And passed by pointer so that the function can change it. But you still have to have somewhere for components.ni to point to, and I'm not sure a local variable will do. (That address isn't legal while the function isn't running, so during the call to breakup that variable is invalid, and it will be invalid again when the function ends and returns to whence it came.)

  5. #35
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    ni actually represents 2 bits in SIC/XE assembly object code. opcode + nixbpe

  6. #36
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, then you need to allocate memory for all of the members of components.

    Such as:

    Code:
    lbl = malloc(sizeof(char) * 100); /*allocate a string of 100 chars; lbl points to it */
    ni = malloc(sizeof(int)); /* allocate 1 int */
    xbpe = malloc(sizeof(int)); /*allocate 1 int*/
    Allocating memory for a struct (dynamic or static) doesn't also allocate memory for its members since the compiler has no way of knowing HOW MUCH memory you actually need.
    Last edited by claudiu; 04-09-2010 at 09:44 PM.

  7. #37
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    I'll give that a shot but if I remember correctly the professor mentioned that we shouldn't have to allocate memory. Should I be allocating memory in both functions or just the calling function?

  8. #38
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You should allocate memory before you use components. As I said yesterday you can't dereference a pointer that doesn't point anywhere (i.e. you can't just declare int *p; and then expect to be able to put a value in *p.) When you allocate memory using malloc() the pointer will point to a VALID memory address allocated on the heap.

  9. #39
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    I thought that was why I was using the following code:

    Code:
    char label[81], opcode[81], opr1[81], opr2[81];
    int ni, xbpe;
    components.lbl = label;
    components.opc = opcode;
    components.opr1 = opr1;
    components.opr2 = opr2;
    components.ni = ∋
    components.xbpe = &xbpe;
    Does that not allocate memory for the pointer to point to? I guess I'm not fully grasping all of this

  10. #40
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    when allocating the memory shouldn't I be doing the following:

    Code:
    components.lbl = malloc(sizeof(char) * 100); /*allocate a string of 100 chars; lbl points to it */

  11. #41
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x00000034c3470db0 in strcpy () from /lib64/tls/libc.so.6
    (gdb) bt
    #0  0x00000034c3470db0 in strcpy () from /lib64/tls/libc.so.6
    #1  0x00000000004029f3 in breakup (
        sourceline=0x504ee0 ".CHECK   getsource removal of initial blank lines", 
        components=
          {lbl = 0x7fbffff790 ".CHECK", opc = 0x7fbffff750 "getsource", opr1 = 0xab8aa1e <Address 0xab8aa1e out of bounds>, opr2 = 0x40070d "strcspn", ni = 0x0, xbpe = 0x34c3207d8e}) at breakup.c:128
    #2  0x0000000000401ee3 in pass1 () at pass1.c:63
    #3  0x0000000000400ed1 in main ()

  12. #42
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Wow, I just realized that the line it's trying to breakup shouldn't be getting passed at all from pass1 as it is a comment. Let me try to fix that first and then see if the out of bounds issues are resolved

  13. #43
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Can you tell me what's wrong with this statement?

    Code:
     while(strcmp(sourcelist[Scnt].substr(0, 1), ".") == 0)
          Scnt++;
    I'm trying to increment the Scnt counter for as long as there's a "." in the first place of the string. But I get the following error:

    Code:
    pass1.c: In function `pass1':
    pass1.c:60: error: request for member `substr' in something not a structure or union

  14. #44
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    hmmmm other than the fact that it's C++ lol is there a substr function in C?

  15. #45
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes, check strstr().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM