Casting const char * to non const char*

797 views Asked by At

I am writing a program in c++ to compare two large XML files and create a file with the identifiers and changes of the products (nodes) that changed. To do so I am using pugixml.

I am working as a PHP developer at the moment, and it has been a while since I have used c++ so I think I am overlooking something trivial, but after hours of searching online I still have not found the solution to my problem, which is:

The child_value function, which simply gives the value between the tags of an element, returns a const char *. What I am trying to do is put all the values in an array and compare them with the values of all other products (which are in a similar array).

The problem arises when going to the next product, and I need to overwrite the values in the array, which I think is the origin of the segmentation error I am getting. So my question is:

I need to use that const char * for comparisons, but I need to overwrite the values so I can do the next comparison, what is the best way to do this? I have tried strcpy, const_cast (like in the example code below) and loads of other suggestions, but all seem to result in the same segmentation error.

It compiles but it will just crash on the second iteration when it tries to overwrite the first values.

for (xml_node groupCurrent = groupsCurrent.child("group");groupCurrent;groupCurrent = groupCurrent.next_sibling("group")){

    xml_node productsCurrent = groupCurrent.child("products");
    size_t nrProductsInGroupCurrent = std::distance(productsCurrent.children().begin(), productsCurrent.children().end());
    nrProductsTotalCurrent = nrProductsTotalCurrent + nrProductsInGroupCurrent;

    for (xml_node productCurrent = productsCurrent.child("product");productCurrent;productCurrent = productCurrent.next_sibling("product")){

        int numberAttributesC=0;
        char * childrenCurrent[32];

        for (xml_node attributeCurrent = productCurrent.first_child();attributeCurrent ;attributeCurrent= attributeCurrent.next_sibling()){
            char * nonConstValue = const_cast<char *> (attributeCurrent.child_value());
            childrenCurrent[numberAttributesC]=nonConstValue;
            numberAttributesC++;
        }

        /*for(int i = 0;i<numberAttributesC;i++){
            std::cout<<childrenCurrent[i];
        }*/
        //xml_node groupsNew = docNew.child("product_database");

    }
}

Any help, suggestions or comments are greatly appreciated. If I find the solution myself in the meanwhile I will post it here.

Regards, Anti

PS: On ubuntu using gcc version 4.8.2

1

There are 1 answers

2
Sean On BEST ANSWER

Just use a vector and string

std::vector<std::string> childrenCurrent;

for (xml_node attributeCurrent = productCurrent.first_child();attributeCurrent ;attributeCurrent= attributeCurrent.next_sibling())
{    
  std::string value = attributeCurrent.child_value();
  childrenCurrent.push_back(value);    
}

NOTE: I'm assuming that child_value is returning a const char*

The string will copy the child value, which removes any memory issues, and using the vector will save you having to worry about managing the number of potential attributes.