Re: sprintf leading up to bus error (signal 10)
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Mon, 18 Jul 2005 21:58:42 +0100
steve wrote:
I should've given a warning that I'm not a software guy but really an electronics engineer so this is somewhat untested ground for me, but I'm doing my best.
I suggest you buy a copy of K&R2 and read the FAQ for this group (google will find the FAQ which tells you what K&R2 is and lots of other good stuff).
Well I've been working all morning and have finally found the source of my "bus error (signal 10)" errors. The source is odd. The error occurs in any function where I make the function call:
(void)sprintf(ptr_testing, "This is my string");
This in itself isn't where the actual error occurs. The error occurs at any later point, in the same function where the sprintf() call is made, where I try to assign a value to one of the variables in one of my data structures. The lines are syntactically reminiscent of:
((lst_vals *)userInst->seniorData)->lst_T_chan = (double) 340.0;
IMO, (double) isn't necessary as the floating point constants are double by default.
You're right the double type cast is not required but in one of my many attempts to resolve the problem I started typecasting everything. This is infact an artifact of previous attemps, but removing it doesn't solve the problem, unfortunately.
I would suggest you strip out all the casts. There are a few instances where casts are needed, but generally they are not and just hide problems.
Where the seniorData is a pointer to a lst_vals data structure defined as follows:
/*data structure for seniorData to point to*/ typedef struct { double lst_T_chan, current_T_chan, lst_t, current_t; } lst_vals;
Any ideas on what's going on? It's way beyond me. I orginally thought that it was a simple typecasting problem, I'm pretty sure I'm wrong. It's extremely odd because any number of commands between those two lines of code will be executed. The instant the assignment shown above is reached the program exits with signal 10.
Are you sure there's enough memory for a structure of type lst_vals at the address to which points userInst->seniorData? I don't know what exactly you're doing but this pointer cast makes me suspect that this is the case. I'd expect a segmentation fault here, however, but you never know what you break by writing where you shouldn't.
There should be enough room for the structure as I'm performing a malloc as follows:
if(!malloc(sizeof(lst_vals))){
If this is your actual code it is wrong. You need to assign the pointer value returned by malloc to the pointer you want to point to it. Also, a good way (in general) to do your mallocs and avoid allocating the wrong amount of memory is:
ptr = malloc(N * sizeof *ptr);
if (ptr) {
/* Handle error */
Where N is the number of items you want space for. This way, you are effectively telling the compiler you want space for N of the things ptr points to.
send_error_to_scn("Error allocating memory in pre_analysis"); status = FALSE; goto END; }
userInst->seniorData = (void *)lst_ptr;
which is returning true.
Any other ideas?
Try stripping things out of the code to produce a small, complete, compilable and runable program that shows the problem. You might find the error yourself in cutting the program down but, if not, by allowing us to see a complete program exhibiting the problem you allow us to see the actual error. Otherwise, the problem could be with any of the things you have not posted.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
.
- References:
- sprintf leading up to bus error (signal 10)
- From: steve
- Re: sprintf leading up to bus error (signal 10)
- From: Alexei A. Frounze
- Re: sprintf leading up to bus error (signal 10)
- From: steve
- sprintf leading up to bus error (signal 10)
- Prev by Date: Re: sprintf leading up to bus error (signal 10)
- Next by Date: Re: sprintf leading up to bus error (signal 10)
- Previous by thread: Re: sprintf leading up to bus error (signal 10)
- Next by thread: Re: sprintf leading up to bus error (signal 10)
- Index(es):
Relevant Pages
|