0

I have want to send a struct to json->setInformation but my program crashes when i try to copy the array which is inside the struct. The rest of the data is okay its just the array which makes the crash occur.

info = data->getInformation();

json->setInformation(info);

getInformation returns a struct which i can read in main.cpp when i try to send this struct to setInformation it crashes...

information.h which holds my struct

struct information{
        String location;
        String protocol;
        uint8_t groupID;
        uint8_t* data;

information& operator=(const struct information& that){
    location = that.location;
    protocol = that.protocol;
    groupID = that.groupID;
    for (int i = 0; i < 9; ++i){
        data[i] = that.data[i];
    }
    return *this;
}
};

json.cpp

void JSON::setInformation(information data){
info->location = data.location;
info->protocol = data.protocol;
info->groupID = data.groupID;
// for (int i = 0; i < 9; ++i){
//     info->data[i] = data.data[i];
// }
// Serial.print("after JSON: ");
// Serial.println(info->data[0]);
}

this code works fine but when i uncomment the for lop which should copy the array it crashes

Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67
Yoeri Kroon
  • 33
  • 1
  • 4
  • 3
    Your struct doesn't have an array. It has a pointer. Think about what the pointer points to. – juanchopanza Oct 19 '16 at 13:41
  • 1
    Where exactly are you setting the `data` field to point to a valid memory block of an appropriate size? – barak manos Oct 19 '16 at 13:48
  • 1
    Consider posting more code (specially about uint8_t array allocation/deallocation). – jpo38 Oct 19 '16 at 13:49
  • BTW, reading your code, it looks like you may as well declare `uint8_t data[9]`, and in the `operator=` function, simply do `return *this = that` (which, BTW, means that you don't even need this function, because structure-assignment is already supported by the language standard). – barak manos Oct 19 '16 at 13:50

2 Answers2

1

Did you allocate memory for your uint8_t data* parameter before using it ?

Then remember to deallocate memory when you don't need it anymore, thus avoiding memory leaks.

Dali
  • 344
  • 1
  • 10
0

Your object is passed by copy to the function, but you have no copy constructor.

Default copy constructor will not copy you raw pointer correctly. So either you declare and implement a copy constructor, either you replace your raw pointer (uint8_t*) by a vector (std::vector<uint8_t>) which is safely copyiable (then copying the object will become a valid operation).

Moreover, we can't see who's allocating/deallocating your raw pointer, but I suspect you are missing a destructor function too.

Your code breaks the rule of three which is the minimal requirement for any class you'll declare in C++.

jpo38
  • 20,821
  • 10
  • 70
  • 151
  • Care to mention [the rule of 3](https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)) in your answer? – Adrian Colomitchi Oct 19 '16 at 13:44
  • 1
    There's nothing in the code that shows that the lack of a copy constructor could be the problem. – juanchopanza Oct 19 '16 at 13:45
  • Or even [rule of 5](http://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11) – UKMonkey Oct 19 '16 at 13:45
  • The pointer would be copied perfectly well, the only issue that could happen is if it gets freed - and we can't see that. – UKMonkey Oct 19 '16 at 13:46
  • @juanchopanza: True, but there's also no allocation of the raw pointer, I suspect not all the code was posted. – jpo38 Oct 19 '16 at 13:47