I have the following function which is being called as the start to a module for a program on an embedded AVR clock. I want to get the value from the clock object which will return a date_time structure and copy it into the space I am allocating from the heap.
void time_set_mode_start(Display *display, volatile Controls *controls,
volatile TimeKeeper *clock, void *data) {
DEBUG_LED_PORT |= _BV(DEBUG_LED);
data = malloc(sizeof(date_time));
}
What is the best way to get data the value returned from clock->getTime() into the data pointer?
One way is to use
memcpy. Specifically, you would probably wantmemcpy(data, clock->getTime(), sizeof(date_time)).Another way — probably better, now that I think about it — is to use ordinary assignment:
This treats
dataas adate_time *, and assigns a value to thedate_timeobject that it points to.(Note: in both of the above code-snippets, I’m assuming that
clock->getTime()returns adate_time *. Is that correct?)By the way, I should point out that
data = malloc(sizeof(date_time));will completely replace the originalvoid * datathat was passed in. Your caller will never see the memory location thatdatanow points to, because the pointer is being passed by value.