zoukankan      html  css  js  c++  java
  • c++ new 的相关

    首先是一个链接  这里 说的很详细了

    http://www.cnblogs.com/alephsoul-alephsoul/archive/2012/10/17/2728019.html

    关于c++ new 的本质

    =======

    在上面基础上 要说的 是 new 和 malloc  的区别

    首先是 malloc的相关用法

    http://www.cnblogs.com/zhangyabin---acm/p/3188076.html

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    using namespace std;
    
    //测试大小端的宏
    static union {char c[4];unsigned int l;} endian_test = {{'l','?','?','b'}};
    #define ENDIANNESS ((char)endian_test.l)
    #pragma pack(4)
    struct MyStruct{
    
    	char m_char;
    	long  m_int;
    };
    class TestArray
    {
    public:
    	TestArray()
    	{
    
    		cout<<"this is construct"<<endl;
    	}
     ~TestArray()
     {
    
    	cout<<"this is destruct"<<endl;
     }
    };
    
    
    int main()
    {
    
      ///测试new一个数组时的指针前几个个字节是保留了 数组的长度
    
           //当以下数组是int 或者MyStruct 时 向前取的值 完全是0 
          //  所以得出只对 自定义的class 才有意义
    	TestArray* pArray =  new TestArray[257];
    
    	int* pPoint = (int*)pArray  - 2;
    	cout<<"the pre 4 byte's value is "<< *pPoint<<endl;
    	printf("the pre  address is %p
    ",pPoint);
    	printf("the array  address is %p
    ",pArray);
    
    	char* pChar = (char*)pPoint;
    	int array[8] = {0};
    
    
    	for(int i = 0 ; i < 8; ++i)
    	{
    		array[i] = *pChar;
    		++pChar;
    		cout<<"the " << i <<" byte 's value is " << array[i]<<endl;
    	}
    
        return 0;  
    }
       
    

    以下是区别

    http://blog.csdn.net/zjc156m/article/details/16819357

    最后是 new 在数组时需要注意的

    http://blog.csdn.net/hazir/article/details/21413833

    这里说的和详细

    以上要补充的是

    我在 mac  os 、64位机器 上  实验的效果是 自定义类  在new  数组时  实际返回指针的向前偏移8 个字节 后 取一个int * p 得到 数组的长度

    但是 这里的只对 自定义的class  有意义, 基本类型 和 自定义的 struct  这里取到的是 0

    见下面的代码

  • 相关阅读:
    C# 截取窗口图像(包括遮挡窗口,最小化窗口)
    C# 自定义DevExpress中XtraMessageBox的窗口Icon
    C# DevExpress的皮肤,没有在多线程中应用
    DevExpress的RibbonForm在WIN7下标题栏透明,没应用程序设置的Skin
    C# RibbonForm的标题Text显示不完全的解决方法
    C# 使用递归获取文件路径,不获取拒绝访问的文件夹
    socket使用
    KMP模板
    HDU1358(KMP)
    windows环境变量
  • 原文地址:https://www.cnblogs.com/CodeWu91/p/7482664.html
Copyright © 2011-2022 走看看