J
Jonathan Bartlett
My questions are these:
I think you are confusing issues. The malloc() statements in your post
ARE allocating the space for the doubles.
last_values *lst_ptr = malloc(sizeof(*lst_ptr));
[however, this should actually be size(last_values)]
allocates memory for the entire last_values structure, which _includes_
your three pointers. However, they aren't pointing _to_ anything, yet.
When you do:
lst_ptr->lst_t = malloc(sizeof(double));
lst_ptr->lst_vc = malloc(sizeof(double));
lst_ptr->lst_ic = malloc(sizeof(double));
These are allocating space for the _actual double values_, and assigning
the pointers to theses values to the various members of lst_ptr.
However, I think, given your goal, you are going about this entirely the
wrong way. I do not know why you would even need a structure of
pointers for this, as opposed to just a structure of doubles.
I have no idea what you are trying to do here. What function are you
trying to call? What "h" files are you trying to use? It looks like
some sort of generic callback-with-data interface. Anyway, it would at
least appear that you are way overusing pointers. Can you make it work
with just:
typedef struct {
double lst_t, lst_vc, lst_ic;
} last_values;
That seems to be a much easier plan. You can rid yourself of 3 malloc's
(and free's!) that way.
Jon
1) Do I have to allocate space for the doubles lst_t, lst_vc, lst_ic
as well
as their pointers?
I think you are confusing issues. The malloc() statements in your post
ARE allocating the space for the doubles.
last_values *lst_ptr = malloc(sizeof(*lst_ptr));
[however, this should actually be size(last_values)]
allocates memory for the entire last_values structure, which _includes_
your three pointers. However, they aren't pointing _to_ anything, yet.
When you do:
lst_ptr->lst_t = malloc(sizeof(double));
lst_ptr->lst_vc = malloc(sizeof(double));
lst_ptr->lst_ic = malloc(sizeof(double));
These are allocating space for the _actual double values_, and assigning
the pointers to theses values to the various members of lst_ptr.
However, I think, given your goal, you are going about this entirely the
wrong way. I do not know why you would even need a structure of
pointers for this, as opposed to just a structure of doubles.
2) Where and how should I define my structure last values assuming
that I
have no ability to change the related *_h file. i.e. can I just
do it at
the start of the function I'm creating everything in of do I have
to do
> it at a higher level?
I have no idea what you are trying to do here. What function are you
trying to call? What "h" files are you trying to use? It looks like
some sort of generic callback-with-data interface. Anyway, it would at
least appear that you are way overusing pointers. Can you make it work
with just:
typedef struct {
double lst_t, lst_vc, lst_ic;
} last_values;
That seems to be a much easier plan. You can rid yourself of 3 malloc's
(and free's!) that way.
Jon