读书人

困扰小弟我3天的有关问题高手指教一

发布时间: 2012-08-31 12:55:03 作者: rapoo

困扰我3天的问题,高手指教一下吧
一个静态库,内有函数
struct PCDATA
{
DWORD ID;
char name[20];
char data[250];
}
struct PCDATA * getmem(DWORD id)
{
struct PCDATA * p;
p=(struct PCDATA *)malloc(sizeof(struct PCDATA));
p->ID=id;
return p;
}
ref class A
{
private:
struct PCDATA * d; //标记 这样使用是否正确,是否会出现
public:
A(DWORD Value)
{
d=getmem(Value);
};
}
在主函数中
array<A^>^ arrayA=gcnew array<A^>(999999);
for(int i=0;i<999999;i++)
{
arrayA[i]=(A^)gcnew A(i);
}
问题:
1,这样一运行就出现内存溢出错误,为什么呢
2,当垃圾回收机制运行后,在标记处是指向地址是否会改变,地址内容会被更改么
谢谢,各位高手,请指教一下


[解决办法]
溢出,应该是堆内存小了
至于,非托管对象包含在托管类型中 msdn有代码演示

C/C++ code
      Visual C++  如何:包装本机类以供 C# 使用  请参见  示例  语言筛选器: 全部 语言筛选器: 多个 语言筛选器: Visual Basic 语言筛选器: C# 语言筛选器: C++ 语言筛选器: J# 语言筛选器: JScript   Visual Basic(声明)  Visual Basic(用法)  C#  C++  J#  JScript 本示例说明如何包装本机 C++ 类以便用 C# 或其他 .NET 语言编写的代码可以使用它。示例  复制代码 // wrap_native_class_for_mgd_consumption.cpp// compile with: /clr /LD#include <windows.h>#include <vcclr.h>#using <System.dll>using namespace System;class UnmanagedClass {public:   LPCWSTR GetPropertyA() { return 0; }   void MethodB( LPCWSTR ) {}};public ref class ManagedClass {public:   // Allocate the native object on the C++ Heap via a constructor   ManagedClass() : m_Impl( new UnmanagedClass ) {}      // Deallocate the native object on a destructor   ~ManagedClass() {      delete m_Impl;   }protected:   // Deallocate the native object on the finalizer just in case no destructor is called   !ManagedClass() {      delete m_Impl;   }public:   property String ^  get_PropertyA {      String ^ get() {         return gcnew String( m_Impl->GetPropertyA());      }   }   void MethodB( String ^ theString ) {      pin_ptr<const WCHAR> str = PtrToStringChars(theString);      m_Impl->MethodB(str);   }private:   UnmanagedClass * m_Impl;}; 请参见参考使用 C++ Interop(隐式 PInvoke) 要提出有关“帮助”或本产品其他功能的建议或错误报告,请转到反馈站点。 

读书人网 >VC

热点推荐