Thread: Illegal instruction / Bus error

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    4

    Illegal instruction / Bus error

    Hello,
    I was searching about what an illegal instruction is and what a bus error is.
    Even though i have found an answer about the illegal instruction, i still can't understand what a bus error stands for.

    I would be very grateful if someone could help me understand and give me an example programm (couple lines) that causes both of these errors for better understanding.

    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,658
    Most objects in memory (say an int) need to be aligned on an address which is a multiple of the size of the object. So for example, a 4-byte int needs to be at an address that is a multiple of 4.

    Now if you try and access an int at an odd address say, then one of several things can happen
    - you access the int just fine, with no performance loss
    - you access the int, but there is an extra memory read which the hardware patches up for you
    - you access the int, but there is a massive performance hit while the OS patches things up for you
    - you get a bus error.

    Compare with a segmentation fault, which is usually associated with trying to read non-existent memory.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    4
    Now its a bit clear to me, though i will need to search for more examples to fully understand.
    I would love an example program though if you could code one for both an illegal instruction error and a bus error.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,658
    Well that's impossible without knowing which OS you have, which compiler you have, and what architecture your CPU is.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2016
    Posts
    4
    oh yeah u are right. i need it for an examination, so i guess its going to be for Linux (Debian) using gcc in x86 arch. but anyway, i will try figuring something out. Thanks for your time and assistance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-28-2009, 08:51 PM
  2. Illegal Instruction when writting a file
    By guiu in forum C Programming
    Replies: 10
    Last Post: 02-04-2009, 12:18 PM
  3. getting an Illegal Instruction error
    By FJRRulz in forum C Programming
    Replies: 22
    Last Post: 04-27-2008, 09:13 AM
  4. Illegal instruction problem with my program
    By bigtruckguy3500 in forum C Programming
    Replies: 3
    Last Post: 04-16-2007, 11:29 PM
  5. gcc inline asm: illegal instruction (core dump)
    By Sargnagel in forum C Programming
    Replies: 4
    Last Post: 10-28-2003, 01:41 PM

Tags for this Thread