Sun Studio 11: C++ 5.8 Compiler

View: New views
4 Messages — Rating Filter:   Alert me  

Sun Studio 11: C++ 5.8 Compiler

by neelabhsharma1 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

As per the specifications the compiler should be based on C99. But i think it still does not handle the function call strtoll(). This issue did not arise with RHL 9.0

If it did the handling properly then the result of the program should not be 0.


/*Snippet of the Code */
#include<stdio.h>
#include<stdlib.h>

int main()
{
char *a = "89abcdef";
long long int c;
a[8] = '\0';
c = strtoll(a, NULL, 16);
printf("the num is %8x", c);
return 0;
}


My understanding of the OS, Platform (SPARC), ofcourse C is just of a beginner and am eager to learn, please provide supporting comments..


Thanks and Regards

Re: Sun Studio 11: C++ 5.8 Compiler

by Michael T Pins :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

neelabhsharma1@... writes:

>As per the specifications the compiler should be based on C99. But i think it still does not handle the function call strtoll(). This issue did not arise with RHL 9.0

>If it did the handling properly then the result of the program should not be 0.

By "result of the program" I assume you mean the output of the printf
command.  As your code is broken (and lint will show you where), the output
is undefined.  Fix your code and the output becomes what you expect.

>/*Snippet of the Code */
>#include<stdio.h>
>#include<stdlib.h>

>int main()
>{
>char *a = "89abcdef";
>long long int c;
>a[8] = '\0';
>c = strtoll(a, NULL, 16);
>printf("the num is %8x", c);
>return 0;
>}

--
Michael T Pins              | "A year from now I'd be surprised if
mtpins@...            | there's not some grand square in Baghdad
keeper of the nn sources    | that is named after President Bush."
ftp://ftp.nndev.org/pub     | -Richard Perle, 9/22/03

Re: Sun Studio 11: C++ 5.8 Compiler

by Steven Leikeim :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Apr 16, 2007 at 01:22:07AM -0000, neelabhsharma1@... wrote:

> As per the specifications the compiler should be based on C99. But i think it still does not handle the function call strtoll(). This issue did not arise with RHL 9.0
>
> If it did the handling properly then the result of the program should not be 0.
>
>
> /*Snippet of the Code */
> #include<stdio.h>
> #include<stdlib.h>
>
> int main()
> {
> char *a = "89abcdef";
> long long int c;
> a[8] = '\0';
> c = strtoll(a, NULL, 16);
> printf("the num is %8x", c);
> return 0;
> }
>
>
> My understanding of the OS, Platform (SPARC), ofcourse C is just of a beginner and am eager to learn, please provide supporting comments..
>

This is consistent with Sun WorkShop 6 and gcc 3.4.2 (Both on Solaris/Sparc).
Gcc 3.4.5 on Linux (Intel) appears to do what you were expecting, but actually
does not. If you change the "char a..." definition to a hex value longer than
8 characters you will not see the last 8 characters on Sparc. On Intel you will
only see the last 8 characters. This is due to differences in byte ordering on
these processors.

The problem is in your printf format. %8x expects to print something of type
"int". Most current compilers specify type "int" to be the same as "long int"
which is 32 bits. "long long int" is 64 bits. What is happening here is you
are printing the first 32 bits of the argument. If you use the correctly
typed format string "%8llx", you should see the correct value printed in all
cases.

I hope this helps your understanding of what has happened here.


Steven Leikeim

--

Steven Leikeim, GSEC-Gold                 | We, the willing
Department of Electrical and Computer     | led by the unknowing
     Engineering                          | are doing the impossible
Schulich School of Engineering            | for the ungrateful.
University of Calgary                     | We have done so much
Calgary, Alberta                          | for so long with so little
                                          | we are now qualified
Phone: (403) 220-5373 Fax: (403) 282-6855 | to do anything with nothing.

Parent Message unknown Re: Sun Studio 11: C++ 5.8 Compiler

by Jonathan Leffler :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

focus-sun Digest 16 Apr 2007 17:18:39 -0000 Issue 372

Sun Studio 11: C++ 5.8 Compiler
                 1456 by: neelabhsharma1.gmail.com

[...]

As per the specifications the compiler should be based on C99. But i think
it still does not handle the function call strtoll(). This issue did not
arise with RHL 9.0

If it did the handling properly then the result of the program should not
be 0.


/*Snippet of the Code */
#include<stdio.h>
#include<stdlib.h>

int main()
{
char *a = "89abcdef";
long long int c;
a[8] = '\0';
c = strtoll(a, NULL, 16);
printf("the num is %8x", c);
return 0;
}


My understanding of the OS, Platform (SPARC), ofcourse C is just of a
beginner and am eager to learn, please provide supporting comments..



[JL:
Did you say C or C++?  C++ is still based on the 1998 C++ standard, which
in turn is based on C90 and not on C99.  Hence things like long long are
(most likely) not part of the base C++ configuration - it is likely that
you would have to enable such extensions in C++ by special options.  {That
was a guess - investigation shows it is a wrong guess...read on.}

Which bits of the manual did you read?

Tested on Solaris 10 with
Black JL: cc -V
cc: Sun C 5.8 2005/10/13
usage: cc [ options] files.  Use 'cc -flags' for details
Black JL: CC -V
CC: Sun C++ 5.8 2005/10/13

Also, the code must be fixed - %8x prints an integer, not a long long int,
so the format string needs to end "%8llx\n".

The C compiler is fine with the fixed code.   The C++ compiler says:

CC -O x.c -o x && ./x
"x.c", line 6: Warning: String literal converted to char* in
initialization.
1 Warning(s) detected.
Segmentation Fault(coredump)

Hmmm...that's because you try to modify the string literal at x[8] = '\0'
- which is a pointless exercise (the value is already '\0').  The warning
is because it should be a const char * you assign to.  With that fixed,
even CC compiles it and runs it OK.

So, you don't even have to enable long long in CC - it just works with
correct code.
]


--
Jonathan Leffler (jleffler@...)
STSM, Informix Database Engineering, IBM Information Management Division
4100 Bohannon Drive, Menlo Park, CA 94025-1013
Tel: +1 650-926-6921    Tie-Line: 630-6921
"I don't suffer from insanity; I enjoy every minute of it!"