Click here to Skip to main content
15,896,265 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionchange color in c Pin
hasani200723-Jun-10 9:36
hasani200723-Jun-10 9:36 
AnswerRe: change color in c Pin
Rick York23-Jun-10 10:07
mveRick York23-Jun-10 10:07 
AnswerRe: change color in c Pin
Stephen Hewitt23-Jun-10 13:30
Stephen Hewitt23-Jun-10 13:30 
QuestionHow can a program in diferent PCs using the same DB be notified of a change? Pin
manchukuo23-Jun-10 9:14
manchukuo23-Jun-10 9:14 
AnswerRe: How can a program in diferent PCs using the same DB be notified of a change? Pin
Luc Pattyn23-Jun-10 9:28
sitebuilderLuc Pattyn23-Jun-10 9:28 
GeneralRe: How can a program in diferent PCs using the same DB be notified of a change? Pin
manchukuo23-Jun-10 16:24
manchukuo23-Jun-10 16:24 
GeneralRe: How can a program in diferent PCs using the same DB be notified of a change? Pin
Luc Pattyn23-Jun-10 16:39
sitebuilderLuc Pattyn23-Jun-10 16:39 
QuestionCan anyone please tell me why these initializations are causing BUS errors? Pin
M.manning23-Jun-10 8:47
M.manning23-Jun-10 8:47 
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);



}
-----------------------------------------------------------------------
AnswerRe: Can anyone please tell me why these initializations are causing BUS errors? Pin
Code-o-mat23-Jun-10 9:06
Code-o-mat23-Jun-10 9:06 
GeneralRe: Can anyone please tell me why these initializations are causing BUS errors? Pin
M.manning23-Jun-10 9:26
M.manning23-Jun-10 9:26 
GeneralRe: Can anyone please tell me why these initializations are causing BUS errors? Pin
Code-o-mat23-Jun-10 9:35
Code-o-mat23-Jun-10 9:35 
AnswerRe: Can anyone please tell me why these initializations are causing BUS errors? Pin
Aescleal23-Jun-10 10:31
Aescleal23-Jun-10 10:31 
GeneralRe: Can anyone please tell me why these initializations are causing BUS errors? Pin
M.manning23-Jun-10 13:31
M.manning23-Jun-10 13:31 
QuestionHOW to save DC of onpaint(); in SDI MFC C++? Pin
humais23-Jun-10 8:40
humais23-Jun-10 8:40 
AnswerRe: HOW to save DC of onpaint(); in SDI MFC C++? Pin
CPallini23-Jun-10 9:16
mveCPallini23-Jun-10 9:16 
GeneralRe: HOW to save DC of onpaint(); in SDI MFC C++? Pin
humais23-Jun-10 9:20
humais23-Jun-10 9:20 
GeneralRe: HOW to save DC of onpaint(); in SDI MFC C++? Pin
humais23-Jun-10 9:24
humais23-Jun-10 9:24 
GeneralRe: HOW to save DC of onpaint(); in SDI MFC C++? Pin
CPallini23-Jun-10 10:37
mveCPallini23-Jun-10 10:37 
GeneralRe: HOW to save DC of onpaint(); in SDI MFC C++? Pin
humais23-Jun-10 19:46
humais23-Jun-10 19:46 
GeneralRe: HOW to save DC of onpaint(); in SDI MFC C++? Pin
Cedric Moonen23-Jun-10 20:53
Cedric Moonen23-Jun-10 20:53 
GeneralRe: HOW to save DC of onpaint(); in SDI MFC C++? Pin
CPallini23-Jun-10 21:08
mveCPallini23-Jun-10 21:08 
GeneralRe: HOW to save DC of onpaint(); in SDI MFC C++? Pin
humais24-Jun-10 21:49
humais24-Jun-10 21:49 
QuestionInsetObject effect the starting character selection Pin
ForNow23-Jun-10 7:50
ForNow23-Jun-10 7:50 
QuestionDebug with breakpoint works, but debug without breakpoint or release mode fails ? Pin
oppstp23-Jun-10 6:19
oppstp23-Jun-10 6:19 
AnswerRe: Debug with breakpoint works, but debug without breakpoint or release mode fails ? Pin
Code-o-mat23-Jun-10 7:00
Code-o-mat23-Jun-10 7:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.