sizetest add2;是不是有问题?

解决方案 »

  1.   

    BOOL相当于int
    bool只占一位
    In Visual C++4.2, the Standard C++ header files contained a typedef that equated bool with int. In Visual C++ 5.0 and later, bool is implemented as a built-in type with a size of 1 byte. That means that for Visual C++ 4.2, a call of sizeof(bool) yields 4, while in Visual C++ 5.0 and later, the same call yields 1. This can cause memory corruption problems if you have defined structure members of type bool in Visual C++ 4.2 and are mixing object files (OBJ) and/or DLLs built with the 4.2 and 5.0 or later compilers. 
      

  2.   

     //cout << "add2.b address:" << &add2.b << endl; //请回答:这一行为何输出是乱码?如果把 b 类型改为 bool 或 int 则正常(我这里是这样)
    改成如下的,则没有问题
        printf("add2.b address:%d\n",&add2.b);
      

  3.   

    to hopping:
    struct sizetest add2 是C的风格
    sizetest add2 是C++的风格。to seesi:
    我知道bool是1byte。注意看我问的问题:
    cout << "add2.b address:" << (&add2.b) << endl; //请回答:这一行为何输出是乱码?如果把 b 类型改为 bool 或 int 则正常(我这里是这样)
      

  4.   

    你不是在开玩笑吧?
    (&add2.b)的类型是什么?是char*。
    cout << (char*)p;会做什么,会试图输出一个字符串嘛!改成
    cout << "add2.b address:" << (void*)(&add2.b) << endl; 
    肯定没错。
      

  5.   

    to seesi:
    好,先给5分!
    为什么会出现这个错误?为什么把b的类型改了就没有这个错误?