Я пытаюсь вырастить массив, чтобы добавить новые указатели malloc'd. По-видимому, realloc не увеличивает размер. Кроме того, я начинаю с достаточно места для одного указателя в массиве, поэтому, хотя realloc не увеличил размер, я все же ожидал, что смогу скопировать указатель, но я получаю SIGSEGV Segmentation Fault.Seg Fault on memset
typedef struct active_allocation {
size_t sz;
void *ptr;
} ACTIVE_ALLOCATION;
struct m61_state {
ACTIVE_ALLOCATION **active_allocations_ptrs_arr; //Array of Points to Active Allocations
size_t sz;
};
struct m61_state m61_state;
...
ACTIVE_ALLOCATION **active_allocations_ptrs_arr = malloc(sizeof(ACTIVE_ALLOCATION*) *1);
m61_state.active_allocations_ptrs_arr = active_allocations_ptrs_arr;
...
//Create a New pointer, to add to the array
ACTIVE_ALLOCATION *active_allocation_record = malloc(sizeof(ACTIVE_ALLOCATION));
// ** Initially there's space for one pointer, but it hasn't been used yet.
//m61_state->sz equals 0.
//Trying to increase the size of an array to 8 for one more ACTIVE_ALLOCATION* Last 4 can be set to NULl
//sizeof(new_active_alloc_array_ptr) equals 4 at this point
new_active_alloc_array_ptr = realloc(m61_state->active_allocations_ptrs_arr, m61_state->sz + sizeof(ACTIVE_ALLOCATION*));
//** sizeof(new_active_alloc_array_ptr) still equals 4. I want it to be 8. I'm not sure why the size didn't change.
//Copy the new pointer that was just created active_allocation_record to the array
memset(m61_state->active_allocations_ptrs_arr[sizeof(ACTIVE_ALLOCATION*)* m61_state->sz], (int)active_allocation_record, sizeof(ACTIVE_ALLOCATION*));
http://www.sscce.org/ – dcaswell
'sizeof (new_active_alloc_array_ptr)' - размер указателя. Он никогда не изменится. – interjay
Если вы используете «memset» для «копирования» данных, вы делаете Doing It Wrong ™. Это то, на что «memcpy» (или простое назначение!). –