and you have already taken the iterator which will become invalid
by this i mean..when you assing some value or pointer NULL it points to nothing.
now you have taken an iterator from mylist first. it might be pointing to some address.
after that you do memset with NULL, this will make mylist adress to nothing.
but iterator has some address which is now nothing.. as it try to access that address it will fault.
Sir, it IS possible. I am taking char because it's size is 1 byte.
And if you look in terms of memory then the array is just holding the amount of data doesn't matter what type. As a matter of fact, when I do 2ndObj.s21 it will print whatever it has in it.
Perhaps you need to check out before posting .. lolz
Anyways, look this program:
is the error still coming after replacing the NULL with a zero.
Yes. Between, why does replacing NULL will 0 will make a difference? Correct me if I am wrong, but NULL is basically a C thing and replacing NULL with 0 was C++ thing? So how does it make a difference if I write 0 or NULL?
For ex.
1 2 3 4 5 6 7 8 9 10
int main() {
if (NULL == 0){
cout<<"True";
}
return 0;
}
Above code, outputs TRUE.
But anyways, I just noticed that when I am writing endl after the statement, it is causing some weird error. Any idea why?
is it so?
it may give wrong output but i doubt if it will crash..
if you talk about about virtual classes then it might as it may overwrite rtti information but for normal class it wont!! what you say?
please check the line
memcpy(&2ndObj, 3rdClassObj.myChar, sizeof(3rdClass.myChar));
how you can be sure that the 2ndObj is big enough to cover 3rdClass size?
You overwrite a memory therefore your program crashes.
Secondly, objects are initialized by constructors, your kind of init is unusual.
malloc() does not run the constructor. Although neither of OP's classes have their own constructors, string does have one, and it has to run to properly initialize each of the
data members of OP's classes.
memset() doesn't work for the same reason: it blindly zeroes out the data members of the string object which may or may not like being zero.