Why atof causes a C0000005 exeption?

Reply

Join Date: Jul 2007
Posts: 47
Reputation: Arctic wolf is an unknown quantity at this point 
Solved Threads: 1
Arctic wolf Arctic wolf is offline Offline
Light Poster

Why atof causes a C0000005 exeption?

 
0
  #1
Aug 15th, 2007
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:
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. void main ()
  6. {
  7. double num;
  8. num=310.589;
  9. char buffer [10];
  10.  
  11. sprintf(buffer,"%f",num);
  12. buffer[7]=NULL;
  13. printf("using &num=\n");
  14. printf((char *)&num);
  15. printf("\nbuffer=\n");
  16. printf(buffer);
  17. printf("\nfragment of buffer=\n");
  18. printf((char *)&buffer[3]);
  19. scanf("%s",buffer);
  20. num=atof(buffer);
  21. num/=2;
  22. printf("\n%lf",num);
  23. _getch();
  24. }
from some reason the folowing line causes a C0000005 exeption(if I understand correctly it means acces violation):
  1. num=atof(buffer);
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.
Last edited by Ancient Dragon; Aug 15th, 2007 at 10:47 am. Reason: add line numbers to code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Why atof causes a C0000005 exeption?

 
1
  #2
Aug 15th, 2007
  1. printf((char *)&num);
is clearly an error; you are casting a double* to a char* (which is incorrect). unpleasant consequences would be the norm if you do such things. in C, to print out a double value, use
  1. printf( "%f", num ) ;
instead.
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.
  1. #include <string>
  2. #include <iostream>
  3. #include <sstream>
  4. using namespace std ;
  5.  
  6. /*void*/ int main()
  7. {
  8. double num = 310.589 ;
  9. /* char buffer [10]; */ string buffer ;
  10. /* sprintf(buffer,"%f",num); */
  11. { ostringstream stm ; stm << num ; buffer = stm.str() ; }
  12. // buffer[7] = *NULL;
  13. /*
  14.   printf("using &num=\n");
  15.   printf((char *)&num);
  16.   printf("\nbuffer=\n");
  17.   printf(buffer);
  18.   printf("\nfragment of buffer=\n");
  19.   printf((char *)&buffer[3]);
  20.   */
  21. cout << "using &num= " << &num << "\nbuffer= " << buffer
  22. << "\nfragment of buffer= " << &( buffer.c_str()[3] ) << '\n' ;
  23. /* scanf("%s",buffer); */ cin >> buffer ;
  24. /* num=atof(buffer); */
  25. {
  26. istringstream stm(buffer) ;
  27. stm >> num ;
  28. if( !stm ) cerr << "conversion failed\n" ;
  29. }
  30. num/=2;
  31. /* printf("\n%lf",num); */ cout << num << '\n' ;
  32. /* _getch(); */ char ch ; cin >> ch ;
  33. }
if you are preparing yourself for c++0x (you really should; final draft should be out in a few months), you could write
  1. #include <string>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <boost/lexical_cast.hpp>
  5. using namespace std ;
  6. using namespace boost ;
  7.  
  8. int main()
  9. {
  10. double num = 310.589 ;
  11. string buffer = lexical_cast<string>(num) ;
  12. cout << "using &num= " << &num << "\nbuffer= " << buffer
  13. << "\nfragment of buffer= " << &( buffer.c_str()[3] ) << '\n' ;
  14. cin >> buffer ;
  15. try { num = lexical_cast<double>(buffer) ; }
  16. catch( const bad_lexical_cast& ) { cerr << "cast error\n" ; }
  17. num/=2;
  18. cout << num << '\n' ;
  19. char ch ; cin >> ch ;
  20. }
note: lexical_cast used here is from boost; it would be part of c++0x
Last edited by vijayan121; Aug 15th, 2007 at 10:51 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,148
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Why atof causes a C0000005 exeption?

 
0
  #3
Aug 15th, 2007
lines 13 and 14 are incorrect. Should be this:
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 47
Reputation: Arctic wolf is an unknown quantity at this point 
Solved Threads: 1
Arctic wolf Arctic wolf is offline Offline
Light Poster

Re: Why atof causes a C0000005 exeption?

 
0
  #4
Aug 15th, 2007
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.
Last edited by Arctic wolf; Aug 15th, 2007 at 3:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Why atof causes a C0000005 exeption?

 
0
  #5
Aug 15th, 2007
"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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,148
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Why atof causes a C0000005 exeption?

 
0
  #6
Aug 15th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 47
Reputation: Arctic wolf is an unknown quantity at this point 
Solved Threads: 1
Arctic wolf Arctic wolf is offline Offline
Light Poster

Re: Why atof causes a C0000005 exeption?

 
0
  #7
Aug 15th, 2007
Salem wrote:
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 47
Reputation: Arctic wolf is an unknown quantity at this point 
Solved Threads: 1
Arctic wolf Arctic wolf is offline Offline
Light Poster

Re: Why atof causes a C0000005 exeption?

 
0
  #8
Aug 15th, 2007
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:
  1. stringstream(buffer)>>num;
But I developed my own way to convert from string to number(using very simple formula acctualy).


Salem wrote:
Your program was broken at "void main" and the rest is just mere accident as far as I can see.
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?
Last edited by Arctic wolf; Aug 15th, 2007 at 6:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Why atof causes a C0000005 exeption?

 
0
  #9
Aug 15th, 2007
I've no idea what your code looks like now then. Describing your edits doesn't work for me.

Post your latest code and latest observations.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Why atof causes a C0000005 exeption?

 
0
  #10
Aug 16th, 2007
Originally Posted by Arctic wolf View Post
...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?
see: http://www.research.att.com/~bs/bs_faq2.html#void-main
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC