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.
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 <column_info> col; //col[0], col[1]...
char primary_key[5];
int recordsize;
int totalsize;
int records;
} table_info;
vector <table_info> 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 < 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<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 < num_col ; i++)
{
offset2 = table_info->col.at(i).offset; // ERROR: ABNORMAL PROGRAM TERMINATION
printf("offset:%d\n",offset2);
size2 = table_info->col.at(i).size;
}
}
table_info* get_table_info(const string& tablename)
{
printf("table info \n");
for (int i = 0; i < (int) v.size(); i++)
{
if (strcmp(v.at(i).name, tablename.c_str()) == 0)
return &v.at(i);
}
return NULL;
}
Any idea why this program terminates? Please help.
The Problem is that this code:
is called before the columns have added to the table. At this time the “col”-collection is empty. Then
push_backpushes a copy of yourtable_infoinstance to the vector (of course with the emptycol-vector). So if you add the columns it is not added to the instance in your vector but to the local instance instead. And sincenum_colis greater than zero it throws an exception if you try to access the columns on that line, sincetable_infois the instance which is stored in the vector (the one with the emptycol):The solution is to move the
push_backcode down, right before you callget_table_info(or after adding the columns, if you will):