steve wrote:
Old said:
I'm not stupid and don't terribly enjoy being moked (If the
boston tea party comment was meant as a joke then I appologize
for having taken it as condescendence).
Sorry for any offence. sprintf() does not allocate any memory,
it writes data to memory that should have been allocated already.
Most standard library functions (other than the 'malloc' family)
do not allocate memory, you should assume this unless there is
documentation to the contrary.
My "solution" of allocating space for 255 characters is an
adequate one because the strings that I'll be dealing
with are significantly shorter than 255 characters.
It would be good to add some checking into your program so that
your sprintf does not overflow the 255 characters, in case you
happen to later run your program with long strings.
If a complete program is required than I'll gladly provide what I can,
What you should do is start with your broken program, and gradually
delete bits that you think are unrelated, and see if the problem
persists. Eventually you will either end up with a small program,
or you will have identified which bit was causing the problem.
(Warning: this technique doesn't always work, because in C sometimes
one broken line can appear to run correctly and have some bad
side-effect in a completely different part of the program).
if(!malloc(sizeof(lst_vals))){
send_error_to_scn("Error allocating memory in pre_analysis");
status = FALSE;
goto END;
}
This calls malloc but does not assign the space to anything...
bound to cause a problem somewhere.
Did you mean to assign it to lst_ptr?
userInst->seniorData = (void *)lst_ptr;
As it stands, this causes undefined behaviour because
lst_ptr has not been assigned a value yet.
This cast on lst_ptr is unnecessary. You can assign any object
pointer to a pointer of type (void *), without casting.
It helps to avoid bugs if you avoid casts unless absolutely necessary.
/*Initialize*/
((lst_vals *)userInst->seniorData)->lst_T_chan = 340.0;
END:
return status;
}/*pre_analysis()*/
Your function doesn't actually use lst_ptr at all: you could
write:
if (!(userInst->seniorData = malloc(sizeof(lst_vals))))
..... error stuff ......
((lst_vals *)userInst->seniorData)->lst_T_chan = 340.0;
BTW I would recommend changing "status = FALSE; goto END;"
to "return FALSE". IMHO it is much clearer to read.