Note that there are some explanatory texts on larger screens.

plurals
  1. POAbnormal program termination error accessing elements while using vector inside a vector in c++
    primarykey
    data
    text
    <p>I have the following code: (please pardon the length of this code). I am trying to access elements of a vector within a vector. table_info* get_table_info function gets the values of the table_info vector for a particular table. When I try to check the values of column_info vector, I an encountering a strange error, where the program suddenly terminates.</p> <pre><code>typedef struct _column_info { char name[20]; // columns name int type; // 0:INT, 1: CHAR int size; int offset; // start position } column_info; typedef struct _table_info { char name[20]; int column_count; char columns[100]; vector &lt;column_info&gt; col; //col[0], col[1]... char primary_key[5]; int recordsize; int totalsize; int records; } table_info; vector &lt;table_info&gt; v; void create_table(char* tablename, struct columns *cc , int num_col, char* pkey) { char* new_columns; new_columns = (char*)malloc(256*sizeof(char)); strcpy(new_columns,""); int len = 0; len = num_col; for ( int i=0 ; i &lt; len ; i++ ) { strcat(new_columns, cc[i].c_name); strcat(new_columns, ":"); strcat(new_columns, cc[i].c_type); if ( strcmp(cc[i].c_type,"char") == 0 ) { strcat(new_columns, "("); strcat(new_columns, cc[i].c_size); strcat(new_columns, ")"); record_size = record_size + atoi(cc[i].c_size); } else record_size = record_size + 4; if( i != (len-1) ) strcat(new_columns, ","); } table_info new_table; strcpy(new_table.name, tablename); strcpy(new_table.columns, new_columns); strcpy(new_table.primary_key, pkey); new_table.recordsize = record_size; new_table.totalsize = 0; new_table.records = 0; v.push_back(new_table); column_info cols; int offset = 0; for(int x=0;x&lt;num_col;x++) { strcpy(cols.name, cc[x].c_name); if ( strcmp(cc[x].c_type,"char") == 0 ) cols.type = 1; else cols.type = 0; cols.size = atoi(cc[x].c_size); cols.offset = offset; offset += cols.size; new_table.col.push_back(cols); } table_info* table_info; table_info = get_table_info(tablename); int offset2=0; int size2 = 0; for (int i = 0; i &lt; num_col ; i++) { offset2 = table_info-&gt;col.at(i).offset; // ERROR: ABNORMAL PROGRAM TERMINATION printf("offset:%d\n",offset2); size2 = table_info-&gt;col.at(i).size; } } table_info* get_table_info(const string&amp; tablename) { printf("table info \n"); for (int i = 0; i &lt; (int) v.size(); i++) { if (strcmp(v.at(i).name, tablename.c_str()) == 0) return &amp;v.at(i); } return NULL; } </code></pre> <p>Any idea why this program terminates? Please help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload