字符串的位置?

  

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;

void print(ostringstream&, ofstream&);
int main()
{   
    char* p;
    int* pint;
    ofstream fout("result.txt");
    string s = "ABCDEFG";
    int n = 33;
    float x = 2.718;
    ostringstream oss;
    print(oss, fout);
    oss << s;
    print(oss, fout);
    oss << " " << n;
    print(oss, fout);
    oss << " " << x;
    print(oss, fout);
   
    /*
    p = &oss;  //这是强类型匹配,不允许直接取地址
    cout << "*(p-5) = " << *(p-5) << endl;
    */
   
    p = &s[0]; //强类型匹配,只能这么做
    fout << "p = " << p << endl;
    fout << "&s[0] = " << &s[0] << endl;
    fout << "&p = " << &p << endl;
    fout << "&s = " << &s << endl;
    pint = (int*)(&s[0]);
    fout << "&s[0] = " << pint << endl;
    pint = (int*)(&p+sizeof("ABCDEFG"));
    fout << "(int*)(&p+sizeof(\"ABCDEFG\")) = " << *pint << endl;
    fout << "*(p-1) = " << *(p-1) << endl;
    fout << "--p = " << --p << endl;      //这后边两个是不可打印字符
    fout << "sizeof(string)+sizeof(s)+sizeof(x)+sizeof(n)+sizeof(oss)="
         << sizeof(string)+sizeof(s)+sizeof(x)+sizeof(n)+sizeof(oss) << endl;
    p = (char*)(&p);        
    for (int i=0; i<15; i++)
    {
        fout << "p+" << i << " = " << (char*)(&p+i) << endl;
        if (9==i || 8 == i || 7 == i)
        {
            //fout << "static_cast<int>(*p) = " << static_cast<int*>(p) << endl;  不能转换指针
            fout << "static_cast<int>(*p) = " << static_cast<int>(*p) << endl;  //可以转换指针所指数据
            //pint = p;
            //fout << "*pint = " << *pint << endl;
        }
    }                
    fout << "p+sizeof(int)+1 = " << p+sizeof(int)+1 << endl; 
    fout << "p+sizeof(string) = " << p+sizeof(string) << endl;
    fout << "p+sizeof(\"ABCDEFG\") = " << p+sizeof("ABCDEFG") << endl;
    fout << "p+sizeof(\"ABCDEFG\")+1 = " << p+sizeof("ABCDEFG")+1 << endl;
    fout << "p+sizeof(\"ABCDEFG\")+1 = " << p+sizeof("ABCDEFG")+1 << endl;
    //p = (char *)(&p-sizeof(pint)-sizeof(fout)-sizeof("result.txt")-sizeof(string)-sizeof(s));
    //下潜500行都找不到    
    fout << "*p = " << *p << endl;
    for (int i=0; i<1000; i++)
    {
        fout << "p-" << i << " = " << *(p-i) << endl;
    }
         
    
    system("pause");
    return 4;
}

void print(ostringstream& oss, ofstream& fout)
{
    cout << "oss.str() = \"" << oss.str() << "\"" << endl;
    fout << "oss.str() = \"" << oss.str() << "\"" << endl;
}

结果:

oss.str() = ""
oss.str() = "ABCDEFG"
oss.str() = "ABCDEFG 33"
oss.str() = "ABCDEFG 33 2.718"
p = ABCDEFG
&s[0] = ABCDEFG
&p = 0x22ff5c
&s = 0x22fe40
&s[0] = 0x3e2744
(int*)(&p+sizeof("ABCDEFG")) = 4198887
*(p-1) = 
--p = ABCDEFG
sizeof(string)+sizeof(s)+sizeof(x)+sizeof(n)+sizeof(oss)=196
p+0 = \"
p+1 = 纖
p+2 =
p+3 =
p+4 =
p+5 =
p+6 =
p+7 = ?"
static_cast<int>(*p) = 92
p+8 = ?@
static_cast<int>(*p) = 92
p+9 =
static_cast<int>(*p) = 92
p+10 = H$>
p+11 = ?>
p+12 =
p+13 = ?"
p+14 = ?"
p+sizeof(int)+1 = w
p+sizeof(string) = 纖
p+sizeof("ABCDEFG") =
p+sizeof("ABCDEFG")+1 =
p+sizeof("ABCDEFG")+1 =
*p = \
p-0 = \
p-1 = 
p-2 = "
p-3 = 
p-4 = |
p-5 = 
p-6 = >
p-7 = $
p-8 = (
p-9 = w
p-10 = ?
p-11 = ?
p-12 =
p-13 = w
p-14 = ?
p-15 = ?
p-16 = ?
p-17 = 
p-18 = 
p-19 = 
p-20 = 
p-21 = 
p-22 = D
p-23 = ?
p-24 = ?
p-25 = 
p-26 = D
p-27 = ?
p-28 = ?
p-29 = 

。。。

注意:这里下潜500字符,都没有找到串"ABCDEFG"。其实我犯笨了,因为print用的是引用,拷贝的是ostringstream对象的地址值。

             "ABCDEFG"就只有一个。然后它的位置在另外一个位置。

     pint = (int*)(&s[0]);
     fout << "&s[0] = " << pint << endl;

&s[0] = ABCDEFG
&p = 0x22ff5c
&s = 0x22fe40
&s[0] = 0x3e2744

&p 与 &s 是同一个进程内存区。

&s[0] 是经过转换,破坏C自动解析字符串规则后取到的。0x3e2744跟0x22ff5c相距很远,可见他们两个是属于两片不同的内存区。

而且一个是0x2,一个是0x3。也就是说0x2是进程区,0x3是常量区。所以p从0x22ff5c开始下潜500也没有找到"ABCDEFG"。

   p = &s[0];   
    for (int i=0; i<50; i++)
    {
        pint = (int*)(p);
        fout << "p+" << i << " = " << pint << " " << *(p+i) << endl;
    }

用这段程序,才发现它是怎么存储的。

p+0 = 0x3e2744 A
p+1 = 0x3e2744 B
p+2 = 0x3e2744 C
p+3 = 0x3e2744 D
p+4 = 0x3e2744 E
p+5 = 0x3e2744 F
p+6 = 0x3e2744 G
p+7 = 0x3e2744 
p+8 = 0x3e2744 ?
p+9 = 0x3e2744 ?
p+10 = 0x3e2744 ?
p+11 = 0x3e2744 ?
p+12 = 0x3e2744 ?
p+13 = 0x3e2744 ?
p+14 = 0x3e2744 ?
p+15 = 0x3e2744 ?
p+16 = 0x3e2744 ?
p+17 = 0x3e2744 ?
p+18 = 0x3e2744 ?
p+19 = 0x3e2744 ?
p+20 = 0x3e2744 
p+21 = 0x3e2744

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值