HELLPPP! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

HELLPPP!

when i run this code on visual studio, it throws exceptional and set breakpoint (it says can't access disposed object) https://code.sololearn.com/c5m13Tzm5nrh/?ref=app

20th Oct 2018, 9:30 AM
Maleeha Khalid
Maleeha Khalid  - avatar
12 Answers
+ 3
Well, it's not that weird. In shallowCopy you copy the address only, so you now have 2 pointers pointing to the same location on the heap. When the program ends the destructor is called. destructor 1 deallocates it from the heap. destructor 2 now deallocates something that is already deallocated, causing memory corruption, which crashes the program. This is the problem std::shared_ptr solves. Internally it keeps track of a reference count and only calls delete when the counter hits 0. So it only calls delete once, even if there are 10000 copies and it comes with the added bonus that you don't have to worry about deallocation. https://en.cppreference.com/w/cpp/memory/shared_ptr 1. Replace int *z; with std::shared_ptr<int> z; 2. Use the constructor's initializer list to initialize z shallowCopy() : z(new int) {} 3. Remove delete z; from the destructor and 4. #include <memory>. Additionally the copy constructor should be const shallowCopy& instead of just shallowCopy&, dito for the deepCopy and int getPointer(){...} should be int getPointer() const{...}, const correctness is an important thing.
20th Oct 2018, 1:44 PM
Dennis
Dennis - avatar
+ 2
Here, I implemented it myself real quick, but just for 1 name. I removed some of your code duplication and saved strlen in a seperate variable to prevent strlen to be called twice, it's quite an expensive function, so you want to minimize its use. The size in strcpy_s needs strlen + 1, because of the null character, but since I added the 1 earlier, I don't have to do it again. I also gave first an identifier so you know it is a class member. Some people use _(varname) or m_(varname) but I just use m(varname), use whichever you want but make sure you use one. This so we don't have to use this-> as it can cause bugs when you forget it. But this way it will cause a compile error instead. Also delete[] mFirst on a nullptr causes it to not do anything, so it's not a bug there. Personally I think getFirstName etc shouldn't allocate something new as the name doesn't imply any form of allocation, this can cause memory leaks as the user doesn't know whether to call delete or not. If you want to allocate something new make sure you change the name so that it is more obvious, e.g. getAllocatedFirstName or something. https://code.sololearn.com/ccx19VvOc289/?ref=app
21st Oct 2018, 9:01 AM
Dennis
Dennis - avatar
+ 1
Code please :)
21st Oct 2018, 8:10 AM
Dennis
Dennis - avatar
+ 1
It doesn't really look like you send the full code, there is a missing class and a lot of missing ;'s. Anyway I notice you delete[] first, second and third in setName and delete[] first in setFirstName and then try to access those same variables. You CANNOT access variables after they are deleted. Well, C++ still lets you, but it's undefined behaviour. You sure you aren't meant to delete[] this->first etc?
21st Oct 2018, 8:28 AM
Dennis
Dennis - avatar
+ 1
I mean, you are doing it correctly in setSecondName and setThirdName but not in the other functions, so just change in setName: delete[]first -> delete[] this->first; delete[]second -> delete[] this->second; delete[]third -> delete[] this->third; and in setFirstName delete[] first -> delete[] this->first; and I think strcpy_s(this->first, strlen(this->first), first); should be strcpy_s(this->first, strlen(first) + 1, first); dito for all other char*'s. But I recommend you drop allllll the char* and replace them with std::string. I mean you are using C++ after all, not C.
21st Oct 2018, 8:38 AM
Dennis
Dennis - avatar
+ 1
Dennis i wish i could like your comment twice..Thank you so much for your time ..Now i got it completely :)))
21st Oct 2018, 9:18 AM
Maleeha Khalid
Maleeha Khalid  - avatar
0
Dennis got it, thank you..it also gives error in deepCopy whenever i delete the variable and then reaccolate it some memory. I don't know why?
21st Oct 2018, 7:58 AM
Maleeha Khalid
Maleeha Khalid  - avatar
0
I mean I am writing a program in which i initializes the name (first name and second name) in constructor on heap and then reassign it in setName function but to clear the grabage I've to delete the name and then reasign it the value through set function.. There are no errors but then when i debug it ,system crash at delete [ ] name. how can i prevent it? Dennis
21st Oct 2018, 8:05 AM
Maleeha Khalid
Maleeha Khalid  - avatar
21st Oct 2018, 8:17 AM
Maleeha Khalid
Maleeha Khalid  - avatar
0
Excuse me..it's a little bit messy but i quickly merge header file and cpp file from visual studio.
21st Oct 2018, 8:18 AM
Maleeha Khalid
Maleeha Khalid  - avatar
0
yes what I'm trying to do is to just remove whatever is in that array first is pointing to..and then resize it and assign it a name again..what's the better method to do it then? I've been struck at this for at least a week. Dennis
21st Oct 2018, 8:31 AM
Maleeha Khalid
Maleeha Khalid  - avatar
0
yes i know..strings are easy to handle but my professor says that they are not a good idea (I don't know why ).
21st Oct 2018, 8:42 AM
Maleeha Khalid
Maleeha Khalid  - avatar