| | |
Why atof causes a C0000005 exeption?
![]() |
•
•
Join Date: Jul 2007
Posts: 47
Reputation:
Solved Threads: 1
Hello,
I did some expiremets with turning numbers with floating point to strings and vice versa,and bumped into something strange,here is the code:
from some reason the folowing line causes a C0000005 exeption(if I understand correctly it means acces violation):
why does it happen?
p.s.
the intresting part is that when I compile it shows no errors and no warnings,
the problem appears only when I build.
I did some expiremets with turning numbers with floating point to strings and vice versa,and bumped into something strange,here is the code:
C++ Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main () { double num; num=310.589; char buffer [10]; sprintf(buffer,"%f",num); buffer[7]=NULL; printf("using &num=\n"); printf((char *)&num); printf("\nbuffer=\n"); printf(buffer); printf("\nfragment of buffer=\n"); printf((char *)&buffer[3]); scanf("%s",buffer); num=atof(buffer); num/=2; printf("\n%lf",num); _getch(); }
C++ Syntax (Toggle Plain Text)
num=atof(buffer);
p.s.
the intresting part is that when I compile it shows no errors and no warnings,
the problem appears only when I build.
Last edited by Ancient Dragon; Aug 15th, 2007 at 10:47 am. Reason: add line numbers to code tags
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
C++ Syntax (Toggle Plain Text)
printf((char *)&num);
C++ Syntax (Toggle Plain Text)
printf( "%f", num ) ;
atof is to be shunned; it has many problems. you would be much better off (and less likely to make errors) if you use C++ facilities where they are available. eg.
C++ Syntax (Toggle Plain Text)
#include <string> #include <iostream> #include <sstream> using namespace std ; /*void*/ int main() { double num = 310.589 ; /* char buffer [10]; */ string buffer ; /* sprintf(buffer,"%f",num); */ { ostringstream stm ; stm << num ; buffer = stm.str() ; } // buffer[7] = *NULL; /* printf("using &num=\n"); printf((char *)&num); printf("\nbuffer=\n"); printf(buffer); printf("\nfragment of buffer=\n"); printf((char *)&buffer[3]); */ cout << "using &num= " << &num << "\nbuffer= " << buffer << "\nfragment of buffer= " << &( buffer.c_str()[3] ) << '\n' ; /* scanf("%s",buffer); */ cin >> buffer ; /* num=atof(buffer); */ { istringstream stm(buffer) ; stm >> num ; if( !stm ) cerr << "conversion failed\n" ; } num/=2; /* printf("\n%lf",num); */ cout << num << '\n' ; /* _getch(); */ char ch ; cin >> ch ; }
C++ Syntax (Toggle Plain Text)
#include <string> #include <iostream> #include <sstream> #include <boost/lexical_cast.hpp> using namespace std ; using namespace boost ; int main() { double num = 310.589 ; string buffer = lexical_cast<string>(num) ; cout << "using &num= " << &num << "\nbuffer= " << buffer << "\nfragment of buffer= " << &( buffer.c_str()[3] ) << '\n' ; cin >> buffer ; try { num = lexical_cast<double>(buffer) ; } catch( const bad_lexical_cast& ) { cerr << "cast error\n" ; } num/=2; cout << num << '\n' ; char ch ; cin >> ch ; }
Last edited by vijayan121; Aug 15th, 2007 at 10:51 am.
lines 13 and 14 are incorrect. Should be this:
Your line 14 will probably cause access violation because printf() assums the first parameter is an ascii null-terminated string, and passing a pointer to a variable is not that kind of thingy.
[edit] ^^^ what vijayan121 said too. [/edit]
C++ Syntax (Toggle Plain Text)
printf("using &num= %p\n", &num);
Your line 14 will probably cause access violation because printf() assums the first parameter is an ascii null-terminated string, and passing a pointer to a variable is not that kind of thingy.
[edit] ^^^ what vijayan121 said too. [/edit]
Last edited by Ancient Dragon; Aug 15th, 2007 at 10:51 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jul 2007
Posts: 47
Reputation:
Solved Threads: 1
Well,I can sure tell you that it's not because of lines 13 and 14.
(first of all the program worked just fine with those lines before I added atof(),second of all when you talled me they can make trouble
I removed them completley from the code,and rebuilded the project,
it still gave the C0000005 exeption).
The realy weird part is that it works just fine in 'debbug' mode,
the problem is only in'release' mode,why is that?
oh,and thank you both for your advice,
vijayan121 I will try to do it without atof() as you suggested,thanks.
(first of all the program worked just fine with those lines before I added atof(),second of all when you talled me they can make trouble
I removed them completley from the code,and rebuilded the project,
it still gave the C0000005 exeption).
The realy weird part is that it works just fine in 'debbug' mode,
the problem is only in'release' mode,why is that?
oh,and thank you both for your advice,
vijayan121 I will try to do it without atof() as you suggested,thanks.
Last edited by Arctic wolf; Aug 15th, 2007 at 3:37 pm.
"Working" != "Bug Free".
Just because it didn't crash immediately on trying
Your program was broken at "void main" and the rest is just mere accident as far as I can see.
Start with the first identified problem and work your way through them.
> the intresting part is that when I compile it shows no errors and no warnings,
If you add enough casts, you can compile any old rubbish.
This is no exception.
> The realy weird part is that it works just fine in 'debbug' mode,
> the problem is only in'release' mode,why is that?
Because your code has undefined behaviour, which means any combination of working, not quite working, reformatting your hard disk, crashing in a heap is permissible.
Getting different results from different builds is a sure sign of doing something wrong.
If your code is correct, the ONLY difference between debug and release should be performance, nothing else.
Just because it didn't crash immediately on trying
printf((char *)&num); doesn't mean that you can always get away with it. It is plainly wrong even if it didn't crash in your specific case.Your program was broken at "void main" and the rest is just mere accident as far as I can see.
Start with the first identified problem and work your way through them.
> the intresting part is that when I compile it shows no errors and no warnings,
If you add enough casts, you can compile any old rubbish.
This is no exception.
> The realy weird part is that it works just fine in 'debbug' mode,
> the problem is only in'release' mode,why is that?
Because your code has undefined behaviour, which means any combination of working, not quite working, reformatting your hard disk, crashing in a heap is permissible.
Getting different results from different builds is a sure sign of doing something wrong.
If your code is correct, the ONLY difference between debug and release should be performance, nothing else.
Another problem I see in the original code is line 11 -- you have to make sure that the buffer size is big enough to hold all the digits that sprintf() will want to put into it. In your code buffer can only hold 9 characters plus the null terminator but sprintf() will attempt to fill it with 10 characters plus null terminator (310.589???) because the default is to display 6 decimals. So sprintf() at line 11 will cause buffer overflow and that function will scribble the remaining characters all over other memory.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jul 2007
Posts: 47
Reputation:
Solved Threads: 1
Salem wrote:
Salem,as I already told,I deleted lines 13 and 14 completley from my code and rebuilded,it still gave the same error,so you can't blame those lines in this particular case.
The problem was solved when I replaced atof with strimstring.
As about the difference between 'debbug' and 'release',it's
not the first time I encounter with it on this particular compiler(VC++ 6.0).
You will be surprised to hear that when I compiled one of my programs
with the warnings set to level 4(highest),it's actually found 60 warnings
in a header file that I recived with the compiler!
They acctualy wrote headers that collide with their own compiler!
Ancient dragon,the intresting thing that it's worked as is the moment I replaced atof,but thanks I'll fix the code.
•
•
•
•
Working" != "Bug Free".
Just because it didn't crash immediately on trying printf((char *)&num); doesn't mean that you can always get away with it. It is plainly wrong even if it didn't crash in your specific case.
Your program was broken at "void main" and the rest is just mere accident as far as I can see.
Start with the first identified problem and work your way through them.
> the intresting part is that when I compile it shows no errors and no warnings,
If you add enough casts, you can compile any old rubbish.
This is no exception.
> The realy weird part is that it works just fine in 'debbug' mode,
> the problem is only in'release' mode,why is that?
Because your code has undefined behaviour, which means any combination of working, not quite working, reformatting your hard disk, crashing in a heap is permissible.
Getting different results from different builds is a sure sign of doing something wrong.
If your code is correct, the ONLY difference between debug and release should be performance, nothing else.
The problem was solved when I replaced atof with strimstring.
As about the difference between 'debbug' and 'release',it's
not the first time I encounter with it on this particular compiler(VC++ 6.0).
You will be surprised to hear that when I compiled one of my programs
with the warnings set to level 4(highest),it's actually found 60 warnings
in a header file that I recived with the compiler!
They acctualy wrote headers that collide with their own compiler!
Ancient dragon,the intresting thing that it's worked as is the moment I replaced atof,but thanks I'll fix the code.
•
•
Join Date: Jul 2007
Posts: 47
Reputation:
Solved Threads: 1
Rechecked the program,please accept correction,using stringstream
didn't gave any errors while building BUT it doesn't work the value of num doesn't change,remaines the same as at the beggining,the compiler just ignores that line: But I developed my own way to convert from string to number(using very simple formula acctualy).
Salem wrote:
Always wanted to ask you why it's wrong to write a main function that doesn't return a value(void mainer's are doomed...) ?
And if I make it return a value,where does that value arrive?
didn't gave any errors while building BUT it doesn't work the value of num doesn't change,remaines the same as at the beggining,the compiler just ignores that line:
C++ Syntax (Toggle Plain Text)
stringstream(buffer)>>num;
Salem wrote:
•
•
•
•
Your program was broken at "void main" and the rest is just mere accident as far as I can see.
And if I make it return a value,where does that value arrive?
Last edited by Arctic wolf; Aug 15th, 2007 at 6:58 pm.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
•
•
•
•
...why it's wrong to write a main function that doesn't return a value(void mainer's are doomed...) ? And if I make it return a value,where does that value arrive?
also have a look at: http://www.research.att.com/~bs/bs_f...#int-to-string
http://www.research.att.com/~bs/new_learning.pdf
![]() |
Similar Threads
- atof ??? how can I change from user? (C)
- URGENT!! atof, strtof, strod ...drivin me nuts!! (C)
- Fatal Error, c0000005 Memory could not be read??? (Windows NT / 2000 / XP)
- compilers in C (C++)
- HJT Log - XP SP2, IE6 Problems (Viruses, Spyware and other Nasties)
- The Calculator (C++)
- win2k pro taskmanager won't run (Windows NT / 2000 / XP)
- Creating and destroying objects (C++)
Other Threads in the C++ Forum
- Previous Thread: creating dynamic array
- Next Thread: about main()
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






