DWQA QuestionsCategory: ProgramA string copy problem in C + +
aircloud asked 1 month ago

Xcode’s next string copy problem. I’ve been working on it for a long time, but I don’t know why.
The main function is as follows:

char* Convertvalue(Tuple &tuple)
{
    string value = "";
    
    int tupleLength = (int)tuple.length();
    std::cout<<tuple.length()+2<<std::endl;

    char *p = (char*)malloc((tupleLength+2)*sizeof(char));
//    char *p = new char[tupleLength+2];
    memset(p, 0, tuple.length()+2);
    char *v = p;
    
    int ivalue;
    float fvalue;
    
    for(int i=0;i<tuple.attr_count;i++)
    {
        if(tuple.attrs[i].attr_type == CHAR)
        {
            std::cout<<tuple.attr_values[i].data()<<endl;
            strcpy(v,tuple.attr_values[i].data());
            v += tuple.attr_values[i].size();
        }
        if(tuple.attrs[i].attr_type == INT)
        {
            ivalue = atoi(tuple.attr_values[i].c_str());
            
            memcpy(v, &ivalue, 4);
            v += 4;
        }
        if(tuple.attrs[i].attr_type == FLOAT)
        {
            fvalue = (float) atof(tuple.attr_values[i].c_str());
            memcpy(v,&fvalue,4);
            v += 4;
        }
    }
    std::cout<<*p<<std::endl;
    return p;
}

The structure of tuple is as follows

struct Table
{
    string table_ Name; // table name
    int attr_ Count; // the total number of attributes in the table
    Attribute attrs [32]; // list of all attributes in the table, up to 32 attributes
    //return primary key id
    int getPrimaryKeyId() {
        for (int i = 0; i < attr_count; ++i)
        {
            if (attrs[i].attr_key_type == PRIMARY)
            {
                return i;
            }
        }
        //if no primary key
        return -1;
    }
    
    int searchAttrId(string att_name) {
        for (int i = 0; i < attr_count; ++i)
        {
            if (attrs[i].attr_name == att_name)
            {
                return i;
            }
        }
        //if no primary key
        return -1;
    }
    
    int length()
    {
        int len = 0;
        for (int i = 0; i < attr_count; ++i)
        {
            len += attrs[i].attr_len;
        }
        return len;
    }
};

struct Tuple: public Table
{
    string attr_values[32];
};

An example of tuple instantiation:

Tuple tuple1;
    //Tuple inherits the public attribute of table and uses it, so I'll simulate it here first
    //In fact, memcpy can be used to do this
    
    tuple1.table_name = "test_table6";
    tuple1.attr_count = 3;
    tuple1.attrs[0].attr_name="id";
    tuple1.attrs[0].attr_type=INT;
    tuple1.attrs[0].attr_key_type=PRIMARY;
    tuple1.attrs[0].attr_len=1;
    
    tuple1.attrs[1].attr_name="name";
    tuple1.attrs[1].attr_type=CHAR;
    tuple1.attrs[1].attr_key_type=EMPTY;
    tuple1.attrs[1].attr_len=31;
    
    tuple1.attrs[2].attr_name="age";
    tuple1.attrs[2].attr_type=INT;
    tuple1.attrs[2].attr_key_type=EMPTY;
    tuple1.attrs[2].attr_len=1;
    
    //When inserting, we need to pay attention to the fact that we basically convert it into a string here. Insert, int and floating-point numbers and so on. We need to write it as a string, otherwise we will make an error
    tuple1.attr_values[0]="18";
    tuple1.attr_values[1]="xiaotao4";
    tuple1.attr_values[2]="25";

The last char * P returned is not an expected string, but an “X12”, which only contains the first content.
I don’t know why there is such a problem

xlphs replied 1 month ago

I sincerely suggest that you use C + + 11 STD:: tuple

xlphs replied 1 month ago

thank you

1 Answers
EidLeung answered 1 month ago

ivalue = atoi(tuple.attr_values[i].c_str());
memcpy(v, &ivalue, 4);
The decimal 18 is the hexadecimal 12. You can convert it to the int address.