Re: sprintf leading up to bus error (signal 10)



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.
.




Relevant Pages

  • Re: Question regarding malloc casing
    ... malloc because it is bug hider. ... still I am using malloc so compiler will throw warring ... If you ignore the bug, ... The compiler trusts you the programmer. ...
    (comp.lang.c)
  • Re: Newbie question about malloc
    ... some minimum number of bytes at a time via malloc(). ... for which the C compiler compiles machine-level code.") Computer ... simulates some other machine that only has 16-bit pointers. ... the emulator, and can optionally run some code "native" intermixed ...
    (comp.lang.c)
  • Re: gcc knows about malloc()
    ... While omitting the system header file is a mistake, ... malloc() function incorrectly, but by failing to cast it at all! ... It is a common mistake to think that casting the int to a pointer ... so you found a compiler bug - please report it to gcc via their usual ...
    (comp.lang.c)
  • Re: undefined reference to sbrk
    ... linker script file). ... when I run the program it gets stuck after I call the malloc ... compiler doesn't complain, but an actual implementation is missing. ... It is responsible establishing the end of the heap, ...
    (comp.arch.embedded)
  • Re: IS this a proper way of freeing memory with free()
    ... (not just the line with malloc). ... void *ptr; ... This bug is caused by a design flaw in fcn. ... Be in the habit of casting malloc ...
    (comp.lang.c)