Click to See Complete Forum and Search --> : This is screwed up big time...
PolteRGeisT
03-02-2003, 01:55 PM
Ok, I want you all to run and compile this program, first as is, then take out the comment. the person who figures out why it does as it does is a genious.
#include <iostream.h>
#include <stdlib.h>
int main(void)
{
char *s;
while(1) {
cin >> s;
cout << 1 << endl;
cout << atol(s) << endl;
cout << 2 << endl;
cout << atoi(s) << endl;
cout << 3 << endl;
//cout << atof(s) << endl;
cout << 4 << endl;
}
return 0;
}
Arcane_Disciple
03-02-2003, 02:44 PM
When I tried it the way its posted it wouldn't run. It couldn't open the stdlib.h. Here's what I did to make run.
#include <iostream>
using namespace std;
int main(void)
{
char *s;
while(1) {
cin >> s;
cout << 1 << endl;
cout << atol(s) << endl;
cout << 2 << endl;
cout << atoi(s) << endl;
cout << 3 << endl;
cout << atof(s) << endl;
cout << 4 << endl;
}
return 0;
}
mindcooler
03-02-2003, 02:59 PM
I don't know what you had hoped to achieve with this program, but you are using cin on a pointer pointing to garbage. What happens is undefined, probably segmentation fault. And <iostream.h> and <stdlib.h> are deprecated, use <iostream> and <cstdlib>, respectively.
PolteRGeisT
03-02-2003, 03:57 PM
I understand what's going on with respect to the un-initialized pointer. What I don't understand is WHY IT WORKS with the program I provided. When you take the comment away, it doesn't work, but in a very odd way.
mindcooler
03-02-2003, 04:12 PM
Results are undefined and should not be relied upon. I get a segfault right away, comment or no.
PolteRGeisT
03-02-2003, 04:35 PM
Very odd, because when I have the comment in there, the program goes through normally. but when the call to atof is added to the program, it segfaults right after the cin statement. :confused: confusion :confused:
PolteRGeisT
03-02-2003, 04:36 PM
I'm compiling with g++ 2.9.6 if that means anything.
mindcooler
03-02-2003, 04:59 PM
Well, what can I say? The result is undefined..anything might happen. Don't rely on undefined behaviour, and don't worry about this stupid program anymore. :)
fishhead
03-03-2003, 12:38 PM
My absolute wild guess is that the program places char * s, a 32 bit pointer at the top of the stack. atol and atoi should both give back 32-bit integers, atof reads the pointer as a double, a 64-bit value ... which might just read past the bottom of the stack, violating memory protection .... That's my best random guess.
infidel
03-03-2003, 12:59 PM
Greetings,
First of all, mincooler already pointed out the header files problems and the uninitialized pointer wich is making you program crash.
Now, and since you are working in C++ you should avoid char* for strings. STL provides <string> wich is great for this type of thing, because (among many other things) it takes care of initializing itself so you don't have to worry about uninitialized/unallocated pointers.
Another thing, atof, atoi, and similars are not standart, and should be avoided.
To convert from numeric to string you should use stringstreams, e.g.
#include <sstream> // std::ostringstream
#include <string> // std::string;
std::string to_string(float f)
{
std::ostringstream os;
os << f;
return os.c_str();
}
Of course a templatized version would be much more usefull as it would allow to convert other numeric types as well, or any other streamable type.
spacedog
03-03-2003, 11:09 PM
hello,
fishhead that was a pretty good guess, going on what you said to make poltergiest program work all you have to do is cast atof(s) to a float and everything will work fine. I also have 2.96
and just out of curiosity, does it really matter if you use iostream.h or iostream. anyway stdio is much better
#include <iostream.h>
#include <stdlib.h>
int main(void)
{
char *s;
while(1) {
cin >> s;
cout << 1 << endl;
cout << atol(s) << endl;
cout << 2 << endl;
cout << atoi(s) << endl;
cout << 3 << endl;
cout << (float)atof(s) << endl;
cout << 4 << endl;
}
return 0;
}
spacedog
bwkaz
03-04-2003, 03:02 PM
Originally posted by spacedog
and just out of curiosity, does it really matter if you use iostream.h or iostream. Yes, it does. iostream.h is deprecated. It's going to go away sometime soon here. Which means any programs still using it will not compile once that happens.
anyway stdio is much better You think so. :p
fishhead
03-05-2003, 12:39 AM
Originally posted by spacedog
hello,
fishhead that was a pretty good guess, going on what you said to make poltergiest program work all you have to do is cast atof(s) to a float and everything will work fine. I also have 2.96
and just out of curiosity, does it really matter if you use iostream.h or iostream. anyway stdio is much better
#include <iostream.h>
#include <stdlib.h>
int main(void)
{
char *s;
while(1) {
cin >> s;
cout << 1 << endl;
cout << atol(s) << endl;
cout << 2 << endl;
cout << atoi(s) << endl;
cout << 3 << endl;
cout << (float)atof(s) << endl;
cout << 4 << endl;
}
return 0;
}
spacedog
Supposing that's the problem, it may not fix it, since it will still try to read a double before converting it into a float.
fishhead
03-05-2003, 04:57 AM
Why not have g++ produce assembly output, then we can ACTUALY figure out what's going on ...
infidel
03-05-2003, 06:35 AM
Greetings,
The problem with it is, apart from not using the standart header and functions, you are not initializing 'char *s;'. Its pointing nowhere valid and when you try to use it the crashes!
Try:
#include <iostream.h>
#include <stdlib.h>
int main(void)
{
char *s = new char[10]; // Initialize the pointer allocating enough space to hold the input
while(1) {
cin >> s;
cout << 1 << endl;
cout << atol(s) << endl;
cout << 2 << endl;
cout << atoi(s) << endl;
cout << 3 << endl;
cout << (float)atof(s) << endl;
cout << 4 << endl;
}
delete [] s; // Free the memory you allocated
return 0;
}
Now, it should work.
spacedog
03-05-2003, 01:17 PM
it was the problem... and it fixed it.