C Compiler Logo

Bus Error

Bus Error

runtime

Bus error (core dumped)

Description

The program tried to access memory in a way that is not allowed by the hardware, often due to alignment issues.

Common Causes

Example of Error

Error Code

int main() {
    char *ptr = "Hello";
    int *iptr = (int *)(ptr + 1);  // Unaligned access
    *iptr = 10;
    return 0;
}

Solution

Ensure proper memory alignment and avoid accessing memory outside allocated regions.

Corrected Code

#include <stdlib.h>

int main() {
    int *iptr = malloc(sizeof(int));
    if (iptr != NULL) {
        *iptr = 10;
        free(iptr);
    }
    return 0;
}

Additional Tips