将此向量char数组复制到String中的正确方法是什么

时间:2018-01-16 16:17:35

标签: c++ arrays vector c++-cli void-pointers

我尝试使用返回void **(数据数组数组)的C API,它可以包含任意数量的每种不同类型(double,long,short,custom struct ...)除了数组char数组之外,API可以正常工作。

// a simplified API prototype
void READHISTORY( long id                  // ID in custom database
                , short numFields          // number of fields 
                , const timestruct* oldest // oldest time to look up
                , const timestruct* newest // newest time to look up
                , long maxRetVals          // the size of each data array
                , const short DataTypes[]  // types[] length numFields
                , void* theData[]          // pointers to data arrays
                , short* numReturned       // number of returned values 
)

要设置主读取,将读取数据库以查看有多少字段及其自定义类型。

#define MAXVALS (2048) 
long id = 20L;
short vals;  // number of vals returned
tstruct oldest = {2017,1,1,0,0,0};
tstruct newest = {2018,1,1,0,0,0}; 
int numFields;
std::vector<short> datatypes;

GETFIELDS(id, &numFields, datatypes);  // API to get the number and types of fields
std::vector<void*> values;

for(int i=0; i<numFields; i++) {
    datatypes.push_back(DataTypes[i]);
    switch (DataTypes[i]) { // want the arrays created dynamically 
       case -1: // time 
          values.push_back(new tstruct[MAXVALS]);break;
       case -2: // short
          values.push_back(new short[MAXVALS]);break;                          
       case -3: // long
          values.push_back(new long[MAXVALS]);break;
       case -4: // double
          values.push_back(new double[MAXVALS]);break;
       default: // positive numbers are char array length
          values.push_back(new char[DataTypes[i]][MAXVALS]);
    }
}

READHISTORY调用正在运行并返回数据,但我收到了char数组数组的异常错误。

// call the API to get data into the individual data arrays
READHISTORY(id, numFields, &oldest, &newest, MAXVALS, datatypes.data(), values.data(), &vals);

for (int i=0; i<vals ; i++) {
  for(int j=0; j< datatypes.size(); j++) {
    switch(datatypes[j]) {
      case -1: tstruct tval = static_cast<tstruct*>(values[1])[i];   break; // works fine           
      case -2: short sval = static_cast<short*>(values[j])[i];       break; // works fine
      case -3: long lval = static_cast<long*>(values[j])[i];         break; // works fine
      case -4: double dval = static_cast<double*>(values[j])[i];     break; // works fine
      ... 

      default:  
        int len = datatypes[j]; // positive datatypes are the char array length
        char* nchar = new char[len+1];

        // attempting to copy the char gets an access violation is here.
        strncpy(nchar, static_cast<char**>(values[j])[i], len); 

        nchar[len+1] = '\0';
        String^ nstr = gcnew String(nchar);
        delete nchar;
        // do something with nstr and so on... 
      }
   }
}

未处理的异常:System.AccessViolationException:尝试读取或写入受保护的内存。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您使用了错误的constructor来创建向量:

std::vector<short> values(numFields);

这会创建一个包含numFields空指针的向量。您稍后追加动态分配的数组,但空指针仍将是READHISTORY尝试使用的第一个元素。

而只是默认构造空向量:

std::vector<short> values;