-2

I am trying to read the elements in an XML and store in a array of struct and need to pass the pointer of this array to other functions. However I have issue compiling in gnu, error message:

error: cannot convert myRec to uint32_t {aka unsigned int}' in return return *recs;

Tried to set myRec recs[count] without malloc, get an error of invalid pointer.

struct myRec
{
std::string one;
std::string two;
std::string three;
std::string four;
std::string five;
std::string six;
};

uint32_t count = 0;

XMLDocument doc;
doc.LoadFile(pFilename);
XMLElement* parent = doc.FirstChildElement("a");
XMLElement* child = parent->FirstChildElement("b");
XMLElement* e = child->FirstChildElement("c");

for (e = child->FirstChildElement("c"); e; e = e->NextSiblingElement("c"))
{
    count++;
}

std::cout << "\n""Count = " << count << std::endl;

recs = (myRec *)malloc(6 *count * sizeof(myRec));

XMLElement *row = child->FirstChildElement();
if (count > 0)
{
    --count;
    count = (count < 0) ? 0 : count;
    for (uint32_t i = 0; i <= count; i++)
    {
        while (row != NULL)
        {
            std::string six;
            six = row->Attribute("ID");
            recs[i].six = six;

            XMLElement *col = row->FirstChildElement();
            while (col != NULL)
            {
                std::string sKey;
                std::string sVal;
                char *sTemp1 = (char *)col->Value();
                if (sTemp1 != NULL) {
                    sKey = static_cast<std::string>(sTemp1);
                }
                else {
                    sKey = "";
                }
                char *sTemp2 = (char *)col->GetText();
                if (sTemp2 != NULL) {
                    sVal = static_cast<std::string>(sTemp2);
                }
                else {
                    sVal = "";
                }
                if (sKey == "one") {
                    recs[i].one = sVal;
                }
                if (sKey == "two") {
                    recs[i].two = sVal;
                }
                if (sKey == "three") {
                    recs[i].three = sVal;
                }
                if (sKey == "four") {
                    recs[i].four = sVal;
                }
                if (sKey == "five") {
                    recs[i].five = sVal;
                }
                col = col->NextSiblingElement();
            }// end while col
            std::cout << "\n""one = " << recs[i].one << "\n"" two= " << recs[i].two << "\n""three = " << recs[i].three << "\n""four = " << recs[i].four << "\n""five = " << recs[i].five << "\n""six = " << recs[i].six << std::endl;
            row = row->NextSiblingElement();
        }// end while row
    }
}
else
{
    std::cout << "Failed to find value, please check XML! \n" << std::endl;
}
return *recs;

expect to return a pointer to the array

I declared it as:

std::string getxmlcontent(const char* pFilename);
myRec*recs= NULL;

Function:

std::string readxml::getxmlcontent(const char* pFilename)
{
}

Not sure if it is the right way as I am quite new to c++

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
inno3491
  • 1
  • 1

1 Answers1

1

You're making a few errors, you probably should get a good C++ book and do some studying

In C++ use new

recs = new myRec[6*count];

instead of

recs = (myRec *)malloc(6 *count * sizeof(myRec));

The problem with malloc in a C++ program is that it won't call constructors, so all the strings you have in your struct are invalid, and (most likely) your program will crash when you run it.

It's not clear to me why you need 6*count, that seems to be because you have six strings in your struct. If so then that's confused thinking, really you just need

recs = new myRec[count];

and you'll get 6*count strings because that's how you declared your struct.

sKey = sTemp1;

instead of

sKey = static_cast<std::string>(sTemp1);

No need for the cast, it's perfectly legal to assign a char* to a std::string.

Finally if you want to return a pointer, then just return the pointer

return recs;

not

return *recs;

However you haven't included the function signature in the code you posted. I suspect there's another error, but I can't tell unless you post how you declare this function.

john
  • 85,011
  • 4
  • 57
  • 81
  • 6 is the number of strings in the struct and count is the number of elements in the XML, each element contains 6inputs that I read in and stored into an array, the count is dynamic as the number of elements in the XML can change. – inno3491 Jul 18 '19 at 09:49
  • @inno3491 Yes, but the point is that your struct already has six strings in it, so you don't need to ask for 6 again. You'll end up allocating 6*6*count strings that way. What you need is `count` copies of your struct, each of which has 6 strings in it. You get that with `new myRec[count]`. – john Jul 18 '19 at 09:54
  • when I print out the contents of the array, i get the correct printing, however I get free(): invalid pointer error after the programs ends – inno3491 Jul 19 '19 at 02:15
  • @inno3491 That indicates a *heap corruption error*, i.e. you are doing something with heap allocated memory that you shouldn't. Unfortunately it's impossible to say what the cause might be without seeing a complete program. What you should do is try and remove as much code as possible from your app while keeping the error. Then when you have the smallest possible program that still has the heap corruption error post the code to stackoverflow and ask what's causing it. – john Jul 19 '19 at 04:24