| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"diadia" |
| Date: |
19 Nov 2003 05:27:19 AM |
| Object: |
about new problem |
#include <iostream>
using namespace std;
void main()
{
char *a;
a = new char[1];
cin >> a;
cout << a;
}
I don't know why this code can work and don't occur segmentation
error?
--
[1;32m¡¼ Origin: [33mGAIS¤ý´Â [37mgais.twbbs.org [31m¡¼ From: [36mgaisant8.cs.ccu.edu.tw[m
.
|
|
| User: "chris" |
|
| Title: Re: about new problem |
27 Nov 2003 09:58:24 AM |
|
|
(diadia) writes:
#include <iostream>
using namespace std;
void main()
{
char *a;
a = new char[1];
cin >> a;
cout << a;
}
Just a pointer (pun intended), your main function should read ``int main()'' as
main returns an int per The Standard.
--
iaebg@sdf.lonestar.org
http://iaebg.freeshell.org
SDF Public Access UNIX System - http://sdf.lonestar.org
.
|
|
|
|
| User: "Karl Heinz Buchegger" |
|
| Title: Re: about new problem |
19 Nov 2003 06:30:53 AM |
|
|
diadia wrote:
#include <iostream>
using namespace std;
void main()
{
char *a;
a = new char[1];
cin >> a;
cout << a;
}
I don't know why this code can work and don't occur segmentation
error?
You mean, when you enter something?
Well. Technically you write beyond the allocated memory. What happens
in such a case is 'undefined behaviour', meaning: anything can happen.
A segmentation error or access violation is just one possibility among
many others and hopefully you have learned by this example, that the
absence of an access violation (which usually is detected by the operating
system and not by the program runtime system) doesn't mean that your
program is correct. The program you wrote still has a problem but
in this specific case it went by unnoticed by the programmer. In other
cases or when you start altering the program and add things to it the error
may manifest itself in strange results which seem to be completely illogical
until you figure out that writing beyond allocation has happend.
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.
|
|
|
|
| User: "Marc Schellens" |
|
| Title: Re: about new problem |
19 Nov 2003 05:33:47 AM |
|
|
diadia wrote:
#include <iostream>
using namespace std;
void main()
{
char *a;
a = new char[1];
cin >> a;
cout << a;
}
I don't know why this code can work and don't occur segmentation
error?
probably you meant cin >> *a instead of cin >> a.
This code is fine, reads a pointer value from cin and writes it to cout.
The new char[1] is irrelevant.
Your main function is not C++ standart conform BTW.
Greetings,
marc
.
|
|
|
| User: "Chris Theis" |
|
| Title: Re: about new problem |
19 Nov 2003 07:38:38 AM |
|
|
"Marc Schellens" <m_schellens@hotmail.com> wrote in message
news:3FBB551B.3090005@hotmail.com...
diadia wrote:
#include <iostream>
using namespace std;
void main()
{
char *a;
a = new char[1];
cin >> a;
cout << a;
}
I don't know why this code can work and don't occur segmentation
error?
probably you meant cin >> *a instead of cin >> a.
This code is fine, reads a pointer value from cin and writes it to cout.
No, it is not. What do you mean by it reads a pointer value from cin? The OP
wants to read in a character!
The new char[1] is irrelevant.
It's certainly not because without it char *a only declares a pointer
without allocating any memory. Storing something in "a" without memory
allocation will bring you one step further towards nirvana :-)
Generally the safest idea to read in characters/strings from streams is to
use the string class. If you need/want to you can still copy them to a
character array afterwards, though I can't really think of a good reason why
one should want to do that.
Your main function is not C++ standart conform BTW.
That's right.
Chris
.
|
|
|
| User: "David Rubin" |
|
| Title: Re: about new problem |
19 Nov 2003 12:25:55 PM |
|
|
Chris Theis wrote:
probably you meant cin >> *a instead of cin >> a.
This code is fine, reads a pointer value from cin and writes it to cout.
No, it attempts to read a string. You would have to declare
void *a;
to read a pointer, IMO.
No, it is not. What do you mean by it reads a pointer value from cin? The OP
wants to read in a character!
Are you sure?
To OP: the program apparanly works due to pure luck. Since you do not
allocate enough space for a null terminator, your program exhibits UB.
/david
--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."
.
|
|
|
| User: "Chris Theis" |
|
| Title: Re: about new problem |
20 Nov 2003 02:23:42 AM |
|
|
"David Rubin" <fullname@warpmail.net> wrote in message
news:TAOub.124113$Gq.16569263@twister.nyc.rr.com...
Chris Theis wrote:
[SNIP]
No, it is not. What do you mean by it reads a pointer value from cin?
The OP
wants to read in a character!
Are you sure?
No I'm not, that's why I recommended that string class :-)
To OP: the program apparanly works due to pure luck. Since you do not
allocate enough space for a null terminator, your program exhibits UB.
.
|
|
|
|
|
|
| User: "Karl Heinz Buchegger" |
|
| Title: Re: about new problem |
19 Nov 2003 06:25:18 AM |
|
|
Marc Schellens wrote:
diadia wrote:
#include <iostream>
using namespace std;
void main()
{
char *a;
a = new char[1];
cin >> a;
cout << a;
}
I don't know why this code can work and don't occur segmentation
error?
probably you meant cin >> *a instead of cin >> a.
This code is fine, reads a pointer value from cin and writes it to cout.
No it doesn't.
Character pointers are handled different to other pointers
in the stream operations.
The new char[1] is irrelevant.
I wouldn't say so.
Your main function is not C++ standart conform BTW.
right.
Greetings,
marc
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.
|
|
|
|
|

|
Related Articles |
|
|