c - Accessing pointer from array of pointer inside a struct results in segmentation fault -


i'm having difficulties on trying access members of item_t pointers **addr variable in cart_t. definition of cart_t follows:

typedef struct cart_struct {     item_t **addr;     int ptr;     float total; } cart_t; 

i have initialized , add items **addr using following code:

cart_t *cart_append_item(cart_t *cart, item_t *item) {     if (cart == null || item == null)         return null;      item_t **new = (item_t**) malloc(sizeof(item_t*) * (cart->ptr + 1));      if (new == null) {         return null;     }      int i;     (i=0; i<cart->ptr; i++) {         new[i] = cart->addr[i];     }      new[i+1] = item;      if (cart->addr != null) {         free(cart->addr);     }      cart->addr = new;      cart->ptr += 1;     printf("price %f, quantity %d, total %f\n", new[cart->ptr]->price, new[cart->ptr]->quantity, new[cart->ptr]->price * new[cart->ptr]->quantity); // works.     //printf("total_prev %f\n", cart->total);     cart->total += item->price * item->quantity;     //printf("total %f\n", cart->total);      return cart; } 

as can see on code above, debug printf works. when try later on cart.addr[n], results in segmentation fault:

// cart_t canteen_cart // item_t *item_current ... if (cart_append_item(&canteen_cart, item_current) == null) {     // error. clean } ... printf("[test] price %f, quantity %d, total %f\n", canteen_cart.addr[canteen_cart.ptr-1]->price, canteen_cart.addr[canteen_cart.ptr-1]->quantity, canteen_cart.addr[canteen_cart.ptr-1]->price * canteen_cart.addr[canteen_cart.ptr-1]->quantity); // segmentation fault ... 

thanks!


snippets of int main():

int main() {     cart_t canteen_cart;     canteen_cart.addr = null;     canteen_cart.ptr = 0;     canteen_cart.total = 0.0f;      item_t *item_current = null;      ...     hundred of lines later of user input thingy     ...      if (state == state_checkout) {         printf("[test] %d\n", canteen_cart.addr == null); // returns 0         printf("[test] %d\n", canteen_cart.ptr > 0); // returns 1, there items in array         printf("price %f, quantity %d, total %f\n", canteen_cart.addr[canteen_cart.ptr-1]->price, canteen_cart.addr[canteen_cart.ptr-1]->quantity, canteen_cart.addr[canteen_cart.ptr-1]->price * canteen_cart.addr[canteen_cart.ptr-1]->quantity);     print_view_checkout(&canteen_cart); // segmentation fault here     }      cart_free_items(&canteen_cart); // free cart , members within it.      return 0;  } 

now when can see how initialize canteen_cart, take single line cart_append_item function:

new[i+1] = item; 

in first call cart_append_item function, value of cart->ptr (it indeed badly named) 0. means loop not iterate , value of i zero. means in effect doing new[1] = item, new has space single element index 0. writing out of bounds of course leads undefined behavior, making whole program ill-formed , invalid. , prone crashing.

you continue write out of bounds each consecutive call cart_append_item well.

the simple solution? use new[cart->ptr] = item instead.


then regarding realloc comment, can make function shorter , simpler if using realloc:

cart_t *cart_append_item(cart_t *cart, item_t *item) {     if (cart == null || item == null)         return null;      item_t **new_items = realloc(cart->addr, sizeof *cart->addr * cart->ptr + 1);     if (new_items == null) {         return null;     }      cart->addr = new_items;      // add new item     cart->addr[cart->ptr++] = item;      cart->total += item->price * item->quantity;      return cart; } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -