Weird error

Pages: 12
Hi Guys,

I am having this weird problem,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class myClass {

public: 

  string s1;
  string s2; 
  string s3;
  string s4;
  string s5;

};

class mySecondClass {

public:

  string   s21         ;
  string   s22        ; 
  string   s23    ;
  string   s24   ;

};

class myThirdClass {

public:
char myChar[2000]; 

}; 
int main() { 

list<myClass> myList;                           
list<myClass> :: iterator myListIterator;

      mySecondClass 2ndObj; 
      myThirdClass  3rdClass;  
// Here, I have already initialized the list and members of all the 
// classes in my original code 

      myListIterator = myList.begin(); 
      memset(&2ndObj, NULL, sizeof(2ndObj));
      memcpy(&2ndObj, 3rdClassObj.myChar, sizeof(3rdClass.myChar));
                                                                                                  

      for (myListIterator = myList.begin(); myListIterator != myList.end(); myListIterator++) {
	   cout<<"T:"<<endl;
	   Show_L(myList); 
	   cout<<myListIterator->s1<<endl;
         }
return 0;
}


So now, the problem is, that when I try to loop through myListIterator = myList.begin() in my code above, it gives Bus Error, whereas if I remove
1
2
      memset(&2ndObj, NULL, sizeof(2ndObj));
            memcpy(&2ndObj, 3rdClassObj.myChar, sizeof(3rdClass.myChar));


it runs fine. I don't see what's going on.

Any thoughts?

Thanks ina advance
Last edited on
use memset with 0 instead of NULL, this will make the addresses invalid and you have already taken the iterator which will become invalid.
Same error man ?

Could you please explain what you meant by and you have already taken the iterator which will become invalid.

and how does NULL to 0 affects it?
Last edited on
wait let me check it on my machine then will reply. :)
oh.. now i looked it carefully:
how can you copy an array to an object!!?? this is not possible.

memcpy(&2ndObj, 3rdClassObj.myChar, sizeof(3rdClass.myChar));


this wont work..second object has 4 strings and you are trying to copy a char array to it.. where will it get copied??
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
using namespace std;

struct s {
int i;
int j;
string s1;
};
int main() {
s obj;
obj.i = 10;
obj.j = 20;
obj.s1 = "hello";
char arr[100];
memset(arr,NULL, sizeof(arr));
memcpy(arr,&obj,sizeof(obj));

s *obj2 = (s*) malloc (sizeof(s)); ;

memset(obj2, NULL, sizeof(obj2));
memcpy(obj2, arr, sizeof(arr));

cout<<obj2->i<<"   "<<obj2->j<<"    "<<obj2->s1;
cout<<endl;



return 0;
}


after that you do memset with NULL, this will make mylist adress to nothing.


I don't understand dude, what does it has to do with 2ndObject, I mean it is not even the instance of the same class, then why would memset affect it?

Please elaborate
no doubt it is possible..i was just wondering what u want to do with that..
infact that was not my ans to your main problem which is Bus error.

yes you are right if you copy correctly each byte will set correctly and will give correct results..your bus error is not coming due to this.

ok leave that thing.. what ever you want to do..lets come back to your error.

1
2
memset(&2ndObj, 0, sizeof(2ndObj));
memcpy(&2ndObj, 3rdClassObj.myChar, sizeof(3rdClass.myChar));


if you do this i think the error should not come.
is the error still coming after replacing the NULL with a zero.


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?
When I changed it to pointer it is working now.
Last edited on
i may be wrong..

in which code?
Ok, you cannot use malloc, free, memset, or memcpy with classes that contain strings. It will most definitely crash when you attempt to use it.

jsmith:

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?
Yes, he's correct.
kbw:
the program which you wrote above, will that crash? it wont!!
It corrupts the heap making the behaviour of the program non-deterministic and undefined. Just because it hadn't crashed yet doesn't mean all is well.
Last edited on
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.

bye
@jsmith
Can you please explain as why would it break?
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.

Pages: 12