Task:
I have an array of records in filtered_records, with the number of filtered records in
num_filtered_records. I want to duplicate this information in binfo->filtered_records
and binfo->num_filtered_records since filtered_records are free’d later in my code.
Definitions:
char** filtered_records;
size_t num_filtered_records;
Snippet:
binfo->filtered_records = malloc(num_filtered_records*sizeof(char*));
memcpy(binfo->filtered_records,
filtered_records,
num_filtered_records * sizeof(char*));
Issue:
When I print binfo->filtered_records, I see all the records, but some of the records have
been replaced by values which are just incorrect. I am not sure what I am missing.
What you are doing doesn’t duplicate the actual data, it just copies pointers. Instead of that
memcpy, do a for:If you don’t have
strdup, usemalloc(strlen(filtered_records[i]) + 1)and thenstrcpy.