I have a DoublyLinkedList object. I create an instance of it and use the push method.
DoublyLinkedList test; test.push("C++_мультипарадигмальный_1_1983_check"); The part after which everything starts to go wrong:
void DoublyLinkedList::push(char* parameters) { List_element* tmp = (List_element*)malloc(sizeof(List_element)); *tmp = List_element(parameters); //. . . } Fields "normal" are normal and filled. Numbers, logical. But char * somehow breaks. Referring to the same address (at least as Visual Studio says) the values in the string for some reason change to something like "EEEEEE"
The constructor looks like this:
List_element::List_element(char* fields) { char* iterator, *fields_copy = fields, *temp; unsigned i; iterator = strchr(fields_copy, '_'); i = iterator - fields_copy; name = (char*)malloc(sizeof(char)*(i+1)); for (int z = 0; z < i; z++) { name[z] = fields[fields_copy - fields + z]; } name[i] = '\0'; fields_copy = iterator + 1; iterator = strchr(fields_copy, '_'); i = iterator - fields_copy; paradigm = (char*)malloc(sizeof(char)*(i+1)); for (int z = 0; z < i; z++) { paradigm[z] = fields[fields_copy - fields + z]; } paradigm[i] = '\0'; fields_copy = iterator + 1; is_compiled = fields_copy[0] - 48; fields_copy = fields_copy + 2; iterator = strchr(fields_copy, '_'); i = iterator - fields_copy; temp = (char*)malloc(sizeof(char)*(i+1)); for (int z = 0; z < i; z++) { temp[z] = fields[fields_copy - fields + z]; } temp[i] = '\0'; year_created = atoi(temp); fields_copy = iterator + 1; iterator = strchr(fields_copy, '\0'); i = iterator - fields_copy; description = (char*)malloc(sizeof(char)*(i+1)); for (int z = 0; z < i; z++) { description[z] = fields[fields_copy - fields + z]; } description[i] = '\0'; next = nullptr; prev = nullptr; } In general, I simply process the string, write the values from the fields to the name field. The algorithm is working, I do not even know what the error is. Tell me please.
The biggest suspicion of this line:
*tmp = List_element(parameters); The following unit test passes normally:
TEST_METHOD(Object_List_element_should_be_created_correctly) { //Arrange List_element list_elem("C++_мультипарадигмальный_1_1983_check"); char* name_test = "C++"; char* paradigm_test = "мультипарадигмальный"; bool is_compiled_test = true; short year_test = 1983; char* description_test = "check"; //Act char* name = list_elem.getName(); char* paradigm = list_elem.getParadigm(); bool is_compiled = list_elem.getIs_compiled(); short year_created = list_elem.getYear_created(); char* description = list_elem.getDescription(); //Assert Assert::AreEqual(strcmp(name_test, name), 0); Assert::AreEqual(strcmp(paradigm_test, paradigm), 0); Assert::AreEqual(is_compiled_test, is_compiled); Assert::AreEqual(year_test, year_created); Assert::AreEqual(strcmp(description_test, description), 0); } UPD:
class List_element { char* name; char* paradigm; bool is_compiled; short year_created; char* description; List_element* next; List_element* prev; public: List_element(char* fields); ~List_element(); char* getName(); char* getParadigm(); bool getIs_compiled(); short getYear_created(); char* getDescription(); List_element* getNext(); List_element* getPrev(); void setNext(List_element* next); void setPrev(List_element* prev); };
List_element.malloc, of course, is not good, but the matter is hardly in it. But the implementation ofList_elementworth a look - the assignment operator. - Harry*tmp, you have a NULL pointer indicating at least the already freed memory. - Harry