Thread: Bus error

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    40

    Bus error

    Can anyone tell me what a bus error is, what causes it, how to get around it. I am getting one for the following line of code:

    strcpy(fmt, "%-1s");

    where fmt is declared as
    char fmt[9];

    Thanks

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    That should compile fine. What about the rest of the code? Could you post the code in context?
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    40
    It is a bit complicated. I have included the files for the program so far. It is attempting to create an avl tree with a server program and share it with a client program via shared memory. To compile it you need to create to do:
    cc -c avl.c
    cc -c avl_test_server.c
    cc -o avl_test_server avl_test_server.o avl.o

    cc -c avl_test_client.c
    cc -o avl_test_client avl_test_client.o avl.o

    The error happens in the avlprint function in avl_test_client.c. There is an if else statement and it happens in the else part where the strcpy is.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Not answering your question, but why are you setting up the fmt like that? It looks to me like you want to dynamically defined the width for the printf(), is this correct? If so, there's a better way. Use the * modifier, like this
    >printf (">%*s<\n", len, stuff);

    Here's an example.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char stuff[] = "testing";
    	int len=25;
    	
    	printf (">%*s<\n", len, stuff);
    	return 0;
    }
    Also, if your application is crashing, it's possible that the output from your debug style printf() statements is buffered, and when the app halts, the buffer isn't flushed. What I mean is, it might not be crashing exactly where you think. Try adding a few fflush(stdout) statements to be sure.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    A Bus Error is generated when your program tries to access some illegal memory location in your system.
    The statement strcpy(); looks perfect, so I don't think it's got to do anything with this error.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?segfault
    http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?SIGBUS

    Two main problems
    1. why on earth are you still writing in K&R C ?
    Does this explain why you can't use %* in your printf statements?

    In addition, the fact that
    (*action)(tree->data, PREORDER, node, level, tree->bal);
    is a function call without a proper prototype exposes you to some awkward type promotion rules which could well clash with the declaration of the avlprint function in avl_test_client.c

    2. I highly doubt that pointers created in one process (that which writes to the shm) will be in any way valid or meaningful when used in another process (that which reads shm). This is especially true of those pointers which point to outside the shm (ie, those returned by malloc).

    Perhaps you should do some simple pointer tests in shm before building a whole tree in there.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    40
    Thank you all for your comments. This is by way of an update on progress to date. I have taken another tack and decided to prove that the client process can attach to the shared memory area and print out the avl descriptor structure that is held in there. Which it does. The avl descriptor structure contains a pointer to the avl node structure. Next I proved that the client process can follow the pointer to this structure and printed out the values held in the fields. Which it does. The avl node structure contains a pointer to the data structure. So I followed this pointer to the data and this is now where I am stuck. It seems to follow it alright but it is not printing out the values so I am scratching my head again. I enclose the latest code for you to peruse.
    Finally I should point out that the avl files (avl.c, avl_typs.h avl.h) are from a standard library developed by a chap called Brad Appleton (www.bradapp.net) that I have build on for my application.

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    40
    But in the event that I am unable to prove that this technique will work, does anyone have any suggestions on how to build an AVL tree in shared memory?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM