Thread: Trying to generate a bus error

  1. #1
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101

    Trying to generate a bus error

    Hi. I am trying to generate a bus error using unaligned memory but it's not working. I tried it myself first but couldn't get it to work, so I used the code from the wikipedia entry:

    Code:
    #include <stdlib.h>
    int main (void) {
      int x, *iptr;
    
      char *cptr;
    
      cptr = (char*)malloc(33);
      if (!cptr) return 1;
    
      cptr++;
    
      x = *cptr;
    
      iptr = (int *) cptr;
      x = *iptr;
    
      return 0;
    }
    It seems to run fine. The version of gcc is 4.1.2. It is supposed to generate a bus error so why isn't it?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The following links indicate that on Intel boxes, unaligned access normally causes performance degradation but not a bus error.

    http://www.thescripts.com/forum/thread161809.html
    http://www.thescripts.com/forum/thread213140.html

    Edit: Here's a good one:

    http://msdn.microsoft.com/library/de...fx86x86-64.asp
    Last edited by robatino; 08-21-2007 at 04:51 PM.

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    This will not always lead to error. Some processor tolerates unaligned acces to memory. And in some case, the compiler will add extra instruction for the memory to be accesed in a proper way (but i don't think it's the case in your example). I did my own code to test on my own computer and there's no errror while executing. I thought the one you showed wasn't really good (not even freeing memory)

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
    	void *ptr;
    	int i;
    
    	ptr = malloc(2 * sizeof(int) - 1);
    	if (malloc != NULL)
    	{
    		for (i = 0; i < sizeof(int); i++)
    		{
    			* (int *) ((int) ptr + i);
    		}
    
    		free(ptr);
    
    		printf("Looks like no memory alignement error happened\n");
    	}
    
    	else
    	{
    		printf("Error while allocating memory\n");
    	}
    
    	getchar();
    
    	return 0;
    }
    Last edited by foxman; 08-22-2007 at 08:40 AM.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    An x86 can handle unaligned access. It's just slow.

    Try this on a SparcStation instead.

  5. #5
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    OK, thanks for the help guys. FWIW, it wasn't my code. I got it from the wikipedia entry. You should maybe change it if you don't think it's a suitable example to show everyone.

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. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM