File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes C / C++ and the fly likes Difference between gcc and turbo c++ compiler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "Difference between gcc and turbo c++ compiler" Watch "Difference between gcc and turbo c++ compiler" New topic
Author

Difference between gcc and turbo c++ compiler

Rachit Kumar Saxena
Ranch Hand

Joined: Dec 24, 2011
Posts: 50
Strings are immutable.So When I TYPE:

char *a="hello";
a[2]='o';
It works in turbo c++,while crashes in gcc.Why?
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Formally, the code snippet you have shown causes "UNDEFINED BEHAVIOUR". This means anything can happen -- including what you consider as "working".

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
What makes you think C strings are immutable?
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

@Campbell -- I assume you posed the question to Rachit, the OP.

While there is some sloppiness in the OP's wording, character string literals are indeed immutable, and modifying them causes Undefined behaviour (Section 6.4.5, Paragraph 7).

Some relevant links:

http://www.c-faq.com/decl/strlitinit.html
http://www.c-faq.com/ansi/strlitnotconst.html
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Thank you. I presume that is why it throws a seg fault.
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

You are welcome, Campbell.

The OP says it throws a seg fault when compiled with gcc, but not with Turbo C. I'd say this suggests gcc is a better compiler than Turbo C (this is indeed the case in reality -- gcc is actively maintained and significanly more standard-conforming than Turbo C).

That, that the gcc generated executable crashes, is a good thing. That, that the Turbo C generated executable apparently works is a terrible thing.

Of course, with a JVM interpreting byte code over a sand-box, different Java implementations do not pose such inconsistencies to the beginner.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Thank you. But a Java™ virtual machine is more tightly controlled; the Java™ Language Specification is strict about lots of things, eg the behaviour of i = i++; So the behaviour of different JVMs should be more similar to one another.
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

I noticed a "But" in your post, ... and yet it seems that we are agreeing with each other?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Sorry for delay in replying.
Yes, we agree, but I was thinking, maybe the explanation for Java™’s consistency is because the language specification is so strict.
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

You are right. C and C++ leaves a lot unspecified (or undefined or allows the implementation to specify/define) whereas the JLS does not.

And no worries regarding "delay in replying". It is amazing what you folks (sheriffs, moderators, and other active members) contribute and how you find the time to do so. Thank you and everyone in the Ranch for that.

sincerely,
- Anand
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between gcc and turbo c++ compiler