Chat on IRC |
Damn Access violation Error in C++
Posted by csspcman [send private reply] at July 18, 2002, 11:32:50 PM
when I use cin.get() I get an Access violation error on run time . I am working with vc++ 6 under windows 2000 pro
how can I avoid this problem? tnx
Posted by Zandalf [send private reply] at July 19, 2002, 12:48:23 AM
you're going to have to describe a little bit more about the porgram you're writing, because access violations can stem from a variety of causes.
Posted by csspcman [send private reply] at July 19, 2002, 01:34:06 AM
OK here it is , the source code
//: C03:Static.cpp // Using a static variable in a function #include <iostream> using namespace std; int main() { char* ch; cin.get(ch,10,'A'); cout << ch; for(;;); } ///:~
Posted by buzgub [send private reply] at July 19, 2002, 01:46:12 AM
csspcman, ch does not point to anything. You might like to change it's declaration to char ch[10] or allocate some memory for it with malloc or new or somesuch; you might also want to use the string object that I believe should be in the C++ standard library on your system. I've never used that, though, so perhaps someone who has can provide some suggestions, hopefully not involving GNU software at all...
Posted by Zandalf [send private reply] at July 19, 2002, 01:53:35 AM
yeah, buzgub is right. unless you use
char ch[10]; OR char* ch = new char[10]; you've got a NULL pointer, and you can't write to NULL... also, be careful about couting that, because you aren't guaranteed a terminating NULL from your cin statement, and so you'll get another access violation.
Posted by buzgub [send private reply] at July 19, 2002, 02:00:58 AM
That's not quite right, Zandalf. It's not a NULL pointer, it's a pointer that points to some unpredictable bit of memory. I think with the second example there you'd also need a delete somewhere to avoid causing memory leaks.
Posted by unknown_lamer [send private reply] at July 19, 2002, 09:04:22 AM
And NULL is deprecated in C++ anyway :P (you are supposed to use 0).
Posted by Zandalf [send private reply] at July 19, 2002, 01:05:15 PM
Okay, point noted, it should be automtically pointed to NULL though (just good coding practice)
I suppose where it actually points is based entirely on the compiler. And I miss NULL, silly 0's....
Posted by CViper [send private reply] at July 19, 2002, 01:39:55 PM
in debug mode vc++ automaticly initializes values with some silly numbers so you're almost guaranteed to get some errors/access violations.
And i still use NULL... and i'd guess some people still stick to their 'Nil' :)
Posted by csspcman [send private reply] at July 22, 2002, 03:25:37 PM
any way it 's not a null pointer but these don't help me . I think vc++ is a stupid compiler . this code works under linux but how come it doesn't with vc++.
Posted by Zandalf [send private reply] at July 22, 2002, 03:29:11 PM
try this code instead:
int main(void) { char* ch = new char[11]; ch[10] = 0; //that's so you can't have a non-terminated string cin.get(ch, 10, 'A'); cout << ch; while(1); } VC++ has always worked just fine for me, which version are you using?
Posted by CViper [send private reply] at July 22, 2002, 03:48:54 PM
Yeah.. blame the compiler/IDE/whatever :)
Anyway make the array 11 char's, or change the cin.get() to cin.get( ch, 9, 'A' ); You always need one more char to store the terminating zero. And i can't belive the original code would work on linux.. It shouldn't work at all.
Posted by vladimir_l [send private reply] at July 22, 2002, 04:01:58 PM
cin.get() that like getchar() right ??? I havent been coding C++ for a while. Anyway get a simpler compielr I say , I've had stupid errors b4 with VC++ too.
Register as a new user | |||||||||||||||