Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is C++ a type-safe language ??

Reply
Thread Tools

Is C++ a type-safe language ??

 
 
Nitin Bhardwaj
Guest
Posts: n/a
 
      07-02-2004
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.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-02-2004
On 1 Jul 2004 21:58:16 -0700, Nitin Bhardwaj <(E-Mail Removed)>
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
 
Reply With Quote
 
 
 
 
Rufus V. Smith
Guest
Posts: n/a
 
      07-02-2004

"Nitin Bhardwaj" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-02-2004
* 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?
 
Reply With Quote
 
Anonymous
Guest
Posts: n/a
 
      07-02-2004

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/
 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      07-02-2004

"Nitin Bhardwaj" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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?


 
Reply With Quote
 
SaltPeter
Guest
Posts: n/a
 
      07-02-2004

"Nitin Bhardwaj" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.



 
Reply With Quote
 
Nitin Bhardwaj
Guest
Posts: n/a
 
      07-06-2004
"SaltPeter" <(E-Mail Removed)> wrote in message news:<LtkFc.195935$(E-Mail Removed) om>...
> "Nitin Bhardwaj" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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.

 
Reply With Quote
 
Sam Holden
Guest
Posts: n/a
 
      07-06-2004
On 5 Jul 2004 23:52:12 -0700,
Nitin Bhardwaj <(E-Mail Removed)> wrote:
> "SaltPeter" <(E-Mail Removed)> wrote in message news:<LtkFc.195935$(E-Mail Removed) 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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-06-2004

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
writing a compiler for Monster language using C language Shravani C Programming 8 03-16-2008 09:36 PM
A language-agnostic language Ed Java 24 03-27-2006 08:19 PM
c is a low-level language or neither low level nor high level language pabbu C Programming 8 11-07-2005 03:05 PM
Using a Scripting Language as Your Scripting Language DaveInSidney Python 0 05-09-2005 03:13 AM
Python is the best and most popular general purpose scripting language; the universal scripting language Ron Stephens Python 23 04-12-2004 05:32 PM



Advertisments