This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between gcc and turbo c++ compiler

 
Rachit Kumar Saxena
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Formally, the code snippet you have shown causes "UNDEFINED BEHAVIOUR". This means anything can happen -- including what you consider as "working".
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think C strings are immutable?
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I presume that is why it throws a seg fault.
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed a "But" in your post, ... and yet it seems that we are agreeing with each other?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic