944,108 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4731
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 15th, 2007
0

Why atof causes a C0000005 exeption?

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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):
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 15
Solved Threads: 1
Light Poster
Arctic wolf is offline Offline
47 posts
since Jul 2007
Aug 15th, 2007
1

Re: Why atof causes a C0000005 exeption?

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Aug 15th, 2007
0

Re: Why atof causes a C0000005 exeption?

lines 13 and 14 are incorrect. Should be this:
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Aug 15th, 2007
0

Re: Why atof causes a C0000005 exeption?

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.
Reputation Points: 15
Solved Threads: 1
Light Poster
Arctic wolf is offline Offline
47 posts
since Jul 2007
Aug 15th, 2007
0

Re: Why atof causes a C0000005 exeption?

"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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 15th, 2007
0

Re: Why atof causes a C0000005 exeption?

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Aug 15th, 2007
0

Re: Why atof causes a C0000005 exeption?

Salem wrote:
Quote ...
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.
Reputation Points: 15
Solved Threads: 1
Light Poster
Arctic wolf is offline Offline
47 posts
since Jul 2007
Aug 15th, 2007
0

Re: Why atof causes a C0000005 exeption?

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:
C++ Syntax (Toggle Plain Text)
  1. stringstream(buffer)>>num;
But I developed my own way to convert from string to number(using very simple formula acctualy).


Salem wrote:
Quote ...
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.
Reputation Points: 15
Solved Threads: 1
Light Poster
Arctic wolf is offline Offline
47 posts
since Jul 2007
Aug 15th, 2007
0

Re: Why atof causes a C0000005 exeption?

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 16th, 2007
0

Re: Why atof causes a C0000005 exeption?

...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
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: creating dynamic array
Next Thread in C++ Forum Timeline: about main()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC