about new problem

D

diadia

#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?
 
M

Marc Schellens

diadia said:
#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
 
K

Karl Heinz Buchegger

Marc said:
#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.
 
K

Karl Heinz Buchegger

diadia said:
#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.
 
C

Chris Theis

Marc Schellens said:
diadia said:
#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
 
D

David Rubin

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
 
C

chris

#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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top