473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Is C++ a type-safe language ??

Hi all,

It is said that C++ is a strongly typed language and thus a type-safe
language (unlike C). So how does one explain the following behaviour :

int main(void)
{
char *p = NULL;
p = "A String Literal";//the compiler isuues no error/warning here
// but ideally it should...as p is a non-const
// pointer and the string literal has the type
// const char *
// So, a conversion from const-ptr TO non-const
// should elicite warning/error from the compiler !!

return 0;
}

I've tried it on both MSVC++ 6 compiler on Windows 2000 ( Intel P IV )
and GNU C++ compiler gcc 3.x RedHat GNU\Linux ( Intel P IV )

Thanks in advance.
Jul 22 '05 #1
21 2473
On 1 Jul 2004 21:58:16 -0700, Nitin Bhardwaj <ni*************@hotmail.com>
wrote:
Hi all,

It is said that C++ is a strongly typed language and thus a type-safe
language (unlike C). So how does one explain the following behaviour :

int main(void)
{
char *p = NULL;
p = "A String Literal";//the compiler isuues no error/warning here
// but ideally it should...as p is a non-const
// pointer and the string literal has the type
// const char *
// So, a conversion from const-ptr TO non-const
// should elicite warning/error from the compiler !!

return 0;
}

I've tried it on both MSVC++ 6 compiler on Windows 2000 ( Intel P IV )
and GNU C++ compiler gcc 3.x RedHat GNU\Linux ( Intel P IV )

Thanks in advance.


It's clearly non-type safe but is done for backward compatibility. When C
was invented there was no const and when C introduced const it was felt
that too much existing code would break if the above was forbidden.
C++ has retained this.

john
Jul 22 '05 #2

"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
news:17**************************@posting.google.c om...
Hi all,

It is said that C++ is a strongly typed language and thus a type-safe
language (unlike C). So how does one explain the following behaviour :

int main(void)
{
char *p = NULL;
p = "A String Literal";//the compiler isuues no error/warning here
// but ideally it should...as p is a non-const
// pointer and the string literal has the type
// const char *
// So, a conversion from const-ptr TO non-const
// should elicite warning/error from the compiler !!

return 0;
}

I've tried it on both MSVC++ 6 compiler on Windows 2000 ( Intel P IV )
and GNU C++ compiler gcc 3.x RedHat GNU\Linux ( Intel P IV )


const-ness is definitely one of the thornier issues in type safety.

next question?

Rufus
Jul 22 '05 #3
* Nitin Bhardwaj:

It is said that C++ is a strongly typed language and thus a type-safe
language (unlike C).
That is incorrect.

However, C++ supports type-safety in more ways than C.

So it's possible to program in C++ in (more or less) type-safe ways.
So how does one explain the following behaviour :

int main(void)
{
char *p = NULL;
p = "A String Literal";//the compiler isuues no error/warning here
// but ideally it should...as p is a non-const
// pointer and the string literal has the type
// const char *
// So, a conversion from const-ptr TO non-const
// should elicite warning/error from the compiler !!

return 0;
}

I've tried it on both MSVC++ 6 compiler on Windows 2000 ( Intel P IV )
and GNU C++ compiler gcc 3.x RedHat GNU\Linux ( Intel P IV )


It's backward compatibility with C.

There are other far more horrendous compatibility-derived issues.

For instance, automatic conversion of array type to pointer type and
treating pointers as arrays, in the context of an array of objects.

On the other hand, in some respects C++ is more type-safe than e.g.
Java.

For example, template support enables static type-checking in many
situations where it's not possible in (old non-generic) Java; C++ har
more stringent type-checking (although not 100%) at link time; C++
constructors enforce class invariants while Java constructors do not.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4

"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
news:17**************************@posting.google.c om...
Hi all,

It is said that C++ is a strongly typed language and thus a type-safe
language (unlike C). So how does one explain the following behaviour :


I would say "strongly typed" is not exactly the same thing as "type-safe".
"strong" is a relative word, is it not?
Jul 22 '05 #5

This link gives a good explanation.
http://www.glenmccl.com/ansi_014.htm
--
Use our news server 'news.foorum.com' from anywhere.
More details at: http://nnrpinfo.go.foorum.com/
Jul 22 '05 #6

"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
news:17**************************@posting.google.c om...
Hi all,

It is said that C++ is a strongly typed language and thus a type-safe
language (unlike C). So how does one explain the following behaviour :

int main(void)
{
char *p = NULL;
p = "A String Literal";//the compiler isuues no error/warning here
// but ideally it should...as p is a non-const
// pointer and the string literal has the type
// const char *
// So, a conversion from const-ptr TO non-const
// should elicite warning/error from the compiler !!
Ideally, what your compiler should do is generate an error about NULL
not being defined.

So you are suggesting that this should be illegal as well?
int n = 5;

Or are you suggesting that a literal 5 should also be treated as an
lvalue?

Try compiling with the commented line:

#include <iostream>
#include <stdio.h>

// main.cpp

int main()
{
const char *p = NULL;
p = "an rvalue";

//char *pc = p; // fails
const char *pc = p;

std::cout << " pc is not " << pc << std::endl;

return 0;
}

return 0;
}

I've tried it on both MSVC++ 6 compiler on Windows 2000 ( Intel P IV )
and GNU C++ compiler gcc 3.x RedHat GNU\Linux ( Intel P IV )

Thanks in advance.

Jul 22 '05 #7
"SaltPeter" <Sa*******@Jupiter.sys> wrote in message news:<Lt**********************@news20.bellglobal.c om>...
"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
news:17**************************@posting.google.c om...
Hi all,

It is said that C++ is a strongly typed language and thus a type-safe
language (unlike C). So how does one explain the following behaviour :

int main(void)
{
char *p = NULL;
p = "A String Literal";//the compiler isuues no error/warning here
// but ideally it should...as p is a non-const
// pointer and the string literal has the type
// const char *
// So, a conversion from const-ptr TO non-const
// should elicite warning/error from the compiler !!
Ideally, what your compiler should do is generate an error about NULL
not being defined.


I had provided a code snippet & not the whole program :-) , Inclusion
of standard header(s) was assumed.

So you are suggesting that this should be illegal as well?
int n = 5;

Or are you suggesting that a literal 5 should also be treated as an
lvalue?

Try compiling with the commented line:

#include <iostream>
#include <stdio.h>

You do not need to seperately #include <stdio.h> to have NULL defined,
<iostream.h> is enough for that !!

// main.cpp

int main()
{
const char *p = NULL;
p = "an rvalue";

//char *pc = p; // fails
Thats what I was pointing out: Here the expression fails because the
type of 'p' is const char * and 'pc' is char *....so the compiler
gives 'cannot convert from const char * to char *'. But why doesn't
give in the following case ?

char *str = "String Literal";

Here also type of "String Literal" is const char * and type of str is
char * !!
const char *pc = p;

std::cout << " pc is not " << pc << std::endl;

return 0;
}

return 0;
}

I've tried it on both MSVC++ 6 compiler on Windows 2000 ( Intel P IV )
and GNU C++ compiler gcc 3.x RedHat GNU\Linux ( Intel P IV )

Thanks in advance.

Jul 22 '05 #8
On 5 Jul 2004 23:52:12 -0700,
Nitin Bhardwaj <ni*************@hotmail.com> wrote:
"SaltPeter" <Sa*******@Jupiter.sys> wrote in message news:<Lt**********************@news20.bellglobal.c om>...

const char *p = NULL;
p = "an rvalue";

//char *pc = p; // fails


Thats what I was pointing out: Here the expression fails because the
type of 'p' is const char * and 'pc' is char *....so the compiler
gives 'cannot convert from const char * to char *'. But why doesn't
give in the following case ?

char *str = "String Literal";

Here also type of "String Literal" is const char * and type of str is
char * !!


It's a special case, in order to make C programmers happy. The ancient
standard draft I have available lists it as deprecated but I don't know
if that stuck - someone else certainly will though.

--
Sam Holden
Jul 22 '05 #9
Thats what I was pointing out: Here the expression fails because the
type of 'p' is const char * and 'pc' is char *....so the compiler
gives 'cannot convert from const char * to char *'. But why doesn't
give in the following case ?

char *str = "String Literal";

Here also type of "String Literal" is const char * and type of str is
char * !!


No it isn't, the type of "String Literal" is const char [15], it's an
array not a pointer.

The conversion of this array or const char to char* is explicitly allowed
by 4.2 paragraph 2 of the standard. It's purpose is compatibility with C.
In the very old days C did not have const and so code like your example
was commonplace.

Nevertheless it is an error to use this to modify a string literal.

One excemption from type safety rules for the purpose of bacwards
compatibility does not make C++ a non-type safe language. It is still more
type safe than any other language in common use (AFAIK).

john
Jul 22 '05 #10

"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
news:17**************************@posting.google.c om...
"SaltPeter" <Sa*******@Jupiter.sys> wrote in message news:<Lt**********************@news20.bellglobal.c om>...
"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
news:17**************************@posting.google.c om...
Hi all,

It is said that C++ is a strongly typed language and thus a type-safe
language (unlike C). So how does one explain the following behaviour :

int main(void)
{
char *p = NULL;
p = "A String Literal";//the compiler isuues no error/warning here
// but ideally it should...as p is a non-const
// pointer and the string literal has the type
// const char *
// So, a conversion from const-ptr TO non-const
// should elicite warning/error from the compiler !!


Ideally, what your compiler should do is generate an error about NULL not being defined.


I had provided a code snippet & not the whole program :-) , Inclusion
of standard header(s) was assumed.


No, its not assumed. Your case is the perfect example why. iostream.h is
deprecated and should not be included in any modern code. Basicly, without
including the headers, NULL could be anything. Surely, you aren't expecting
folks to rely on a crystal ball to substitute values for undefined
constants.

So you are suggesting that this should be illegal as well?
int n = 5;

Or are you suggesting that a literal 5 should also be treated as an
lvalue?

Try compiling with the commented line:

#include <iostream>
#include <stdio.h>

You do not need to seperately #include <stdio.h> to have NULL defined,
<iostream.h> is enough for that !!


iostream.h is not part of the C++ standard anymore.
#include <iostream>

And as Benny Hill use to say...
When you ASSUME, you make an ASS out of U and ME, lol.

// main.cpp

int main()
{
const char *p = NULL;
p = "an rvalue";

//char *pc = p; // fails
Thats what I was pointing out: Here the expression fails because the
type of 'p' is const char * and 'pc' is char *....so the compiler
gives 'cannot convert from const char * to char *'. But why doesn't
give in the following case ?

char *str = "String Literal";

Here also type of "String Literal" is const char * and type of str is
char * !!


How do you figure that an array of chars all of a sudden denotes a pointer
to char? I'm baffled. I can see the str pointer(the lvalue), but how do you
magicly deduce that the string literal, in this case: an array of
characters, has become a pointer, or a constant pointer? Its just an array
of characters, thats it.
const char *pc = p;

std::cout << " pc is not " << pc << std::endl;

return 0;
}

return 0;
}

I've tried it on both MSVC++ 6 compiler on Windows 2000 ( Intel P IV )
and GNU C++ compiler gcc 3.x RedHat GNU\Linux ( Intel P IV )

Thanks in advance.

Jul 22 '05 #11
In message <ZA********************@news20.bellglobal.com>, SaltPeter
<Sa*******@Jupiter.sys> writes

"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
news:17**************************@posting.google. com...
"SaltPeter" <Sa*******@Jupiter.sys> wrote in message

news:<Lt**********************@news20.bellglobal. com>...
> "Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
> news:17**************************@posting.google.c om...
> > Hi all,
> >
> > It is said that C++ is a strongly typed language and thus a type-safe
> > language (unlike C). So how does one explain the following behaviour :
> >
> > int main(void)
> > {
> > char *p = NULL;
> > p = "A String Literal";//the compiler isuues no error/warning here
> > // but ideally it should...as p is a non-const
> > // pointer and the string literal has the type
> > // const char *
> > // So, a conversion from const-ptr TO non-const
> > // should elicite warning/error from the compiler !!
>
> Ideally, what your compiler should do is generate an error aboutNULL > not being defined.


I had provided a code snippet & not the whole program :-) , Inclusion
of standard header(s) was assumed.


No, its not assumed. Your case is the perfect example why. iostream.h is
deprecated and should not be included in any modern code. Basicly, without
including the headers, NULL could be anything. Surely, you aren't expecting
folks to rely on a crystal ball to substitute values for undefined
constants.


Oh, come off it. NULL is cited all over the place in the Standard, and
defined in *seven* different standard headers, any of which could be
included by any of the others. You would have to be out of your mind to
define it as anything else.

--
Richard Herring
Jul 22 '05 #12
Richard Herring wrote:
NULL is cited all over the place in the Standard


Actually it isn't. I just searched on an older PDF copy of the standard
and there were about 20 mentions all of which were in the descriptions
of the C headers that define NULL or in some discussions of them in the
appendices.
Jul 22 '05 #13
In message <cc***********@news.rchland.ibm.com>, Bill Seurer
<se****@us.ibm.com> writes
Richard Herring wrote:
NULL is cited all over the place in the Standard
Actually it isn't.


It doesn't appear on every line of every page, I'll grant you.
I just searched on an older PDF copy of the standard and there were
about 20 mentions all of which were in the descriptions of the C
headers that define NULL
And all those headers are part of the C++ standard.
or in some discussions of them in the appendices.


And in the discussion of streambufs. That's not a C feature.

Be that as it may, my point stands. If you include _any_ standard
header, you can no longer safely assume that NULL is not defined.

--
Richard Herring
Jul 22 '05 #14

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:iy**************@baesystems.com...
In message <ZA********************@news20.bellglobal.com>, SaltPeter
<Sa*******@Jupiter.sys> writes

"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
news:17**************************@posting.google. com...
"SaltPeter" <Sa*******@Jupiter.sys> wrote in messagenews:<Lt**********************@news20.bellglobal. com>...
> "Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
> news:17**************************@posting.google.c om...
> > Hi all,
> >
> > It is said that C++ is a strongly typed language and thus a type-safe > > language (unlike C). So how does one explain the following behaviour : > >
> > int main(void)
> > {
> > char *p = NULL;
> > p = "A String Literal";//the compiler isuues no error/warning here > > // but ideally it should...as p is a non-const
> > // pointer and the string literal has the type
> > // const char *
> > // So, a conversion from const-ptr TO non-const
> > // should elicite warning/error from the compiler !!
>
> Ideally, what your compiler should do is generate an error about

NULL
> not being defined.

I had provided a code snippet & not the whole program :-) , Inclusion
of standard header(s) was assumed.


No, its not assumed. Your case is the perfect example why. iostream.h is
deprecated and should not be included in any modern code. Basicly, withoutincluding the headers, NULL could be anything. Surely, you aren't expectingfolks to rely on a crystal ball to substitute values for undefined
constants.


Oh, come off it. NULL is cited all over the place in the Standard, and
defined in *seven* different standard headers, any of which could be
included by any of the others. You would have to be out of your mind to
define it as anything else.


Wrong, both by fact and principle. The issue is not over where NULL is
defined but rather if NULL is defined. Read the OP's original quest. His
code ignores the include directives used in his program. Therefore:

int main()
{
char *p = NULL
// ....
}

can't be compiled. Code posted in the newsgroup should specify the headers
included.

I strongly disagree with your point of view and the OP's point of view that
included headers should be assumed. All of which is rather relevent since it
turns out he was using iostream.h rather than iostream. That was the whole
point of the NULL issue.

--
Richard Herring

Jul 22 '05 #15
In message <5M********************@news20.bellglobal.com>, SaltPeter
<Sa*******@Jupiter.sys> writes

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:iy**************@baesystems.com...
In message <ZA********************@news20.bellglobal.com>, SaltPeter
<Sa*******@Jupiter.sys> writes
>
>"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
>news:17**************************@posting.google. com...
>> "SaltPeter" <Sa*******@Jupiter.sys> wrote in message
>news:<Lt**********************@news20.bellglobal. com>...
>> > "Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
>> > news:17**************************@posting.google.c om...
>> > > Hi all,
>> > >
>> > > It is said that C++ is a strongly typed language and thus atype-safe >> > > language (unlike C). So how does one explain the followingbehaviour : >> > >
>> > > int main(void)
>> > > {
>> > > char *p = NULL;
>> > > p = "A String Literal";//the compiler isuues no error/warninghere >> > > // but ideally it should...as p is a non-const
>> > > // pointer and the string literal has the type
>> > > // const char *
>> > > // So, a conversion from const-ptr TO non-const
>> > > // should elicite warning/error from the compiler !!
>> >
>> > Ideally, what your compiler should do is generate an error about
>NULL
>> > not being defined.
>>
>> I had provided a code snippet & not the whole program :-) , Inclusion
>> of standard header(s) was assumed.
>
>No, its not assumed. Your case is the perfect example why. iostream.h is
>deprecated and should not be included in any modern code. Basicly,without >including the headers, NULL could be anything. Surely, you aren'texpecting >folks to rely on a crystal ball to substitute values for undefined
>constants.
Oh, come off it. NULL is cited all over the place in the Standard, and
defined in *seven* different standard headers, any of which could be
included by any of the others. You would have to be out of your mind to
define it as anything else.


Wrong, both by fact and principle.


Sounds grand, but on deeper analysis means little. Which fact above is
wrong? Which principle?
The issue is not over where NULL is
defined
No. The issue is the type of a string literal, and why the standard
allows conversion of const pointer to non-const pointer in those
circumstances. Your nitpicking over NULL is a digression from the real
point.
but rather if NULL is defined.
.... but if you insist on such nitpicking, the issue is not _if_ NULL is
defined, but _how_ it might be defined, and consequently whether one can
deduce the OP's intention.
Read the OP's original quest. His
code ignores the include directives used in his program. Therefore:

int main()
{
char *p = NULL
If that had merely been
char * p;
the OP's original question would have been substantively identical, and
the answer the same. NULL is an irrelevancet.
// ....
}

can't be compiled. Code posted in the newsgroup should specify the headers
included.

I strongly disagree with your point of view
Then you misunderstand it. My point is not that included headers should
be assumed, but that the absence of anything that might be defined - per
the standard - in included standard headers must not be assumed. It's in
the same category as things like defining identifiers with leading
underscores.
and the OP's point of view that
included headers should be assumed.
All of which is rather relevent since it
turns out he was using iostream.h rather than iostream.
Hardly. Whichever standard headers he included, there would only be two
possible outcomes:
(a) NULL is undefined
(b) NULL is defined as a null pointer constant.
(c) There is no (c)
Therefore no crystal ball is required to deduce what the OP must have
meant by NULL.
That was the whole
point of the NULL issue.

--
Richard Herring
Jul 22 '05 #16

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:ee**************@baesystems.com...
In message <5M********************@news20.bellglobal.com>, SaltPeter
<Sa*******@Jupiter.sys> writes

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:iy**************@baesystems.com...
In message <ZA********************@news20.bellglobal.com>, SaltPeter
<Sa*******@Jupiter.sys> writes
>
>"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
>news:17**************************@posting.google. com...
>> "SaltPeter" <Sa*******@Jupiter.sys> wrote in message
>news:<Lt**********************@news20.bellglobal. com>...
>> > "Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
>> > news:17**************************@posting.google.c om...
>> > > Hi all,
>> > >
>> > > It is said that C++ is a strongly typed language and thus atype-safe
>> > > language (unlike C). So how does one explain the following

behaviour :
>> > >
>> > > int main(void)
>> > > {
>> > > char *p = NULL;
>> > > p = "A String Literal";//the compiler isuues no error/warning
here
>> > > // but ideally it should...as p is a non-const
>> > > // pointer and the string literal has the type
>> > > // const char *
>> > > // So, a conversion from const-ptr TO non-const
>> > > // should elicite warning/error from the compiler !!
>> >
>> > Ideally, what your compiler should do is generate an error
about >NULL
>> > not being defined.
>>
>> I had provided a code snippet & not the whole program :-) , Inclusion >> of standard header(s) was assumed.
>
>No, its not assumed. Your case is the perfect example why. iostream.h is >deprecated and should not be included in any modern code. Basicly,

without
>including the headers, NULL could be anything. Surely, you aren't

expecting
>folks to rely on a crystal ball to substitute values for undefined
>constants.

Oh, come off it. NULL is cited all over the place in the Standard, and
defined in *seven* different standard headers, any of which could be
included by any of the others. You would have to be out of your mind to
define it as anything else.


Wrong, both by fact and principle.


Sounds grand, but on deeper analysis means little. Which fact above is
wrong? Which principle?


NULL isn't defined in the standard, although rather heavily used and
implied. Example: iterating to past-the-end in an STL container.
The issue is not over where NULL is
defined
No. The issue is the type of a string literal, and why the standard
allows conversion of const pointer to non-const pointer in those
circumstances. Your nitpicking over NULL is a digression from the real
point.


A literal string is NOT a pointer. You are referring to a rule that doesn't
apply here. Its an array of const characters which in the OP's case was used
to initialize a pointer. There is a fine line one needs to walk when one
starts saying statements like: "a temporary integer variable therefore
implies a pointer-to-int". Its does not.
but rather if NULL is defined.
... but if you insist on such nitpicking, the issue is not _if_ NULL is
defined, but _how_ it might be defined, and consequently whether one can
deduce the OP's intention.
Read the OP's original quest. His
code ignores the include directives used in his program. Therefore:

int main()
{
char *p = NULL


If that had merely been
char * p;
the OP's original question would have been substantively identical, and
the answer the same. NULL is an irrelevancet.


NULL, like any constant is relevent. Here, try to compile this:

#include <iostream>

#define NULL ((void *)0)

int()
{
int *p = NULL
std::cout << p;
}

Note the NULL definition using C's definition (does not compile, not allowed
in C++ to initialize a pointer). This solves the issue of why NULL can't and
must not be "assumed".
// ....
}

can't be compiled. Code posted in the newsgroup should specify the
headersincluded.

I strongly disagree with your point of view


Then you misunderstand it. My point is not that included headers should
be assumed, but that the absence of anything that might be defined - per
the standard - in included standard headers must not be assumed. It's in
the same category as things like defining identifiers with leading
underscores.
and the OP's point of view that
included headers should be assumed.


All of which is rather relevent since it
turns out he was using iostream.h rather than iostream.


Hardly. Whichever standard headers he included, there would only be two
possible outcomes:
(a) NULL is undefined
(b) NULL is defined as a null pointer constant.
(c) There is no (c)
Therefore no crystal ball is required to deduce what the OP must have
meant by NULL.


c) explained above. ((void *)0) is illegal in C++.
That was the whole
point of the NULL issue.

--
Richard Herring

Jul 22 '05 #17
"SaltPeter" <Sa*******@Jupiter.sys> wrote
I had provided a code snippet & not the whole program :-) , Inclusion
of standard header(s) was assumed.


No, its not assumed. Your case is the perfect example why. iostream.h is
deprecated and should not be included in any modern code.


iostream.h was never in any standard. Therefore it cannot be
deprecated.
Jul 22 '05 #18

"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:84**************************@posting.google.c om...
"SaltPeter" <Sa*******@Jupiter.sys> wrote
I had provided a code snippet & not the whole program :-) , Inclusion
of standard header(s) was assumed.


No, its not assumed. Your case is the perfect example why. iostream.h is
deprecated and should not be included in any modern code.


iostream.h was never in any standard. Therefore it cannot be
deprecated.


Thats not correct, the old header was deprecated by the ISO/ANSI C++
committee.
http://www.parashift.com/c++-faq-lit...standards.html
Jul 22 '05 #19
"SaltPeter" <Sa*******@Jupiter.sys> wrote...
"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:84**************************@posting.google.c om...
"SaltPeter" <Sa*******@Jupiter.sys> wrote

> I had provided a code snippet & not the whole program :-) , Inclusion > of standard header(s) was assumed.

No, its not assumed. Your case is the perfect example why. iostream.h is deprecated and should not be included in any modern code.


iostream.h was never in any standard. Therefore it cannot be
deprecated.


Thats not correct, the old header was deprecated by the ISO/ANSI C++
committee.
http://www.parashift.com/c++-faq-lit...standards.html


I think you're confused about what is deprecated. Read that FAQ chapter
carefully. C headers that have the .h form are deprecated. C++ headers
have never had standard form with .h, so they simply cannot be deprecated.
Deprecated means "OK in this edition of the Standard, but may not be OK
later, so don't rely on them".

Better yet, get yourself a copy of the Standard. You will see no mention
of <iostream.h> in it.

V
Jul 22 '05 #20

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:tj3Ic.66153$Oq2.42680@attbi_s52...
"SaltPeter" <Sa*******@Jupiter.sys> wrote...
"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:84**************************@posting.google.c om...
"SaltPeter" <Sa*******@Jupiter.sys> wrote
>
> > I had provided a code snippet & not the whole program :-) , Inclusion > > of standard header(s) was assumed.
>
> No, its not assumed. Your case is the perfect example why.
iostream.h
is > deprecated and should not be included in any modern code.

iostream.h was never in any standard. Therefore it cannot be
deprecated.


Thats not correct, the old header was deprecated by the ISO/ANSI C++
committee.
http://www.parashift.com/c++-faq-lit...standards.html


I think you're confused about what is deprecated. Read that FAQ chapter
carefully. C headers that have the .h form are deprecated. C++ headers
have never had standard form with .h, so they simply cannot be deprecated.
Deprecated means "OK in this edition of the Standard, but may not be OK
later, so don't rely on them".

Better yet, get yourself a copy of the Standard. You will see no mention
of <iostream.h> in it.

V


You've got a point about the use of "deprecated", it doesn't mean
admonished, replaced or removed.
Jul 22 '05 #21
In message <8J*********************@news20.bellglobal.com>, SaltPeter
<Sa*******@Jupiter.sys> writes

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:ee**************@baesystems.com...
In message <5M********************@news20.bellglobal.com>, SaltPeter
<Sa*******@Jupiter.sys> writes
>
>"Richard Herring" <ju**@[127.0.0.1]> wrote in message
>news:iy**************@baesystems.com...
>> In message <ZA********************@news20.bellglobal.com>, SaltPeter
>> <Sa*******@Jupiter.sys> writes
>> >
>> >"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
>> >news:17**************************@posting.google. com...
>> >> "SaltPeter" <Sa*******@Jupiter.sys> wrote in message
>> >news:<Lt**********************@news20.bellglobal. com>...
>> >> > "Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
>> >> > news:17**************************@posting.google.c om...
>> >> > > Hi all,
>> >> > >
>> >> > > It is said that C++ is a strongly typed language and thus a
>type-safe
>> >> > > language (unlike C). So how does one explain the following
>behaviour :
>> >> > >
>> >> > > int main(void)
>> >> > > {
>> >> > > char *p = NULL;
>> >> > > p = "A String Literal";//the compiler isuues noerror/warning >here
>> >> > > // but ideally it should...as p is a non-const
>> >> > > // pointer and the string literal has the type
>> >> > > // const char *
>> >> > > // So, a conversion from const-ptr TO non-const
>> >> > > // should elicite warning/error from the compiler !!
>> >> >
>> >> > Ideally, what your compiler should do is generate an errorabout >> >NULL
>> >> > not being defined.
>> >>
>> >> I had provided a code snippet & not the whole program :-) ,Inclusion >> >> of standard header(s) was assumed.
>> >
>> >No, its not assumed. Your case is the perfect example why. iostream.his >> >deprecated and should not be included in any modern code. Basicly,
>without
>> >including the headers, NULL could be anything. Surely, you aren't
>expecting
>> >folks to rely on a crystal ball to substitute values for undefined
>> >constants.
>>
>> Oh, come off it. NULL is cited all over the place in the Standard, and
>> defined in *seven* different standard headers, any of which could be
>> included by any of the others. You would have to be out of your mind to
>> define it as anything else.
>
>Wrong, both by fact and principle.
Sounds grand, but on deeper analysis means little. Which fact above is
wrong? Which principle?


NULL isn't defined in the standard,


"C.2.2.3
"The macro NULL, defined in any of <clocale>, <cstddef>, <cstdio>,
<cstlib>, <cstring>, <ctime> or <cwchar>, is an implementation-defined
C++ null pointer constant in this International Standard (18.1)"

18.1.4 says much the same. So its replacement text has to be something
which constitutes a legal C++ null pointer constant.
although rather heavily used and
implied. Example: iterating to past-the-end in an STL container.
How is iterating to past-the end a use of NULL?
>The issue is not over where NULL is
>defined
No. The issue is the type of a string literal, and why the standard
allows conversion of const pointer to non-const pointer in those
circumstances. Your nitpicking over NULL is a digression from the real
point.


A literal string is NOT a pointer.


Did I say it was?
You are referring to a rule that doesn't
apply here.
I'm not.
Its an array of const characters which in the OP's case was used
to initialize a pointer.
"4.2.
"A string literal ... can be converted to an rvalue of type "pointer to
char"... For the purpose of ranking in overload resolution, this
conversion is considered an array-to-pointer conversion followed by a
qualification conversion."
There is a fine line one needs to walk when one
starts saying statements like: "a temporary integer variable therefore
implies a pointer-to-int". Its does not.
Since nobody is saying that, so what? You appear to be arguing against
several things that nobody is saying.
> but rather if NULL is defined.


... but if you insist on such nitpicking, the issue is not _if_ NULL is
defined, but _how_ it might be defined, and consequently whether one can
deduce the OP's intention.
>Read the OP's original quest. His
>code ignores the include directives used in his program. Therefore:
>
>int main()
>{
> char *p = NULL


If that had merely been
char * p;
the OP's original question would have been substantively identical, and
the answer the same. NULL is an irrelevancet.


NULL, like any constant is relevent. Here, try to compile this:

#include <iostream>

#define NULL ((void *)0)

int()


Do you mean int main() ?
{
int *p = NULL
Needs a semicolon.
std::cout << p;
}

Note the NULL definition using C's definition (does not compile, not allowed
in C++ to initialize a pointer).
Right, so why are you using a C definition in C++ ?
This solves the issue of why NULL can't and
must not be "assumed".


Not surprisingly, even with the corrections noted, it fails to compile.
*However*, the message _my_ compiler gives is "Redefinition of 'NULL' is
not identical".

If I remove the erroneous definition of NULL, _my_ compiler has no
problems compiling this:

#include <iostream>
int main()
{
int * p = NULL;
std::cout << p;
}

This solves by example the issue of why NULL can't and must not be
"assumed" _not_ to be defined.
> // ....
>}
>
>can't be compiled. Code posted in the newsgroup should specify theheaders >included.
>
>I strongly disagree with your point of view


Then you misunderstand it. My point is not that included headers should
be assumed, but that the absence of anything that might be defined - per
the standard - in included standard headers must not be assumed. It's in
the same category as things like defining identifiers with leading
underscores.
>and the OP's point of view that
>included headers should be assumed.


> All of which is rather relevent since it
>turns out he was using iostream.h rather than iostream.


Hardly. Whichever standard headers he included, there would only be two
possible outcomes:
(a) NULL is undefined
(b) NULL is defined as a null pointer constant.
(c) There is no (c)
Therefore no crystal ball is required to deduce what the OP must have
meant by NULL.


c) explained above. ((void *)0) is illegal in C++.


And nobody but yourself has suggested otherwise.
>That was the whole
>point of the NULL issue.


NULL is a null pointer constant. That doesn't mean it's ((void*)0) .
--
Richard Herring
Jul 22 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
2
by: Jason Cartwright | last post by:
I have an abstract base class and two derived classes that I want to serialize and deserialize with schema validation. When I serialize instances of the derived classes the XmlSerializer adds the...
6
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is ...
0
by: Chris Fink | last post by:
When I am consuming a webservice, an object has an undefined value (inq3Type.Call3Data). I do not completely understand why this is happening and apologize for the vague question. My assumption...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
3
by: john | last post by:
Hi to All To demonstrate: public class MyBaseGenericClass<T> { } public class MyGenericClass1<T: MyBaseGenericClass<T> {
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
5
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
3
by: amanjsingh | last post by:
Hi, I am trying to implement Java Web Service using Apache Axis2 and Eclipse as a tool. I have created the basic code and deployed the service using various eclipse plugin but when I try to invoke...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.