0

I am migrating codes with char data type to string, and below is my code:

struct SETTING_VALUE_LOCAL_SORT_LIST {  
   std::string top;
   int id;
}

int list_table_size = 25;
LIST* setting_value = NULL;
setting_value = new LIST[list_table_size];
memset(setting_value, 0, (list_table_size * sizeof(LIST)));

std::cout << "set 1: \n";
setting_value[0].top = "test1";
setting_value[0].id = 1;

Can you please tell me why my program crashes after printing "set 1"? I seems there is an error accessing by element and need to access by the address set during memset. How should I do that?

Edit: Can you explain a little more, does that mean string cannot be in struct and then initialize?

Thanks in advanced.

steiryx
  • 71
  • 10
  • 1
    To "zero" initialize everything: `LIST* setting_value = new LIST[list_table_size]();` – juanchopanza Nov 29 '16 at 07:49
  • I have seen a better explanation on the link below, I think the problem is accessing an invalid address which was destructed during initialization of structure, please refer here for more clear explanation: http://stackoverflow.com/questions/1998752/memset-or-value-initialization-to-zero-out-a-struct – steiryx Nov 29 '16 at 08:28
  • @juanchopanza thanks for the hints – steiryx Nov 29 '16 at 08:40

0 Answers0