Hi all,
I was recently working on a C++ application in Xcode (free Mac developer tool) and started running into some really weird BUS errors. What happened was that the code would compile and run fine in terminal up until I initialized two new variables just like this --> int r, c;
After going back and getting rid of that line of code I tried a few new initialization but found that virtually any new initialization I wrote into the code caused the same problem. I would recompile and execute the code only to find that the terminal was still throwing BUS errors. Yet when I got rid of those new initializations it went back to running perfectly well.
All the code I have written and compiled is included below and I have commented some of relevant sections where the problem occurs. If anyone could help me that would be much obliged because I have no clue what could be causing these errors.
-----------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*-------STRUCTURES---------*/
typedef struct {
int rows;
int cols;
int num_colors;
int file_size;
int vector_size;
int* data;
} sImage;
/*----------GET IMAGE INFO SUBPROGRAM--------------*/
long get_info(FILE* inputFile, long offset, int numberOfChars)
{
unsigned char dummy = '0';
unsigned char *ptrC = &dummy;
long value = 0;
fseek(inputFile, offset, SEEK_SET);
for(int i = 0; i < numberOfChars; i++) {
fread(ptrC, sizeof(char), 1, inputFile);
/* calculate value based on adding bytes */
value = (long)(value + (*ptrC)*(pow(256, (i))));
}
return(value);
}
/*-----------SET IMAGE DATA SUBPROGRAM----------------*/
void set_image_data(FILE* inputFile, sImage* bmp) {
bmp->cols = (int)get_info(inputFile, 18, 4);
bmp->rows = (int)get_info(inputFile, 22, 4);
bmp->file_size = (int)get_info(inputFile, 2, 4);
bmp->num_colors = (int)get_info(inputFile, 46, 4);
bmp->vector_size = (int)(bmp->file_size - (14 + 40 + 4*(bmp->num_colors)));
}
/*------------PRINT IMAGE DATA SUBPROGRAM-------------*/
void print_image_data(sImage* bmp){
printf("Width: %d\n", bmp->cols);
printf("Height: %d\n", bmp->rows);
printf("File size: %ld\n", bmp->file_size);
printf("# Colors: %d\n", bmp->num_colors);
printf("Vector size: %d\n", bmp->vector_size);
}
int main(int argc, char* argv[])
{
// Check that correct file path entered
if(argc < 2)
{
printf("Usage: %s bmpInput.bmp\n", argv[0]);
exit(0);
}
printf("Reading filename %s\n", argv[1]);
// Variable Declarations
FILE *bmpInput, *rasterOutput; /* Input file and text file with raster output */
sImage *original; /* Original BMP image data */
/*--------READ INPUT FILE------------*/
bmpInput = fopen(argv[1], "rb");
fseek(bmpInput, 0L, SEEK_END);
/*--------DECLARE OUTPUT TEXT FILE--------*/
rasterOutput = fopen("data.txt", "w");
/*--------SET AND PRINT IMAGE DATA--------*/
set_image_data(bmpInput,original);
print_image_data(original);
/*
*
* TRYING TO ACCESS ORIGINAL->NUM_COLORS IN THIS LINE CAUSES A BUS ERROR TO BE THROWN * WHEN EXECUTED IN TERMINAL, EVEN THOUGH IT IS CLEARLY ACCESSED IN THE NEXT LINE OF CODE
*
*/
// SHOULD THROW BUS ERROR WHEN UNCOMMENTED
// original->num_colors++;
/*----START AT BEGINNING OF RASTER DATA-----*/
fseek(bmpInput, (54 + 4*original->num_colors), SEEK_SET);
/*
*
* INITIALIZING TWO NEW INTEGER VARIABLES WILL ALSO CAUSE THE PROGRAM TO THROW A BUS ERROR
*
*/
// SHOULD THROW BUS ERROR WHEN UNCOMMENTED
// int r, c;
/*
*
* WAS WRITTING THE BELLOW BIT OF CODE WHEN I FIRST NOTICED THE PROBLEM WITH THE BUS ERRORS
*
*/
/*----------READ RASTER DATA----------*/
//for(int r = 0; r < original->rows; r++) {
//for(int c = 0; c < original->cols; c++) {
/*-----read data and print in (row,column) form----*/
//fread(pChar, sizeof(char), 1, bmpInput);
//fprintf(rasterOutput, "(%d, %d) = %d\n", r, c, *pChar);
//}
//}
fclose(bmpInput);
fclose(rasterOutput);
}
-----------------------------------------------------------------------