Google Answers Logo
View Question
 
Q: STL string initialisation problem ( No Answer,   1 Comment )
Question  
Subject: STL string initialisation problem
Category: Computers > Programming
Asked by: njm-ga
List Price: $5.00
Posted: 07 Nov 2002 12:15 PST
Expires: 07 Dec 2002 12:15 PST
Question ID: 102091
Hi,
    I wanted to know what is the difference between these two
initialisation of Rogue Wave STL string. Are they different. Because
when I generated the assembly code for this I found that test.c
generated a piece of code that was not generated by TEST1.c.

TEST.c
#include <string>
#include <iostream>
using namespace std;

int main()
{
        string st;
        st="";
        return 0;
}

TEST1.c
#include <string>
#include <iostream>
using namespace std;

int main()
{
        string st("");
        return 0;
}

Piece of assemly code generated by test.c. Can you just also tell me
what the below code is doing.
        .type   __1cDstdMbasic_string4Ccn0ALchar_traits4Cc__n0AJallocator4Cc___2t6Mrkn0C__v_,2
__1cDstdMbasic_string4Ccn0ALchar_traits4Cc__n0AJallocator4Cc___2t6Mrkn0C__v_:
        save    %sp,-200,%sp
        st      %i1,[%fp+72]
        st      %i0,[%fp+68]

        ! block 1
.L87:
.L88:

! File /opt/forte6.2.patched.2/SUNWspro/WS6U2/include/CC/Cstd/./string:

        ld      [%fp+68],%i4
        st      %g0,[%fp-8]
        add     %fp,-8,%o1
        st      %o1,[%fp-96]
        ld      [%fp+72],%i5
        st      %i4,[%fp-84]
        st      %i5,[%fp-88]
        ld      [%o1+0],%o0
        st      %o0,[%i4+0]
        ld      [%fp+68],%i3
        sethi   %hi(__1cDstdMbasic_string4Ccn0ALchar_traits4Cc__n0AJallocator4Cc___J__nullref_),%o2
        or      %o2,%lo(__1cDstdMbasic_string4Ccn0ALchar_traits4Cc__n0AJallocator4Cc___J__nullref_),%o2
        st      %o2,[%fp-12]
        st      %o2,[%fp-20]
        add     %o2,40,%o0
        st      %o0,[%fp-16]
        add     %fp,-16,%o0
        st      %o0,[%fp-44]
        ld      [%o0+0],%o0
        st      %o0,[%i3+0]
        st      %i3,[%fp-32]
        add     %fp,-36,%o0
        st      %o0,[%fp-24]
        st      %i3,[%fp-28]
        ld      [%i3+0],%o0
        st      %o0,[%fp-36]
        st      %o2,[%fp-48]
        st      %o2,[%fp-80]
        cmp     %o2,%o2
        be      .L91
        st      %o2,[%fp-52]

        ! block 2
.L90:
        st      %o2,[%fp-60]
        st      %o2,[%fp-68]
        st      %o2,[%fp-56]
        add     %o2,24,%o1
        ld      [%o1+0],%o0
        add     %o0,1,%o0
        st      %o0,[%o1+0]
        ba      .L92
        st      %o2,[%fp-64]

        ! block 3
.L91:
.L92:
        jmp     %i7+8
        restore

        ! block 4
.L89:
        jmp     %i7+8
        restore

Thank you
Nitin
Answer  
There is no answer at this time.

Comments  
Subject: Re: STL string initialisation problem
From: opedroso-ga on 09 Nov 2002 17:18 PST
 
Yes, they are different. Even though the extension is .C, this is a
C++ question.

On test.c, the default constructor for string() gets called and then
the operator= function gets called to set its value to the empty
string.

On test1.c, the copy constructor for string(char* p) is called with
the argument of an empty string.

They both have the same final result, create a string st variable
holding an empty string, but different functions are called to
accomplish that.

What is the assembly doing?
Until Block 1 message - allocating memory on the stack to hold the
string st
Until Block 2 message - calling string st constructor to execute
"string st;"
Until Block 3 message - doing operator= to execute "st="";"
After Block 3 message - executing "return 0;"

I could be wrong on the details, since I never seen this machine's
assembly before, but I am sure I am not too far.

Most times, the compiler will have an option to generate the original
source code and the respective assembly statements line by line. See
if the compiler has this option and rerun it.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy