帮助找到复制动态内存的有效方法!! [英] help finding an efficient way to copy dynamic memory!!

查看:64
本文介绍了帮助找到复制动态内存的有效方法!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了自己的复制功能来处理我自己的动态内存

结构。它有效,但我觉得它效率不高。必须有

更快的方式来复制数据。在我的一些例程中我开发了复制函数被调用数百次,其中

非常大的数据结构。这可能需要一些时间....


如果您无法通过代码判断数据结构是存储复数的
矩阵。


附上的是代码。


struct complexMatrix

{

float r ;

浮动i;

};


typedef complexMatrix ** ComplexMatrix;

typedef complexMatrix复杂;


ComplexMatrix Cmxcopy(ComplexMatrix mat,int rows,int cols)

{


int i;

int j;

ComplexMatrix temp;

temp = CmxAlloc(rows,cols);


for(i = 0; i< rows; i ++)

for(j = 0; j< cols; j ++)

{

temp [i] [j] .r = mat [0] [0] .r;

temp [i] [j] .i = mat [0] [0] .i;

}


返回临时;


}

所有建议,评论,电子邮件,传真,邮寄和备忘录欢迎。

I have developed my own copy function for coping my own dynamic memory
structure. It works, but I feel its not too efficient. There must be
a quicker way to copy the data. In some of the routines I have
developed the copy function gets called several hundreds of times, with
very large data structures. This can take some time....

If you can''t tell by the code the data structure is a matrix for
storing complex numbers.

Attached is the code.

struct complexMatrix
{
float r;
float i;
};

typedef complexMatrix** ComplexMatrix;
typedef complexMatrix Complex;

ComplexMatrix Cmxcopy(ComplexMatrix mat, int rows, int cols)
{

int i;
int j;
ComplexMatrix temp;
temp=CmxAlloc(rows,cols);

for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
{
temp[i][j].r=mat[0][0].r;
temp[i][j].i=mat[0][0].i;
}

return temp;

}
All suggesting, comments, emails, faxes, post-its and memos welcome.

推荐答案

laclac1,有潜力在执行

CmxCopy之后的内存泄漏,除非你为ComplexMatrix实现了正确的析构函数。

laclac1, There is a potential for memory leakage after executing
CmxCopy unless you implement the correct destructor for ComplexMatrix.


la ****** @ yahoo.com 写道:
我已经开发了自己的复制功能来处理我自己的动态记忆
结构。它有效,但我觉得它效率不高。必须有更快捷的方式来复制数据。在我开发的一些例程中,复制函数被调用数百次,具有非常大的数据结构。这可能需要一些时间......

如果你不能通过代码告诉数据结构是一个存储复杂数字的矩阵。

随附的是代码。

struct complexMatrix
{
浮动r;
浮动i;
};

typedef complexMatrix ** ComplexMatrix;
typedef complexMatrix Complex;

ComplexMatrix Cmxcopy(ComplexMatrix mat,int rows,int cols)
{

int i;
int j;
ComplexMatrix temp;
temp = CmxAlloc(rows,cols);


这是非常类似于C的。你为什么不开始编写C ++?只在需要时声明

对象,并避免定义而不进行初始化:


ComplexMatrix temp = CmxAlloc(rows,cols);

for(i = 0; i< rows; i ++)


for(int i = 0; i< rows; i ++)

for(j = 0; j< cols; j ++)


for(int j = 0; j< cols; j ++)

{
temp [i ] [j] .r = mat [0] [0] .r;
temp [i] [j] .i = mat [0] [0] .i;


为什么要复制''mat''的左上角元素?不应该这样吗


temp [i] [j] .r = mat [i] [j] .r;

temp [i] [j] .i = mat [i] [j] .i;

???

}


既然你的''complexMatrix''struct是一个POD,你_may_使用memcpy复制

该类型的对象。这可能会改善一些事情:


memcpy(& temp [i] [j],& mat [i] [j],sizeof(temp [i] [j] ));


如果你将指针保持在当前的

元素并且每个内部循环增加它而不是索引,它可以进一步改进(对于

一些编译器它带来了一些优势)。


实际改进它的另一种方法是避免双重分配/填充

矩阵,然后开发''allocate_and_copy''并在''CmxAlloc'和'循环中使用它'



返回temp;

}

所有建议,评论,电子邮件,传真,邮寄和备忘录欢迎。
I have developed my own copy function for coping my own dynamic memory
structure. It works, but I feel its not too efficient. There must be
a quicker way to copy the data. In some of the routines I have
developed the copy function gets called several hundreds of times, with
very large data structures. This can take some time....

If you can''t tell by the code the data structure is a matrix for
storing complex numbers.

Attached is the code.

struct complexMatrix
{
float r;
float i;
};

typedef complexMatrix** ComplexMatrix;
typedef complexMatrix Complex;

ComplexMatrix Cmxcopy(ComplexMatrix mat, int rows, int cols)
{

int i;
int j;
ComplexMatrix temp;
temp=CmxAlloc(rows,cols);
This is very C-like. Why don''t you start writing C++? Only declare
objects when they are needed, and avoid defining without initialising:

ComplexMatrix temp = CmxAlloc(rows, cols);

for(i=0;i<rows;i++)
for (int i=0; i<rows; i++)
for(j=0;j<cols;j++)
for (int j=0; j<cols; j++)
{
temp[i][j].r=mat[0][0].r;
temp[i][j].i=mat[0][0].i;
Why are you copying the top left element of the ''mat''? Shouldn''t this be

temp[i][j].r=mat[i][j].r;
temp[i][j].i=mat[i][j].i;
???
}
Since your ''complexMatrix'' struct is a POD you _may_ use memcpy to copy
objects of that type. That might improve things a bit:

memcpy(&temp[i][j], &mat[i][j], sizeof(temp[i][j]));

And it can be improved even further if you keep the pointer to the current
element around and increment it every inner loop, instead of indexing (for
some compilers it gives a bit of advantage).

Another way to actually improve it is to avoid dual allocation/filling
of the matrix and instead develop a ''allocate_and_copy'' and use it in
place of ''CmxAlloc'' and the loops.

return temp;

}
All suggesting, comments, emails, faxes, post-its and memos welcome.




V



V


Frank Chang写道:
Frank Chang wrote:
laclac1,除非你为ComplexMatrix实现正确的析构函数,否则在执行CmxCopy之后可能会发生内存泄漏。
laclac1, There is a potential for memory leakage after executing
CmxCopy unless you implement the correct destructor for ComplexMatrix.




Fra请注意。 ''ComplexMatrix''指向''complexMatrix''结构的指针

。没有析构函数。



Frank, please pay attention. ''ComplexMatrix'' is a pointer to a pointer
to ''complexMatrix'' struct. There is no destructor.


这篇关于帮助找到复制动态内存的有效方法!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆